source: rtems-docs/posix-users/mutex.rst @ 12dccfe

5
Last change on this file since 12dccfe was 12dccfe, checked in by Sebastian Huber <sebastian.huber@…>, on 01/09/19 at 15:14:05

Remove superfluous "All rights reserved."

  • Property mode set to 100644
File size: 14.9 KB
Line 
1.. comment SPDX-License-Identifier: CC-BY-SA-4.0
2
3.. Copyright (C) 1988, 2002 On-Line Applications Research Corporation (OAR)
4
5Mutex Manager
6#############
7
8Introduction
9============
10
11The mutex manager implements the functionality required of the mutex manager as
12defined by POSIX 1003.1b-1996. This standard requires that a compliant
13operating system provide the facilties to ensure that threads can operate with
14mutual exclusion from one another and defines the API that must be provided.
15
16The services provided by the mutex manager are:
17
18- pthread_mutexattr_init_ - Initialize a Mutex Attribute Set
19
20- pthread_mutexattr_destroy_ - Destroy a Mutex Attribute Set
21
22- pthread_mutexattr_setprotocol_ - Set the Blocking Protocol
23
24- pthread_mutexattr_getprotocol_ - Get the Blocking Protocol
25
26- pthread_mutexattr_setprioceiling_ - Set the Priority Ceiling
27
28- pthread_mutexattr_getprioceiling_ - Get the Priority Ceiling
29
30- pthread_mutexattr_setpshared_ - Set the Visibility
31
32- pthread_mutexattr_getpshared_ - Get the Visibility
33
34- pthread_mutex_init_ - Initialize a Mutex
35
36- pthread_mutex_destroy_ - Destroy a Mutex
37
38- pthread_mutex_lock_ - Lock a Mutex
39
40- pthread_mutex_trylock_ - Poll to Lock a Mutex
41
42- pthread_mutex_timedlock_ - Lock a Mutex with Timeout
43
44- pthread_mutex_unlock_ - Unlock a Mutex
45
46- pthread_mutex_setprioceiling_ - Dynamically Set the Priority Ceiling
47
48- pthread_mutex_getprioceiling_ - Dynamically Get the Priority Ceiling
49
50Background
51==========
52
53Mutex Attributes
54----------------
55
56Mutex attributes are utilized only at mutex creation time. A mutex attribute
57structure may be initialized and passed as an argument to the ``mutex_init``
58routine. Note that the priority ceiling of a mutex may be set at run-time.
59
60.. list-table::
61 :class: rtems-table
62
63 * - *blocking protcol*
64   - is the XXX
65 * - *priority ceiling*
66   - is the XXX
67 * - *pshared*
68   - is the XXX
69
70PTHREAD_MUTEX_INITIALIZER
71-------------------------
72
73This is a special value that a variable of type ``pthread_mutex_t`` may be
74statically initialized to as shown below:
75
76.. code-block:: c
77
78    pthread_mutex_t my_mutex = PTHREAD_MUTEX_INITIALIZER;
79
80This indicates that ``my_mutex`` will be automatically initialized by an
81implicit call to ``pthread_mutex_init`` the first time the mutex is used.
82
83Note that the mutex will be initialized with default attributes.
84
85Operations
86==========
87
88There is currently no text in this section.
89
90Services
91========
92
93This section details the mutex manager's services.  A subsection is dedicated
94to each of this manager's services and describes the calling sequence, related
95constants, usage, and status codes.
96
97.. _pthread_mutexattr_init:
98
99pthread_mutexattr_init - Initialize a Mutex Attribute Set
100---------------------------------------------------------
101.. index:: pthread_mutexattr_init
102.. index:: initialize a mutex attribute set
103
104**CALLING SEQUENCE:**
105
106.. code-block:: c
107
108    #include <pthread.h>
109    int pthread_mutexattr_init(
110        pthread_mutexattr_t *attr
111    );
112
113**STATUS CODES:**
114
115*EINVAL*
116    The attribute pointer argument is invalid.
117
118**DESCRIPTION:**
119
120The ``pthread_mutexattr_init`` routine initializes the mutex attributes object
121specified by ``attr`` with the default value for all of the individual
122attributes.
123
124**NOTES:**
125
126XXX insert list of default attributes here.
127
128.. _pthread_mutexattr_destroy:
129
130pthread_mutexattr_destroy - Destroy a Mutex Attribute Set
131---------------------------------------------------------
132.. index:: pthread_mutexattr_destroy
133.. index:: destroy a mutex attribute set
134
135**CALLING SEQUENCE:**
136
137.. code-block:: c
138
139    #include <pthread.h>
140        int pthread_mutexattr_destroy(
141        pthread_mutexattr_t *attr
142    );
143
144**STATUS CODES:**
145
146.. list-table::
147 :class: rtems-table
148
149 * - ``EINVAL``
150   - The attribute pointer argument is invalid.
151 * - ``EINVAL``
152   - The attribute set is not initialized.
153
154**DESCRIPTION:**
155
156The ``pthread_mutex_attr_destroy`` routine is used to destroy a mutex
157attributes object. The behavior of using an attributes object after it is
158destroyed is implementation dependent.
159
160**NOTES:**
161
162NONE
163
164.. _pthread_mutexattr_setprotocol:
165
166pthread_mutexattr_setprotocol - Set the Blocking Protocol
167---------------------------------------------------------
168.. index:: pthread_mutexattr_setprotocol
169.. index:: set the blocking protocol
170
171**CALLING SEQUENCE:**
172
173.. code-block:: c
174
175    #include <pthread.h>
176    int pthread_mutexattr_setprotocol(
177        pthread_mutexattr_t *attr,
178        int                  protocol
179    );
180
181**STATUS CODES:**
182
183.. list-table::
184 :class: rtems-table
185
186 * - ``EINVAL``
187   - The attribute pointer argument is invalid.
188 * - ``EINVAL``
189   - The attribute set is not initialized.
190 * - ``EINVAL``
191   - The protocol argument is invalid.
192
193**DESCRIPTION:**
194
195The ``pthread_mutexattr_setprotocol`` routine is used to set value of the
196``protocol`` attribute. This attribute controls the order in which threads
197waiting on this mutex will receive it.
198
199The ``protocol`` can be one of the following:
200
201.. list-table::
202 :class: rtems-table
203
204 * - ``PTHREAD_PRIO_NONE``
205   - in which case blocking order is FIFO.
206 * - ``PTHREAD_PRIO_INHERIT``
207   - in which case blocking order is priority with the priority inheritance
208     protocol in effect.
209 * - ``PTHREAD_PRIO_PROTECT``
210   - in which case blocking order is priority with the priority ceiling
211     protocol in effect.
212
213**NOTES:**
214
215There is currently no way to get simple priority blocking ordering with POSIX
216mutexes even though this could easily by supported by RTEMS.
217
218.. _pthread_mutexattr_getprotocol:
219
220pthread_mutexattr_getprotocol - Get the Blocking Protocol
221---------------------------------------------------------
222.. index:: pthread_mutexattr_getprotocol
223.. index:: get the blocking protocol
224
225**CALLING SEQUENCE:**
226
227.. code-block:: c
228
229    #include <pthread.h>
230    int pthread_mutexattr_getprotocol(
231        pthread_mutexattr_t *attr,
232        int                 *protocol
233    );
234
235**STATUS CODES:**
236
237.. list-table::
238 :class: rtems-table
239
240 * - ``EINVAL``
241   - The attribute pointer argument is invalid.
242 * - ``EINVAL``
243   - The attribute set is not initialized.
244 * - ``EINVAL``
245   - The protocol pointer argument is invalid.
246
247**DESCRIPTION:**
248
249The ``pthread_mutexattr_getprotocol`` routine is used to obtain the value of
250the ``protocol`` attribute. This attribute controls the order in which threads
251waiting on this mutex will receive it.
252
253**NOTES:**
254
255NONE
256
257.. _pthread_mutexattr_setprioceiling:
258
259pthread_mutexattr_setprioceiling - Set the Priority Ceiling
260-----------------------------------------------------------
261.. index:: pthread_mutexattr_setprioceiling
262.. index:: set the priority ceiling
263
264**CALLING SEQUENCE:**
265
266.. code-block:: c
267
268    #include <pthread.h>
269    int pthread_mutexattr_setprioceiling(
270        pthread_mutexattr_t *attr,
271        int                  prioceiling
272    );
273
274**STATUS CODES:**
275
276.. list-table::
277 :class: rtems-table
278
279 * - ``EINVAL``
280   - The attribute pointer argument is invalid.
281 * - ``EINVAL``
282   - The attribute set is not initialized.
283 * - ``EINVAL``
284   - The prioceiling argument is invalid.
285
286**DESCRIPTION:**
287
288The ``pthread_mutexattr_setprioceiling`` routine is used to set value of the
289``prioceiling`` attribute. This attribute specifies the priority that is the
290ceiling for threads obtaining this mutex. Any task obtaining this mutex may not
291be of greater priority that the ceiling. If it is of lower priority, then its
292priority will be elevated to ``prioceiling``.
293
294**NOTES:**
295
296NONE
297
298.. _pthread_mutexattr_getprioceiling:
299
300pthread_mutexattr_getprioceiling - Get the Priority Ceiling
301-----------------------------------------------------------
302.. index:: pthread_mutexattr_getprioceiling
303.. index:: get the priority ceiling
304
305**CALLING SEQUENCE:**
306
307.. code-block:: c
308
309    #include <pthread.h>
310    int pthread_mutexattr_getprioceiling(
311        const pthread_mutexattr_t *attr,
312        int                       *prioceiling
313    );
314
315**STATUS CODES:**
316
317.. list-table::
318 :class: rtems-table
319
320 * - ``EINVAL``
321   - The attribute pointer argument is invalid.
322 * - ``EINVAL``
323   - The attribute set is not initialized.
324 * - ``EINVAL``
325   - The prioceiling pointer argument is invalid.
326
327**DESCRIPTION:**
328
329The ``pthread_mutexattr_getprioceiling`` routine is used to obtain the value of
330the ``prioceiling`` attribute. This attribute specifies the priority ceiling
331for this mutex.
332
333**NOTES:**
334
335NONE
336
337.. _pthread_mutexattr_setpshared:
338
339pthread_mutexattr_setpshared - Set the Visibility
340-------------------------------------------------
341.. index:: pthread_mutexattr_setpshared
342.. index:: set the visibility
343
344**CALLING SEQUENCE:**
345
346.. code-block:: c
347
348    #include <pthread.h>
349    int pthread_mutexattr_setpshared(
350        pthread_mutexattr_t *attr,
351        int                  pshared
352    );
353
354**STATUS CODES:**
355
356.. list-table::
357 :class: rtems-table
358
359 * - ``EINVAL``
360   - The attribute pointer argument is invalid.
361 * - ``EINVAL``
362   - The attribute set is not initialized.
363 * - ``EINVAL``
364   - The pshared argument is invalid.
365
366**DESCRIPTION:**
367
368**NOTES:**
369
370.. _pthread_mutexattr_getpshared:
371
372pthread_mutexattr_getpshared - Get the Visibility
373-------------------------------------------------
374.. index:: pthread_mutexattr_getpshared
375.. index:: get the visibility
376
377**CALLING SEQUENCE:**
378
379.. code-block:: c
380
381    #include <pthread.h>
382    int pthread_mutexattr_getpshared(
383        const pthread_mutexattr_t *attr,
384        int                       *pshared
385    );
386
387**STATUS CODES:**
388
389.. list-table::
390 :class: rtems-table
391
392 * - ``EINVAL``
393   - The attribute pointer argument is invalid.
394 * - ``EINVAL``
395   - The attribute set is not initialized.
396 * - ``EINVAL``
397   - The pshared pointer argument is invalid.
398
399**DESCRIPTION:**
400
401**NOTES:**
402
403.. _pthread_mutex_init:
404
405pthread_mutex_init - Initialize a Mutex
406---------------------------------------
407.. index:: pthread_mutex_init
408.. index:: initialize a mutex
409
410**CALLING SEQUENCE:**
411
412.. code-block:: c
413
414    #include <pthread.h>
415    int pthread_mutex_init(
416        pthread_mutex_t           *mutex,
417        const pthread_mutexattr_t *attr
418    );
419
420**STATUS CODES:**
421
422.. list-table::
423 :class: rtems-table
424
425 * - ``EINVAL``
426   - The attribute set is not initialized.
427 * - ``EINVAL``
428   - The specified protocol is invalid.
429 * - ``EAGAIN``
430   - The system lacked the necessary resources to initialize another mutex.
431 * - ``ENOMEM``
432   - Insufficient memory exists to initialize the mutex.
433 * - ``EBUSY``
434   - Attempted to reinialize the object reference by mutex, a previously
435     initialized, but not yet destroyed.
436
437**DESCRIPTION:**
438
439**NOTES:**
440
441.. _pthread_mutex_destroy:
442
443pthread_mutex_destroy - Destroy a Mutex
444---------------------------------------
445.. index:: pthread_mutex_destroy
446.. index:: destroy a mutex
447
448**CALLING SEQUENCE:**
449
450.. code-block:: c
451
452    #include <pthread.h>
453        int pthread_mutex_destroy(
454        pthread_mutex_t *mutex
455    );
456
457**STATUS CODES:**
458
459.. list-table::
460 :class: rtems-table
461
462 * - ``EINVAL``
463   - The specified mutex is invalid.
464 * - ``EBUSY``
465   - Attempted to destroy the object reference by mutex, while it is locked or
466     referenced by another thread.
467
468**DESCRIPTION:**
469
470**NOTES:**
471
472.. _pthread_mutex_lock:
473
474pthread_mutex_lock - Lock a Mutex
475---------------------------------
476.. index:: pthread_mutex_lock
477.. index:: lock a mutex
478
479**CALLING SEQUENCE:**
480
481.. code-block:: c
482
483    #include <pthread.h>
484    int pthread_mutex_lock(
485        pthread_mutex_t *mutex
486    );
487
488**STATUS CODES:**
489
490.. list-table::
491 :class: rtems-table
492
493 * - ``EINVAL``
494   - The specified mutex is invalid.
495 * - ``EINVAL``
496   - The mutex has the protocol attribute of ``PTHREAD_PRIO_PROTECT`` and the
497     priority of the calling thread is higher than the current priority
498     ceiling.
499 * - ``EDEADLK``
500   - The current thread already owns the mutex.
501
502**DESCRIPTION:**
503
504**NOTES:**
505
506.. _pthread_mutex_trylock:
507
508pthread_mutex_trylock - Poll to Lock a Mutex
509--------------------------------------------
510.. index:: pthread_mutex_trylock
511.. index:: poll to lock a mutex
512
513**CALLING SEQUENCE:**
514
515.. code-block:: c
516
517    #include <pthread.h>
518    int pthread_mutex_trylock(
519        pthread_mutex_t *mutex
520    );
521
522**STATUS CODES:**
523
524.. list-table::
525 :class: rtems-table
526
527 * - ``EINVAL``
528   - The specified mutex is invalid.
529 * - ``EINVAL``
530   - The mutex has the protocol attribute of ``PTHREAD_PRIO_PROTECT`` and the
531     priority of the calling thread is higher than the current priority ceiling.
532 * - ``EBUSY``
533   - The mutex is already locked.
534
535**DESCRIPTION:**
536
537**NOTES:**
538
539.. _pthread_mutex_timedlock:
540
541pthread_mutex_timedlock - Lock a Mutex with Timeout
542---------------------------------------------------
543.. index:: pthread_mutex_timedlock
544.. index:: lock a mutex with timeout
545
546**CALLING SEQUENCE:**
547
548.. code-block:: c
549
550    #include <pthread.h>
551    #include <time.h>
552    int pthread_mutex_timedlock(
553        pthread_mutex_t       *mutex,
554        const struct timespec *timeout
555    );
556
557**STATUS CODES:**
558
559.. list-table::
560 :class: rtems-table
561
562 * - ``EINVAL``
563   - The specified mutex is invalid.
564 * - ``EINVAL``
565   - The nanoseconds field of timeout is invalid.
566 * - ``EINVAL``
567   - The mutex has the protocol attribute of ``PTHREAD_PRIO_PROTECT`` and the
568     priority of the calling thread is higher than the current priority
569     ceiling.
570 * - ``EDEADLK``
571   - The current thread already owns the mutex.
572 * - ``ETIMEDOUT``
573   - The calling thread was unable to obtain the mutex within the specified
574     timeout period.
575
576**DESCRIPTION:**
577
578**NOTES:**
579
580.. _pthread_mutex_unlock:
581
582pthread_mutex_unlock - Unlock a Mutex
583-------------------------------------
584.. index:: pthread_mutex_unlock
585.. index:: unlock a mutex
586
587**CALLING SEQUENCE:**
588
589.. code-block:: c
590
591    #include <pthread.h>
592    int pthread_mutex_unlock(
593        pthread_mutex_t *mutex
594    );
595
596**STATUS CODES:**
597
598.. list-table::
599 :class: rtems-table
600
601 * - ``EINVAL``
602   - The specified mutex is invalid.
603
604**DESCRIPTION:**
605
606**NOTES:**
607
608.. _pthread_mutex_setprioceiling:
609
610pthread_mutex_setprioceiling - Dynamically Set the Priority Ceiling
611-------------------------------------------------------------------
612.. index:: pthread_mutex_setprioceiling
613.. index:: dynamically set the priority ceiling
614
615**CALLING SEQUENCE:**
616
617.. code-block:: c
618
619    #include <pthread.h>
620    int pthread_mutex_setprioceiling(
621        pthread_mutex_t *mutex,
622        int              prioceiling,
623        int             *oldceiling
624    );
625
626**STATUS CODES:**
627
628.. list-table::
629 :class: rtems-table
630
631 * - ``EINVAL``
632   - The oldceiling pointer parameter is invalid.
633 * - ``EINVAL``
634   - The prioceiling parameter is an invalid priority.
635 * - ``EINVAL``
636   - The specified mutex is invalid.
637
638**DESCRIPTION:**
639
640**NOTES:**
641
642.. _pthread_mutex_getprioceiling:
643
644pthread_mutex_getprioceiling - Get the Current Priority Ceiling
645---------------------------------------------------------------
646.. index:: pthread_mutex_getprioceiling
647.. index:: get the current priority ceiling
648
649**CALLING SEQUENCE:**
650
651.. code-block:: c
652
653    #include <pthread.h>
654    int pthread_mutex_getprioceiling(
655        pthread_mutex_t *mutex,
656        int             *prioceiling
657    );
658
659**STATUS CODES:**
660
661.. list-table::
662 :class: rtems-table
663
664 * - ``EINVAL``
665   - The prioceiling pointer parameter is invalid.
666 * - ``EINVAL``
667   - The specified mutex is invalid.
668
669**DESCRIPTION:**
670
671**NOTES:**
Note: See TracBrowser for help on using the repository browser.