source: rtems-docs/posix_users/mutex.rst @ 9aafb39

4.115
Last change on this file since 9aafb39 was 489740f, checked in by Chris Johns <chrisj@…>, on 05/20/16 at 02:47:09

Set SPDX License Identifier in each source file.

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