source: rtems-docs/c-user/semaphore/directives.rst @ 241cd06

Last change on this file since 241cd06 was 241cd06, checked in by Sebastian Huber <sebastian.huber@…>, on 04/07/21 at 05:11:46

c-user: Fix rtems_semaphore_release() docs

The directive returns RTEMS_UNSATISFIED if the semaphore's count already
had the maximum value of UINT32_MAX.

  • Property mode set to 100644
File size: 33.5 KB
Line 
1.. SPDX-License-Identifier: CC-BY-SA-4.0
2
3.. Copyright (C) 2020, 2021 embedded brains GmbH (http://www.embedded-brains.de)
4.. Copyright (C) 1988, 2008 On-Line Applications Research Corporation (OAR)
5
6.. This file is part of the RTEMS quality process and was automatically
7.. generated.  If you find something that needs to be fixed or
8.. worded better please post a report or patch to an RTEMS mailing list
9.. or raise a bug report:
10..
11.. https://www.rtems.org/bugs.html
12..
13.. For information on updating and regenerating please refer to the How-To
14.. section in the Software Requirements Engineering chapter of the
15.. RTEMS Software Engineering manual.  The manual is provided as a part of
16.. a release.  For development sources please refer to the online
17.. documentation at:
18..
19.. https://docs.rtems.org
20
21.. _SemaphoreManagerDirectives:
22
23Directives
24==========
25
26This section details the directives of the Semaphore Manager. A subsection is
27dedicated to each of this manager's directives and lists the calling sequence,
28parameters, description, return values, and notes of the directive.
29
30.. Generated from spec:/rtems/sem/if/create
31
32.. raw:: latex
33
34    \clearpage
35
36.. index:: rtems_semaphore_create()
37.. index:: create a semaphore
38
39.. _InterfaceRtemsSemaphoreCreate:
40
41rtems_semaphore_create()
42------------------------
43
44Creates a semaphore.
45
46.. rubric:: CALLING SEQUENCE:
47
48.. code-block:: c
49
50    rtems_status_code rtems_semaphore_create(
51      rtems_name          name,
52      uint32_t            count,
53      rtems_attribute     attribute_set,
54      rtems_task_priority priority_ceiling,
55      rtems_id           *id
56    );
57
58.. rubric:: PARAMETERS:
59
60``name``
61    This parameter is the object name of the semaphore.
62
63``count``
64    This parameter is the initial count of the semaphore.  If the semaphore is
65    a binary semaphore, then a count of 0 will make the calling task the owner
66    of the binary semaphore and a count of 1 will create a binary semaphore
67    without an owner.
68
69``attribute_set``
70    This parameter is the attribute set of the semaphore.
71
72``priority_ceiling``
73    This parameter is the priority ceiling if the semaphore is a binary
74    semaphore with the priority ceiling or MrsP locking protocol as defined by
75    the attribute set.
76
77``id``
78    This parameter is the pointer to an object identifier variable.  When the
79    directive call is successful, the identifier of the created semaphore will
80    be stored in this variable.
81
82.. rubric:: DESCRIPTION:
83
84This directive creates a semaphore which resides on the local node.  The
85semaphore has the user-defined object name specified in ``name`` and the
86initial count specified in ``count``.  The assigned object identifier is
87returned in ``id``.  This identifier is used to access the semaphore with other
88semaphore related directives.
89
90The **attribute set** specified in ``attribute_set`` is built through a
91*bitwise or* of the attribute constants described below.  Not all combinations
92of attributes are allowed.  Some attributes are mutually exclusive.  If
93mutually exclusive attributes are combined, the behaviour is undefined.
94Attributes not mentioned below are not evaluated by this directive and have no
95effect.  Default attributes can be selected by using the
96:c:macro:`RTEMS_DEFAULT_ATTRIBUTES` constant.  The attribute set defines
97
98* the scope of the semaphore: :c:macro:`RTEMS_LOCAL` (default) or
99  :c:macro:`RTEMS_GLOBAL`,
100
101* the task wait queue discipline used by the semaphore: :c:macro:`RTEMS_FIFO`
102  (default) or :c:macro:`RTEMS_PRIORITY`,
103
104* the class of the semaphore: :c:macro:`RTEMS_COUNTING_SEMAPHORE` (default),
105  :c:macro:`RTEMS_BINARY_SEMAPHORE`, or
106  :c:macro:`RTEMS_SIMPLE_BINARY_SEMAPHORE`, and
107
108* the locking protocol of a binary semaphore: no locking protocol (default),
109  :c:macro:`RTEMS_INHERIT_PRIORITY`, :c:macro:`RTEMS_PRIORITY_CEILING`, or
110  :c:macro:`RTEMS_MULTIPROCESSOR_RESOURCE_SHARING`.
111
112The semaphore has a local or global **scope** in a multiprocessing network
113(this attribute does not refer to SMP systems).  The scope is selected by the
114mutually exclusive :c:macro:`RTEMS_LOCAL` and :c:macro:`RTEMS_GLOBAL`
115attributes.
116
117* A **local scope** is the default and can be emphasized through the use of the
118  :c:macro:`RTEMS_LOCAL` attribute.  A local semaphore can be only used by the
119  node which created it.
120
121* A **global scope** is established if the :c:macro:`RTEMS_GLOBAL` attribute is
122  set.  Setting the global attribute in a single node system has no effect.
123
124The **task wait queue discipline** is selected by the mutually exclusive
125:c:macro:`RTEMS_FIFO` and :c:macro:`RTEMS_PRIORITY` attributes.
126
127* The **FIFO discipline** is the default and can be emphasized through use of
128  the :c:macro:`RTEMS_FIFO` attribute.
129
130* The **priority discipline** is selected by the :c:macro:`RTEMS_PRIORITY`
131  attribute.  The locking protocols require the priority discipline.
132
133The **semaphore class** is selected by the mutually exclusive
134:c:macro:`RTEMS_COUNTING_SEMAPHORE`, :c:macro:`RTEMS_BINARY_SEMAPHORE`, and
135:c:macro:`RTEMS_SIMPLE_BINARY_SEMAPHORE` attributes.
136
137* The **counting semaphore class** is the default and can be emphasized through
138  use of the :c:macro:`RTEMS_COUNTING_SEMAPHORE` attribute.
139
140* The **binary semaphore class** is selected by the
141  :c:macro:`RTEMS_BINARY_SEMAPHORE` attribute.  Binary semaphores are mutual
142  exclusion (mutex) synchronization primitives which may have an owner.  The
143  count of a binary semaphore is restricted to 0 and 1 values.
144
145* The **simple binary semaphore class** is selected by the
146  :c:macro:`RTEMS_SIMPLE_BINARY_SEMAPHORE` attribute.  Simple binary semaphores
147  have no owner.  They may be used for task and interrupt synchronization.  The
148  count of a simple binary semaphore is restricted to 0 and 1 values.
149
150Binary semaphores may use a **locking protocol**.  If a locking protocol is
151selected, then the scope shall be local and the priority task wait queue
152discipline shall be selected.  The locking protocol is selected by the mutually
153exclusive :c:macro:`RTEMS_INHERIT_PRIORITY`, :c:macro:`RTEMS_PRIORITY_CEILING`,
154and :c:macro:`RTEMS_MULTIPROCESSOR_RESOURCE_SHARING` attributes.
155
156* The default is **no locking protocol**.  This can be emphasized through use
157  of the :c:macro:`RTEMS_NO_INHERIT_PRIORITY`,
158  :c:macro:`RTEMS_NO_MULTIPROCESSOR_RESOURCE_SHARING`, and
159  :c:macro:`RTEMS_NO_PRIORITY_CEILING` attributes.
160
161* The **priority inheritance locking protocol** is selected by the
162  :c:macro:`RTEMS_INHERIT_PRIORITY` attribute.
163
164* The **priority ceiling locking protocol** is selected by the
165  :c:macro:`RTEMS_PRIORITY_CEILING` attribute.  For this locking protocol a
166  priority ceiling shall be specified in ``priority_ceiling``.
167
168* The **MrsP locking protocol** is selected by the
169  :c:macro:`RTEMS_MULTIPROCESSOR_RESOURCE_SHARING` attribute in SMP
170  configurations, otherwise this attribute selects the **priority ceiling
171  locking protocol**.  For these locking protocols a priority ceiling shall be
172  specified in ``priority_ceiling``.  This priority is used to set the priority
173  ceiling for all schedulers.  This can be changed later with the
174  :ref:`InterfaceRtemsSemaphoreSetPriority` directive using the returned object
175  identifier.
176
177.. rubric:: RETURN VALUES:
178
179:c:macro:`RTEMS_SUCCESSFUL`
180    The requested operation was successful.
181
182:c:macro:`RTEMS_INVALID_NAME`
183    The ``name`` parameter was invalid.
184
185:c:macro:`RTEMS_INVALID_ADDRESS`
186    The ``id`` parameter was `NULL
187    <https://en.cppreference.com/w/c/types/NULL>`_.
188
189:c:macro:`RTEMS_INVALID_NUMBER`
190    The ``count`` parameter was invalid.
191
192:c:macro:`RTEMS_NOT_DEFINED`
193    The ``attribute_set`` parameter was invalid.
194
195:c:macro:`RTEMS_TOO_MANY`
196    There was no inactive object available to create a semaphore.  The number
197    of semaphores available to the application is configured through the
198    :ref:`CONFIGURE_MAXIMUM_SEMAPHORES` application configuration option.
199
200:c:macro:`RTEMS_TOO_MANY`
201    In multiprocessing configurations, there was no inactive global object
202    available to create a global semaphore.  The number of global objects
203    available to the application is configured through the
204    :ref:`CONFIGURE_MP_MAXIMUM_GLOBAL_OBJECTS` application configuration
205    option.
206
207:c:macro:`RTEMS_INVALID_PRIORITY`
208    The ``priority_ceiling`` parameter was invalid.
209
210.. rubric:: NOTES:
211
212For control and maintenance of the semaphore, RTEMS allocates a :term:`SMCB`
213from the local SMCB free pool and initializes it.
214
215The SMCB for a global semaphore is allocated on the local node.  Semaphores
216should not be made global unless remote tasks must interact with the semaphore.
217This is to avoid the system overhead incurred by the creation of a global
218semaphore.  When a global semaphore is created, the semaphore's name and
219identifier must be transmitted to every node in the system for insertion in the
220local copy of the global object table.
221
222.. rubric:: CONSTRAINTS:
223
224The following constraints apply to this directive:
225
226* The directive may be called from within device driver initialization context.
227
228* The directive may be called from within task context.
229
230* The directive may obtain and release the object allocator mutex.  This may
231  cause the calling task to be preempted.
232
233* When the directive operates on a global object, the directive sends a message
234  to remote nodes.  This may preempt the calling task.
235
236* The number of semaphores available to the application is configured through
237  the :ref:`CONFIGURE_MAXIMUM_SEMAPHORES` application configuration option.
238
239* Where the object class corresponding to the directive is configured to use
240  unlimited objects, the directive may allocate memory from the RTEMS
241  Workspace.
242
243* The number of global objects available to the application is configured
244  through the :ref:`CONFIGURE_MP_MAXIMUM_GLOBAL_OBJECTS` application
245  configuration option.
246
247.. Generated from spec:/rtems/sem/if/ident
248
249.. raw:: latex
250
251    \clearpage
252
253.. index:: rtems_semaphore_ident()
254
255.. _InterfaceRtemsSemaphoreIdent:
256
257rtems_semaphore_ident()
258-----------------------
259
260Identifies a semaphore by the object name.
261
262.. rubric:: CALLING SEQUENCE:
263
264.. code-block:: c
265
266    rtems_status_code rtems_semaphore_ident(
267      rtems_name name,
268      uint32_t   node,
269      rtems_id  *id
270    );
271
272.. rubric:: PARAMETERS:
273
274``name``
275    This parameter is the object name to look up.
276
277``node``
278    This parameter is the node or node set to search for a matching object.
279
280``id``
281    This parameter is the pointer to an object identifier variable.  When the
282    directive call is successful, the object identifier of an object with the
283    specified name will be stored in this variable.
284
285.. rubric:: DESCRIPTION:
286
287This directive obtains a semaphore identifier associated with the semaphore
288name specified in ``name``.
289
290The node to search is specified in ``node``.  It shall be
291
292* a valid node number,
293
294* the constant :c:macro:`RTEMS_SEARCH_ALL_NODES` to search in all nodes,
295
296* the constant :c:macro:`RTEMS_SEARCH_LOCAL_NODE` to search in the local node
297  only, or
298
299* the constant :c:macro:`RTEMS_SEARCH_OTHER_NODES` to search in all nodes
300  except the local node.
301
302.. rubric:: RETURN VALUES:
303
304:c:macro:`RTEMS_SUCCESSFUL`
305    The requested operation was successful.
306
307:c:macro:`RTEMS_INVALID_ADDRESS`
308    The ``id`` parameter was `NULL
309    <https://en.cppreference.com/w/c/types/NULL>`_.
310
311:c:macro:`RTEMS_INVALID_NAME`
312    The ``name`` parameter was 0.
313
314:c:macro:`RTEMS_INVALID_NAME`
315    There was no object with the specified name on the specified nodes.
316
317:c:macro:`RTEMS_INVALID_NODE`
318    In multiprocessing configurations, the specified node was invalid.
319
320.. rubric:: NOTES:
321
322If the semaphore name is not unique, then the semaphore identifier will match
323the first semaphore with that name in the search order.  However, this
324semaphore identifier is not guaranteed to correspond to the desired semaphore.
325
326The objects are searched from lowest to the highest index.  If ``node`` is
327:c:macro:`RTEMS_SEARCH_ALL_NODES`, all nodes are searched with the local node
328being searched first.  All other nodes are searched from lowest to the highest
329node number.
330
331If node is a valid node number which does not represent the local node, then
332only the semaphores exported by the designated node are searched.
333
334This directive does not generate activity on remote nodes.  It accesses only
335the local copy of the global object table.
336
337The semaphore identifier is used with other semaphore related directives to
338access the semaphore.
339
340.. rubric:: CONSTRAINTS:
341
342The following constraints apply to this directive:
343
344* The directive may be called from within any runtime context.
345
346* The directive will not cause the calling task to be preempted.
347
348.. Generated from spec:/rtems/sem/if/delete
349
350.. raw:: latex
351
352    \clearpage
353
354.. index:: rtems_semaphore_delete()
355.. index:: delete a semaphore
356
357.. _InterfaceRtemsSemaphoreDelete:
358
359rtems_semaphore_delete()
360------------------------
361
362Deletes the semaphore.
363
364.. rubric:: CALLING SEQUENCE:
365
366.. code-block:: c
367
368    rtems_status_code rtems_semaphore_delete( rtems_id id );
369
370.. rubric:: PARAMETERS:
371
372``id``
373    This parameter is the semaphore identifier.
374
375.. rubric:: DESCRIPTION:
376
377This directive deletes the semaphore specified by ``id``.
378
379.. rubric:: RETURN VALUES:
380
381:c:macro:`RTEMS_SUCCESSFUL`
382    The requested operation was successful.
383
384:c:macro:`RTEMS_INVALID_ID`
385    There was no semaphore associated with the identifier specified by ``id``.
386
387:c:macro:`RTEMS_ILLEGAL_ON_REMOTE_OBJECT`
388    The semaphore resided on a remote node.
389
390:c:macro:`RTEMS_RESOURCE_IN_USE`
391    The binary semaphore had an owner.
392
393.. rubric:: NOTES:
394
395Binary semaphores with an owner cannot be deleted.
396
397When a semaphore is deleted, all tasks blocked waiting to obtain the semaphore
398will be readied and returned a status code which indicates that the semaphore
399was deleted.
400
401The :term:`SMCB` for the deleted semaphore is reclaimed by RTEMS.
402
403When a global semaphore is deleted, the semaphore identifier must be
404transmitted to every node in the system for deletion from the local copy of the
405global object table.
406
407The semaphore must reside on the local node, even if the semaphore was created
408with the :c:macro:`RTEMS_GLOBAL` attribute.
409
410Proxies, used to represent remote tasks, are reclaimed when the semaphore is
411deleted.
412
413.. rubric:: CONSTRAINTS:
414
415The following constraints apply to this directive:
416
417* The directive may be called from within device driver initialization context.
418
419* The directive may be called from within task context.
420
421* The directive may obtain and release the object allocator mutex.  This may
422  cause the calling task to be preempted.
423
424* When the directive operates on a global object, the directive sends a message
425  to remote nodes.  This may preempt the calling task.
426
427* The calling task does not have to be the task that created the object.  Any
428  local task that knows the object identifier can delete the object.
429
430* Where the object class corresponding to the directive is configured to use
431  unlimited objects, the directive may free memory to the RTEMS Workspace.
432
433.. Generated from spec:/rtems/sem/if/obtain
434
435.. raw:: latex
436
437    \clearpage
438
439.. index:: rtems_semaphore_obtain()
440.. index:: obtain a semaphore
441.. index:: lock a semaphore
442
443.. _InterfaceRtemsSemaphoreObtain:
444
445rtems_semaphore_obtain()
446------------------------
447
448Obtains the semaphore.
449
450.. rubric:: CALLING SEQUENCE:
451
452.. code-block:: c
453
454    rtems_status_code rtems_semaphore_obtain(
455      rtems_id       id,
456      rtems_option   option_set,
457      rtems_interval timeout
458    );
459
460.. rubric:: PARAMETERS:
461
462``id``
463    This parameter is the semaphore identifier.
464
465``option_set``
466    This parameter is the option set.
467
468``timeout``
469    This parameter is the timeout in clock ticks if the :c:macro:`RTEMS_WAIT`
470    option is set.  Use :c:macro:`RTEMS_NO_TIMEOUT` to wait potentially
471    forever.
472
473.. rubric:: DESCRIPTION:
474
475This directive obtains the semaphore specified by ``id``.
476
477The **option set** specified in ``option_set`` is built through a *bitwise or*
478of the option constants described below.  Not all combinations of options are
479allowed.  Some options are mutually exclusive.  If mutually exclusive options
480are combined, the behaviour is undefined.  Options not mentioned below are not
481evaluated by this directive and have no effect. Default options can be selected
482by using the :c:macro:`RTEMS_DEFAULT_OPTIONS` constant.
483
484The calling task can **wait** or **try to obtain** the semaphore according to
485the mutually exclusive :c:macro:`RTEMS_WAIT` and :c:macro:`RTEMS_NO_WAIT`
486options.
487
488* **Waiting to obtain** the semaphore is the default and can be emphasized
489  through the use of the :c:macro:`RTEMS_WAIT` option.  The ``timeout``
490  parameter defines how long the calling task is willing to wait.  Use
491  :c:macro:`RTEMS_NO_TIMEOUT` to wait potentially forever, otherwise set a
492  timeout interval in clock ticks.
493
494* **Trying to obtain** the semaphore is selected by the
495  :c:macro:`RTEMS_NO_WAIT` option.  If this option is defined, then the
496  ``timeout`` parameter is ignored.  When the semaphore cannot be immediately
497  obtained, then the :c:macro:`RTEMS_UNSATISFIED` status is returned.
498
499With either :c:macro:`RTEMS_WAIT` or :c:macro:`RTEMS_NO_WAIT` if the current
500semaphore count is positive, then it is decremented by one and the semaphore is
501successfully obtained by returning immediately with the
502:c:macro:`RTEMS_SUCCESSFUL` status code.
503
504If the calling task chooses to return immediately and the current semaphore
505count is zero, then the :c:macro:`RTEMS_UNSATISFIED` status code is returned
506indicating that the semaphore is not available.
507
508If the calling task chooses to wait for a semaphore and the current semaphore
509count is zero, then the calling task is placed on the semaphore's wait queue
510and blocked.  If a local, binary semaphore was created with the
511:c:macro:`RTEMS_INHERIT_PRIORITY` attribute, then the priority of the task
512currently holding the binary semaphore will inherit the current priority set of
513the blocking task.  The priority inheritance is carried out recursively.  This
514means, that if the task currently holding the binary semaphore is blocked on
515another local, binary semaphore using the priority inheritance locking
516protocol, then the owner of this semaphore will inherit the current priority
517sets of both tasks, and so on.  A task has a current priority for each
518scheduler.
519
520.. rubric:: RETURN VALUES:
521
522:c:macro:`RTEMS_SUCCESSFUL`
523    The requested operation was successful.
524
525:c:macro:`RTEMS_INVALID_ID`
526    There was no semaphore associated with the identifier specified by ``id``.
527
528:c:macro:`RTEMS_UNSATISFIED`
529    The semaphore could not be obtained immediately.
530
531:c:macro:`RTEMS_INCORRECT_STATE`
532    Acquiring of the local, binary semaphore by the calling task would have
533    cased a deadlock.
534
535:c:macro:`RTEMS_INCORRECT_STATE`
536    The calling task attempted to recursively obtain a local, binary semaphore
537    using the MrsP locking protocol.
538
539:c:macro:`RTEMS_UNSATISFIED`
540    The semaphore was flushed while the calling task was waiting to obtain the
541    semaphore.
542
543:c:macro:`RTEMS_TIMEOUT`
544    The timeout happened while the calling task was waiting to obtain the
545    semaphore.
546
547:c:macro:`RTEMS_OBJECT_WAS_DELETED`
548    The semaphore was deleted while the calling task was waiting to obtain the
549    semaphore.
550
551.. rubric:: NOTES:
552
553If a local, binary semaphore was created with the
554:c:macro:`RTEMS_PRIORITY_CEILING` or
555:c:macro:`RTEMS_MULTIPROCESSOR_RESOURCE_SHARING` attribute, a task successfully
556obtains the semaphore, and the priority of that task is greater than the
557ceiling priority for this semaphore, then the priority of the task acquiring
558the semaphore is elevated to that of the ceiling.
559
560Deadlock situations are detected for local, binary semaphores.  If a deadlock
561is detected, then the directive immediately returns the
562:c:macro:`RTEMS_INCORRECT_STATE` status code.
563
564It is not allowed to recursively obtain (nested access) a local, binary
565semaphore using the MrsP locking protocol and any attempt to do this will just
566return the :c:macro:`RTEMS_INCORRECT_STATE` status code.  This error can only
567happen in SMP configurations.
568
569If the semaphore was created with the :c:macro:`RTEMS_PRIORITY` attribute, then
570the calling task is inserted into the wait queue according to its priority.
571However, if the semaphore was created with the :c:macro:`RTEMS_FIFO` attribute,
572then the calling task is placed at the rear of the wait queue.
573
574Attempting to obtain a global semaphore which does not reside on the local node
575will generate a request to the remote node to access the semaphore.  If the
576semaphore is not available and :c:macro:`RTEMS_NO_WAIT` was not specified, then
577the task must be blocked until the semaphore is released.  A proxy is allocated
578on the remote node to represent the task until the semaphore is released.
579
580.. rubric:: CONSTRAINTS:
581
582The following constraints apply to this directive:
583
584* When a local, counting semaphore or a local, simple binary semaphore is
585  accessed and the :c:macro:`RTEMS_NO_WAIT` option is set, the directive may be
586  called from within interrupt context.
587
588* When a local semaphore is accessed and the request can be immediately
589  satisfied, the directive may be called from within device driver
590  initialization context.
591
592* The directive may be called from within task context.
593
594* When the request cannot be immediately satisfied and the
595  :c:macro:`RTEMS_WAIT` option is set, the calling task blocks at some point
596  during the directive call.
597
598* The timeout functionality of the directive requires a :term:`clock tick`.
599
600* When the directive operates on a remote object, the directive sends a message
601  to the remote node and waits for a reply.  This will preempt the calling
602  task.
603
604.. Generated from spec:/rtems/sem/if/release
605
606.. raw:: latex
607
608    \clearpage
609
610.. index:: rtems_semaphore_release()
611.. index:: release a semaphore
612.. index:: unlock a semaphore
613
614.. _InterfaceRtemsSemaphoreRelease:
615
616rtems_semaphore_release()
617-------------------------
618
619Releases the semaphore.
620
621.. rubric:: CALLING SEQUENCE:
622
623.. code-block:: c
624
625    rtems_status_code rtems_semaphore_release( rtems_id id );
626
627.. rubric:: PARAMETERS:
628
629``id``
630    This parameter is the semaphore identifier.
631
632.. rubric:: DESCRIPTION:
633
634This directive releases the semaphore specified by ``id``.  If the semaphore's
635wait queue is not empty, then
636
637* the first task on the wait queue is removed and unblocked, the semaphore's
638  count is not changed, otherwise
639
640* the semaphore's count is incremented by one for counting semaphores and set
641  to one for binary and simple binary semaphores.
642
643.. rubric:: RETURN VALUES:
644
645:c:macro:`RTEMS_SUCCESSFUL`
646    The requested operation was successful.
647
648:c:macro:`RTEMS_INVALID_ID`
649    There was no semaphore associated with the identifier specified by ``id``.
650
651:c:macro:`RTEMS_NOT_OWNER_OF_RESOURCE`
652    The calling task was not the owner of the semaphore.
653
654:c:macro:`RTEMS_UNSATISFIED`
655    The semaphore's count already had the maximum value of `UINT32_MAX
656    <https://en.cppreference.com/w/c/types/integer>`_.
657
658.. rubric:: NOTES:
659
660The calling task may be preempted if it causes a higher priority task to be
661made ready for execution.
662
663The outermost release of a local, binary semaphore using the priority
664inheritance, priority ceiling, or MrsP locking protocol may result in the
665calling task having its priority lowered.  This will occur if the highest
666priority of the calling task was available due to the ownership of the released
667semaphore.  If a task was on the semaphore's wait queue, then the priority
668associated with the semaphore will be transferred to the new owner.
669
670Releasing a global semaphore which does not reside on the local node will
671generate a request telling the remote node to release the semaphore.
672
673If the task to be unblocked resides on a different node from the semaphore,
674then the semaphore allocation is forwarded to the appropriate node, the waiting
675task is unblocked, and the proxy used to represent the task is reclaimed.
676
677.. rubric:: CONSTRAINTS:
678
679The following constraints apply to this directive:
680
681* When a local, counting semaphore or a local, simple binary semaphore is
682  accessed, the directive may be called from within interrupt context.
683
684* When a local semaphore is accessed, the directive may be called from within
685  device driver initialization context.
686
687* The directive may be called from within task context.
688
689* The directive may unblock another task which may preempt the calling task.
690
691* When the directive operates on a remote object, the directive sends a message
692  to the remote node and waits for a reply.  This will preempt the calling
693  task.
694
695.. Generated from spec:/rtems/sem/if/flush
696
697.. raw:: latex
698
699    \clearpage
700
701.. index:: rtems_semaphore_flush()
702.. index:: flush a semaphore
703.. index:: unblock all tasks waiting on a semaphore
704
705.. _InterfaceRtemsSemaphoreFlush:
706
707rtems_semaphore_flush()
708-----------------------
709
710Flushes the semaphore.
711
712.. rubric:: CALLING SEQUENCE:
713
714.. code-block:: c
715
716    rtems_status_code rtems_semaphore_flush( rtems_id id );
717
718.. rubric:: PARAMETERS:
719
720``id``
721    This parameter is the semaphore identifier.
722
723.. rubric:: DESCRIPTION:
724
725This directive unblocks all tasks waiting on the semaphore specified by ``id``.
726The semaphore's count is not changed by this directive.  Tasks which are
727unblocked as the result of this directive will return from the
728:ref:`InterfaceRtemsSemaphoreObtain` directive with a status code of
729:c:macro:`RTEMS_UNSATISFIED` to indicate that the semaphore was not obtained.
730
731.. rubric:: RETURN VALUES:
732
733:c:macro:`RTEMS_SUCCESSFUL`
734    The requested operation was successful.
735
736:c:macro:`RTEMS_INVALID_ID`
737    There was no semaphore associated with the identifier specified by ``id``.
738
739:c:macro:`RTEMS_ILLEGAL_ON_REMOTE_OBJECT`
740    The semaphore resided on a remote node.
741
742:c:macro:`RTEMS_NOT_DEFINED`
743    Flushing a semaphore using the MrsP locking protocol is undefined
744    behaviour.
745
746.. rubric:: NOTES:
747
748If the task to be unblocked resides on a different node from the semaphore,
749then the waiting task is unblocked, and the proxy used to represent the task is
750reclaimed.
751
752It is not allowed to flush a local, binary semaphore using the MrsP locking
753protocol and any attempt to do this will just return the
754:c:macro:`RTEMS_NOT_DEFINED` status code.  This error can only happen in SMP
755configurations.
756
757For barrier synchronization, the :ref:`RTEMSAPIClassicBarrier` offers a cleaner
758alternative to using the semaphore flush directive.  Unlike POSIX barriers,
759they have a manual release option.
760
761Using the semaphore flush directive for condition synchronization in concert
762with another semaphore may be subject to the lost wake-up problem.  The
763following attempt to implement a condition variable is broken.
764
765.. code-block:: c
766    :linenos:
767
768    #include <rtems.h>
769    #include <assert.h>
770
771    void cnd_wait( rtems_id cnd, rtems_id mtx )
772    {
773      rtems_status_code sc;
774
775      sc = rtems_semaphore_release( mtx );
776      assert( sc == RTEMS_SUCCESSFUL );
777
778      // Here, a higher priority task may run and satisfy the condition.
779      // We may never wake up from the next semaphore obtain.
780
781      sc = rtems_semaphore_obtain( cnd, RTEMS_WAIT, RTEMS_NO_TIMEOUT );
782      assert( sc == RTEMS_UNSATISFIED );
783
784      sc = rtems_semaphore_obtain( mtx, RTEMS_WAIT, RTEMS_NO_TIMEOUT );
785      assert( sc == RTEMS_SUCCESSFUL );
786    }
787
788    void cnd_broadcast( rtems_id cnd )
789    {
790      rtems_status_code sc;
791
792      sc = rtems_semaphore_flush( cnd );
793      assert( sc == RTEMS_SUCCESSFUL );
794    }
795
796.. rubric:: CONSTRAINTS:
797
798The following constraints apply to this directive:
799
800* When a local, counting semaphore or a local, simple binary semaphore is
801  accessed, the directive may be called from within interrupt context.
802
803* When a local semaphore is accessed, the directive may be called from within
804  device driver initialization context.
805
806* The directive may be called from within task context.
807
808* The directive may unblock another task which may preempt the calling task.
809
810* When the directive operates on a remote object, the directive sends a message
811  to the remote node and waits for a reply.  This will preempt the calling
812  task.
813
814.. Generated from spec:/rtems/sem/if/set-priority
815
816.. raw:: latex
817
818    \clearpage
819
820.. index:: rtems_semaphore_set_priority()
821.. index:: set priority by scheduler for a semaphore
822
823.. _InterfaceRtemsSemaphoreSetPriority:
824
825rtems_semaphore_set_priority()
826------------------------------
827
828Sets the priority by scheduler for the semaphore.
829
830.. rubric:: CALLING SEQUENCE:
831
832.. code-block:: c
833
834    rtems_status_code rtems_semaphore_set_priority(
835      rtems_id             semaphore_id,
836      rtems_id             scheduler_id,
837      rtems_task_priority  new_priority,
838      rtems_task_priority *old_priority
839    );
840
841.. rubric:: PARAMETERS:
842
843``semaphore_id``
844    This parameter is the semaphore identifier.
845
846``scheduler_id``
847    This parameter is the identifier of the scheduler corresponding to the new
848    priority.
849
850``new_priority``
851    This parameter is the new priority corresponding to the specified
852    scheduler.
853
854``old_priority``
855    This parameter is the pointer to a task priority variable.  When the
856    directive call is successful, the old priority of the semaphore
857    corresponding to the specified scheduler will be stored in this variable.
858
859.. rubric:: DESCRIPTION:
860
861This directive sets the priority of the semaphore specified by
862``semaphore_id``.  The priority corresponds to the scheduler specified by
863``scheduler_id``.
864
865The special priority value :c:macro:`RTEMS_CURRENT_PRIORITY` can be used to get
866the current priority without changing it.
867
868The availability and use of a priority depends on the class and locking
869protocol of the semaphore:
870
871* For local, binary semaphores using the MrsP locking protocol, the ceiling
872  priority for each scheduler can be set by this directive.
873
874* For local, binary semaphores using the priority ceiling protocol, the ceiling
875  priority can be set by this directive.
876
877* For other semaphore classes and locking protocols, setting a priority is
878  undefined behaviour.
879
880.. rubric:: RETURN VALUES:
881
882:c:macro:`RTEMS_SUCCESSFUL`
883    The requested operation was successful.
884
885:c:macro:`RTEMS_INVALID_ADDRESS`
886    The ``old_priority`` parameter was `NULL
887    <https://en.cppreference.com/w/c/types/NULL>`_.
888
889:c:macro:`RTEMS_INVALID_ID`
890    There was no scheduler associated with the identifier specified by
891    ``scheduler_id``.
892
893:c:macro:`RTEMS_INVALID_ID`
894    There was no semaphore associated with the identifier specified by
895    ``semaphore_id``.
896
897:c:macro:`RTEMS_ILLEGAL_ON_REMOTE_OBJECT`
898    The semaphore resided on a remote node.
899
900:c:macro:`RTEMS_INVALID_PRIORITY`
901    The ``new_priority`` parameter was invalid.
902
903:c:macro:`RTEMS_NOT_DEFINED`
904    Setting a priority for the class or locking protocol of the semaphore is
905    undefined behaviour.
906
907.. rubric:: NOTES:
908
909Please have a look at the following example:
910
911.. code-block:: c
912    :linenos:
913
914    #include <assert.h>
915    #include <rtems.h>
916
917    #define SCHED_A rtems_build_name( ' ', ' ', ' ', 'A' )
918    #define SCHED_B rtems_build_name( ' ', ' ', ' ', 'B' )
919
920    static void Init( rtems_task_argument arg )
921    {
922      rtems_status_code   sc;
923      rtems_id            semaphore_id;
924      rtems_id            scheduler_a_id;
925      rtems_id            scheduler_b_id;
926      rtems_task_priority prio;
927
928      (void) arg;
929
930      // Get the scheduler identifiers
931      sc = rtems_scheduler_ident( SCHED_A, &scheduler_a_id );
932      assert( sc == RTEMS_SUCCESSFUL );
933      sc = rtems_scheduler_ident( SCHED_B, &scheduler_b_id );
934      assert( sc == RTEMS_SUCCESSFUL );
935
936      // Create a local, binary semaphore using the MrsP locking protocol
937      sc = rtems_semaphore_create(
938        rtems_build_name( 'M', 'R', 'S', 'P' ),
939        1,
940        RTEMS_BINARY_SEMAPHORE | RTEMS_PRIORITY |
941          RTEMS_MULTIPROCESSOR_RESOURCE_SHARING,
942        1,
943        &semaphore_id
944      );
945      assert( sc == RTEMS_SUCCESSFUL );
946
947      // The ceiling priority for each scheduler is equal to the priority
948      // specified for the semaphore creation.
949      prio = RTEMS_CURRENT_PRIORITY;
950      sc = rtems_semaphore_set_priority( semaphore_id, scheduler_a_id, prio, &prio );
951      assert( sc == RTEMS_SUCCESSFUL );
952      assert( prio == 1 );
953
954      // Check the old value and set a new ceiling priority for scheduler B
955      prio = 2;
956      sc = rtems_semaphore_set_priority( semaphore_id, scheduler_b_id, prio, &prio );
957      assert( sc == RTEMS_SUCCESSFUL );
958      assert( prio == 1 );
959
960      // Check the ceiling priority values
961      prio = RTEMS_CURRENT_PRIORITY;
962      sc = rtems_semaphore_set_priority( semaphore_id, scheduler_a_id, prio, &prio );
963      assert( sc == RTEMS_SUCCESSFUL );
964      assert( prio == 1 );
965      prio = RTEMS_CURRENT_PRIORITY;
966      sc = rtems_semaphore_set_priority( semaphore_id, scheduler_b_id, prio, &prio );
967      assert( sc == RTEMS_SUCCESSFUL );
968      assert( prio == 2 );
969
970      sc = rtems_semaphore_delete( semaphore_id );
971      assert( sc == RTEMS_SUCCESSFUL );
972
973      rtems_shutdown_executive( 0 );
974    }
975
976    #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
977    #define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
978    #define CONFIGURE_MAXIMUM_TASKS 1
979    #define CONFIGURE_MAXIMUM_SEMAPHORES 1
980    #define CONFIGURE_MAXIMUM_PROCESSORS 2
981
982    #define CONFIGURE_SCHEDULER_SIMPLE_SMP
983
984    #include <rtems/scheduler.h>
985
986    RTEMS_SCHEDULER_CONTEXT_SIMPLE_SMP( a );
987    RTEMS_SCHEDULER_CONTEXT_SIMPLE_SMP( b );
988
989    #define CONFIGURE_SCHEDULER_TABLE_ENTRIES \
990        RTEMS_SCHEDULER_TABLE_SIMPLE_SMP( a, SCHED_A ), \
991        RTEMS_SCHEDULER_TABLE_SIMPLE_SMP( b, SCHED_B )
992
993    #define CONFIGURE_SCHEDULER_ASSIGNMENTS \
994        RTEMS_SCHEDULER_ASSIGN( 0, RTEMS_SCHEDULER_ASSIGN_PROCESSOR_MANDATORY ), \
995        RTEMS_SCHEDULER_ASSIGN( 1, RTEMS_SCHEDULER_ASSIGN_PROCESSOR_MANDATORY )
996
997    #define CONFIGURE_RTEMS_INIT_TASKS_TABLE
998    #define CONFIGURE_INIT
999
1000    #include <rtems/confdefs.h>
1001
1002.. rubric:: CONSTRAINTS:
1003
1004The following constraints apply to this directive:
1005
1006* The directive may be called from within interrupt context.
1007
1008* The directive may be called from within device driver initialization context.
1009
1010* The directive may be called from within task context.
1011
1012* The directive may change the priority of another task which may preempt the
1013  calling task.
Note: See TracBrowser for help on using the repository browser.