source: rtems-docs/posix-users/semaphore.rst @ 42d50d7

5
Last change on this file since 42d50d7 was c5161ee, checked in by Sebastian Huber <sebastian.huber@…>, on 10/24/17 at 08:50:45

posix-users: Clarify timed operation errors

Close #3182.

  • Property mode set to 100644
File size: 15.1 KB
Line 
1.. comment SPDX-License-Identifier: CC-BY-SA-4.0
2
3.. COMMENT: COPYRIGHT (c) 1989-2008.
4.. COMMENT: On-Line Applications Research Corporation (OAR).
5.. COMMENT: All rights reserved.
6
7Semaphore Manager
8#################
9
10Introduction
11============
12
13The semaphore manager provides functions to allocate, delete, and control
14semaphores. This manager is based on the POSIX 1003.1 standard.
15
16The directives provided by the semaphore manager are:
17
18- sem_init_ - Initialize an unnamed semaphore
19
20- sem_destroy_ - Destroy an unnamed semaphore
21
22- sem_open_ - Open a named semaphore
23
24- sem_close_ - Close a named semaphore
25
26- sem_unlink_ - Remove a named semaphore
27
28- sem_wait_ - Lock a semaphore
29
30- sem_trywait_ - Lock a semaphore
31
32- sem_timedwait_ - Wait on a Semaphore for a Specified Time
33
34- sem_post_ - Unlock a semaphore
35
36- sem_getvalue_ - Get the value of a semeaphore
37
38Background
39==========
40
41Theory
42------
43
44Semaphores are used for synchronization and mutual exclusion by indicating the
45availability and number of resources. The task (the task which is returning
46resources) notifying other tasks of an event increases the number of resources
47held by the semaphore by one. The task (the task which will obtain resources)
48waiting for the event decreases the number of resources held by the semaphore
49by one. If the number of resources held by a semaphore is insufficient (namely
500), the task requiring resources will wait until the next time resources are
51returned to the semaphore. If there is more than one task waiting for a
52semaphore, the tasks will be placed in the queue.
53
54"sem_t" Structure
55-----------------
56.. index:: sem_t
57
58The ``sem_t`` structure is used to represent semaphores. It is passed as an
59argument to the semaphore directives and is defined as follows:
60
61.. code-block:: c
62
63    typedef int sem_t;
64
65Building a Semaphore Attribute Set
66----------------------------------
67
68Operations
69==========
70
71Using as a Binary Semaphore
72---------------------------
73
74Although POSIX supports mutexes, they are only visible between threads. To work
75between processes, a binary semaphore must be used.
76
77Creating a semaphore with a limit on the count of 1 effectively restricts the
78semaphore to being a binary semaphore. When the binary semaphore is available,
79the count is 1. When the binary semaphore is unavailable, the count is 0.
80
81Since this does not result in a true binary semaphore, advanced binary features
82like the Priority Inheritance and Priority Ceiling Protocols are not available.
83
84There is currently no text in this section.
85
86Directives
87==========
88
89This section details the semaphore manager's directives.  A subsection is
90dedicated to each of this manager's directives and describes the calling
91sequence, related constants, usage, and status codes.
92
93.. _sem_init:
94
95sem_init - Initialize an unnamed semaphore
96------------------------------------------
97.. index:: sem_init
98.. index:: initialize an unnamed semaphore
99
100**CALLING SEQUENCE:**
101
102.. code-block:: c
103
104    int sem_init(
105        sem_t        *sem,
106        int           pshared,
107        unsigned int  value
108    );
109
110**STATUS CODES:**
111
112.. list-table::
113 :class: rtems-table
114
115 * - ``EINVAL``
116   - The value argument exceeds ``SEM_VALUE_MAX``
117 * - ``ENOSPC``
118   - A resource required to initialize the semaphore has been exhausted The
119     limit on semaphores (``SEM_VALUE_MAX``) has been reached
120 * - ``ENOSYS``
121   - The function sem_init is not supported by this implementation
122 * - ``EPERM``
123   - The process lacks appropriate privileges to initialize the semaphore
124
125**DESCRIPTION:**
126
127The ``sem_init`` function is used to initialize the unnamed semaphore referred
128to by ``sem``. The value of the initialized semaphore is the parameter
129``value``. The semaphore remains valid until it is destroyed.
130
131.. COMMENT: ADD MORE HERE XXX
132
133**NOTES:**
134
135If the functions completes successfully, it shall return a value of zero.
136otherwise, it shall return a value of -1 and set ``errno`` to specify the error
137that occurred.
138
139Multiprocessing is currently not supported in this implementation.
140
141.. _sem_destroy:
142
143sem_destroy - Destroy an unnamed semaphore
144------------------------------------------
145.. index:: sem_destroy
146.. index:: destroy an unnamed semaphore
147
148**CALLING SEQUENCE:**
149
150.. code-block:: c
151
152    int sem_destroy(
153        sem_t *sem
154    );
155
156**STATUS CODES:**
157
158.. list-table::
159 :class: rtems-table
160
161 * - ``EINVAL``
162   - The value argument exceeds ``SEM_VALUE_MAX``
163 * - ``ENOSYS``
164   - The function ``sem_init`` is not supported by this implementation
165 * - ``EBUSY``
166   - There are currently processes blocked on the semaphore
167
168**DESCRIPTION:**
169
170The ``sem_destroy`` function is used to destroy an unnamed semaphore refered to
171by ``sem``. ``sem_destroy`` can only be used on a semaphore that was created
172using sem_init.
173
174**NOTES:**
175
176If the functions completes successfully, it shall return a value of zero.
177Otherwise, it shall return a value of -1 and set ``errno`` to specify the error
178that occurred.
179
180Multiprocessing is currently not supported in this implementation.
181
182.. _sem_open:
183
184sem_open - Open a named semaphore
185---------------------------------
186.. index:: sem_open
187.. index:: open a named semaphore
188
189**CALLING SEQUENCE:**
190
191.. code-block:: c
192
193    int sem_open(
194        const char *name,
195        int         oflag
196    );
197
198**ARGUMENTS:**
199
200The following flag bit may be set in oflag:
201
202.. list-table::
203 :class: rtems-table
204
205 * - ``O_CREAT``
206   - Creates the semaphore if it does not already exist. If ``O_CREAT`` is set
207     and the semaphore already exists then ``O_CREAT`` has no
208     effect. Otherwise, ``sem_open()`` creates a semaphore. The ``O_CREAT``
209     flag requires the third and fourth argument: mode and value of type
210     ``mode_t`` and ``unsigned int``, respectively.
211 * - ``O_EXCL``
212   - If ``O_EXCL`` and ``O_CREAT`` are set, all call to ``sem_open()`` shall
213     fail if the semaphore name exists
214
215**STATUS CODES:**
216
217.. list-table::
218 :class: rtems-table
219
220 * - ``EACCES``
221   - Valid name specified but oflag permissions are denied, or the semaphore
222     name specified does not exist and permission to create the named semaphore
223     is denied.
224 * - ``EEXIST``
225   - ``O_CREAT`` and ``O_EXCL`` are set and the named semaphore already exists.
226 * - ``EINTR``
227   - The ``sem_open()`` operation was interrupted by a signal.
228 * - ``EINVAL``
229   - The ``sem_open()`` operation is not supported for the given name.
230 * - ``EMFILE``
231   - Too many semaphore descriptors or file descriptors in use by this process.
232 * - ``ENAMETOOLONG``
233   - The length of the name exceed ``PATH_MAX`` or name component is longer
234     than ``NAME_MAX`` while ``POSIX_NO_TRUNC`` is in effect.
235 * - ``ENOENT``
236   - ``O_CREAT`` is not set and the named semaphore does not exist.
237 * - ``ENOSPC``
238   - There is insufficient space for the creation of a new named semaphore.
239 * - ``ENOSYS``
240   - The function ``sem_open()`` is not supported by this implementation.
241
242**DESCRIPTION:**
243
244The ``sem_open()`` function establishes a connection between a specified
245semaphore and a process. After a call to sem_open with a specified semaphore
246name, a process can reference to semaphore by the associated name using the
247address returned by the call. The oflag arguments listed above control the
248state of the semaphore by determining if the semaphore is created or accessed
249by a call to ``sem_open()``.
250
251**NOTES:**
252
253.. _sem_close:
254
255sem_close - Close a named semaphore
256-----------------------------------
257.. index:: sem_close
258.. index:: close a named semaphore
259
260**CALLING SEQUENCE:**
261
262.. code-block:: c
263
264    int sem_close(
265        sem_t *sem_close
266    );
267
268**STATUS CODES:**
269
270.. list-table::
271 :class: rtems-table
272
273 * - ``EACCES``
274   - The semaphore argument is not a valid semaphore descriptor.
275 * - ``ENOSYS``
276   - The function ``sem_close`` is not supported by this implementation.
277
278**DESCRIPTION:**
279
280The ``sem_close()`` function is used to indicate that the calling process is
281finished using the named semaphore indicated by ``sem``. The function
282``sem_close`` deallocates any system resources that were previously allocated
283by a ``sem_open`` system call. If ``sem_close()`` completes successfully it
284returns a 1, otherwise a value of -1 is return and ``errno`` is set.
285
286**NOTES:**
287
288.. _sem_unlink:
289
290sem_unlink - Unlink a semaphore
291-------------------------------
292.. index:: sem_unlink
293.. index:: unlink a semaphore
294
295**CALLING SEQUENCE:**
296
297.. code-block:: c
298
299    int sem_unlink(
300        const char *name
301    );
302
303**STATUS CODES:**
304
305.. list-table::
306 :class: rtems-table
307
308 * - ``EACCESS``
309   - Permission is denied to unlink a semaphore.
310 * - ``ENAMETOOLONG``
311   - The length of the strong name exceed ``NAME_MAX`` while ``POSIX_NO_TRUNC``
312     is in effect.
313 * - ``ENOENT``
314   - The name of the semaphore does not exist.
315 * - ``ENOSPC``
316   - There is insufficient space for the creation of a new named semaphore.
317 * - ``ENOSYS``
318   - The function ``sem_unlink`` is not supported by this implementation.
319
320**DESCRIPTION:**
321
322The ``sem_unlink()`` function shall remove the semaphore name by the string
323name. If a process is currently accessing the name semaphore, the
324``sem_unlink`` command has no effect. If one or more processes have the
325semaphore open when the ``sem_unlink`` function is called, the destruction of
326semaphores shall be postponed until all reference to semaphore are destroyed by
327calls to ``sem_close``, ``_exit()``, or ``exec``.  After all references have
328been destroyed, it returns immediately.
329
330If the termination is successful, the function shall return 0. Otherwise, a -1
331is returned and the ``errno`` is set.
332
333**NOTES:**
334
335.. _sem_wait:
336
337sem_wait - Wait on a Semaphore
338------------------------------
339.. index:: sem_wait
340.. index:: wait on a semaphore
341
342**CALLING SEQUENCE:**
343
344.. code-block:: c
345
346    int sem_wait(
347        sem_t *sem
348    );
349
350**STATUS CODES:**
351
352.. list-table::
353 :class: rtems-table
354
355 * - ``EINVAL``
356   - The ``sem`` argument does not refer to a valid semaphore
357
358**DESCRIPTION:**
359
360This function attempts to lock a semaphore specified by ``sem``. If the
361semaphore is available, then the semaphore is locked (i.e., the semaphore
362value is decremented). If the semaphore is unavailable (i.e., the semaphore
363value is zero), then the function will block until the semaphore becomes
364available. It will then successfully lock the semaphore. The semaphore
365remains locked until released by a ``sem_post()`` call.
366
367If the call is unsuccessful, then the function returns -1 and sets ``errno`` to
368the appropriate error code.
369
370**NOTES:**
371
372Multiprocessing is not supported in this implementation.
373
374.. _sem_trywait:
375
376sem_trywait - Non-blocking Wait on a Semaphore
377----------------------------------------------
378.. index:: sem_trywait
379.. index:: non
380
381**CALLING SEQUENCE:**
382
383.. code-block:: c
384
385    int sem_trywait(
386        sem_t *sem
387    );
388
389**STATUS CODES:**
390
391.. list-table::
392 :class: rtems-table
393
394 * - ``EAGAIN``
395   - The semaphore is not available (i.e., the semaphore value is zero), so the
396     semaphore could not be locked.
397 * - ``EINVAL``
398   - The ``sem`` argument does not refewr to a valid semaphore
399
400**DESCRIPTION:**
401
402This function attempts to lock a semaphore specified by ``sem``. If the
403semaphore is available, then the semaphore is locked (i.e., the semaphore value
404is decremented) and the function returns a value of 0. The semaphore remains
405locked until released by a ``sem_post()`` call. If the semaphore is unavailable
406(i.e., the semaphore value is zero), then the function will return a value
407of -1 immediately and set ``errno`` to ``EAGAIN``.
408
409If the call is unsuccessful, then the function returns -1 and sets ``errno`` to
410the appropriate error code.
411
412**NOTES:**
413
414Multiprocessing is not supported in this implementation.
415
416.. _sem_timedwait:
417
418sem_timedwait - Wait on a Semaphore for a Specified Time
419--------------------------------------------------------
420.. index:: sem_timedwait
421.. index:: wait on a semaphore for a specified time
422
423**CALLING SEQUENCE:**
424
425.. code-block:: c
426
427    int sem_timedwait(
428        sem_t                 *sem,
429        const struct timespec *abstime
430    );
431
432**STATUS CODES:**
433
434.. list-table::
435 :class: rtems-table
436
437 * - ``EINVAL``
438   - The ``sem`` argument does not refewr to a valid semaphore
439 * - ``EINVAL``
440   - The nanoseconds field of timeout is invalid.
441 * - ``ETIMEDOUT``
442   - The calling thread was unable to get the semaphore within the specified
443     timeout period.
444
445**DESCRIPTION:**
446
447This function attemtps to lock a semaphore specified by ``sem``, and will wait
448for the semaphore until the absolute time specified by ``abstime``. If the
449semaphore is available, then the semaphore is locked (i.e., the semaphore value
450is decremented) and the function returns a value of 0. The semaphore remains
451locked until released by a ``sem_post()`` call. If the semaphore is
452unavailable, then the function will wait for the semaphore to become available
453for the amount of time specified by ``timeout``.
454
455If the semaphore does not become available within the interval specified by
456``timeout``, then the function returns -1 and sets ``errno`` to ``EAGAIN``.  If
457any other error occurs, the function returns -1 and sets ``errno`` to the
458appropriate error code.
459
460**NOTES:**
461
462Multiprocessing is not supported in this implementation.
463
464.. _sem_post:
465
466sem_post - Unlock a Semaphore
467-----------------------------
468.. index:: sem_post
469.. index:: unlock a semaphore
470
471**CALLING SEQUENCE:**
472
473.. code-block:: c
474
475    int sem_post(
476        sem_t *sem
477    );
478
479**STATUS CODES:**
480
481.. list-table::
482 :class: rtems-table
483
484 * - ``EINVAL``
485   - The ``sem`` argument does not refer to a valid semaphore
486
487**DESCRIPTION:**
488
489This function attempts to release the semaphore specified by ``sem``. If other
490tasks are waiting on the semaphore, then one of those tasks (which one depends
491on the scheduler being used) is allowed to lock the semaphore and return from
492its ``sem_wait()``, ``sem_trywait()``, or ``sem_timedwait()`` call. If there
493are no other tasks waiting on the semaphore, then the semaphore value is simply
494incremented. ``sem_post()`` returns 0 upon successful completion.
495
496If an error occurs, the function returns -1 and sets ``errno`` to the
497appropriate error code.
498
499**NOTES:**
500
501Multiprocessing is not supported in this implementation.
502
503.. _sem_getvalue:
504
505sem_getvalue - Get the value of a semaphore
506-------------------------------------------
507.. index:: sem_getvalue
508.. index:: get the value of a semaphore
509
510**CALLING SEQUENCE:**
511
512.. code-block:: c
513
514    int sem_getvalue(
515        sem_t *sem,
516        int   *sval
517    );
518
519**STATUS CODES:**
520
521.. list-table::
522 :class: rtems-table
523
524 * - ``EINVAL``
525   - The ``sem`` argument does not refer to a valid semaphore
526 * - ``ENOSYS``
527   - The function ``sem_getvalue`` is not supported by this implementation
528
529**DESCRIPTION:**
530
531The ``sem_getvalue`` functions sets the location referenced by the ``sval``
532argument to the value of the semaphore without affecting the state of the
533semaphore. The updated value represents a semaphore value that occurred at some
534point during the call, but is not necessarily the actual value of the semaphore
535when it returns to the calling process.
536
537If ``sem`` is locked, the value returned by ``sem_getvalue`` will be zero or a
538negative number whose absolute value is the number of processes waiting for the
539semaphore at some point during the call.
540
541**NOTES:**
542
543If the functions completes successfully, it shall return a value of zero.
544Otherwise, it shall return a value of -1 and set ``errno`` to specify the error
545that occurred.
Note: See TracBrowser for help on using the repository browser.