source: rtems/doc/user/sem.t @ dc2b337

4.104.114.84.95
Last change on this file since dc2b337 was 5a69fe3, checked in by Joel Sherrill <joel.sherrill@…>, on 10/25/99 at 16:36:50

Formatting improvements.

  • Property mode set to 100644
File size: 29.4 KB
Line 
1@c
2@c  COPYRIGHT (c) 1988-1998.
3@c  On-Line Applications Research Corporation (OAR).
4@c  All rights reserved.
5@c
6@c  $Id$
7@c
8
9@chapter Semaphore Manager
10
11@cindex semaphores
12@cindex binary semaphores
13@cindex counting semaphores
14@cindex mutual exclusion
15
16@section Introduction
17
18The semaphore manager utilizes standard Dijkstra
19counting semaphores to provide synchronization and mutual
20exclusion capabilities.  The directives provided by the
21semaphore manager are:
22
23@itemize @bullet
24@item @code{@value{DIRPREFIX}semaphore_create} -  Create a semaphore
25@item @code{@value{DIRPREFIX}semaphore_ident} - Get ID of a semaphore
26@item @code{@value{DIRPREFIX}semaphore_delete} - Delete a semaphore
27@item @code{@value{DIRPREFIX}semaphore_obtain} - Acquire a semaphore
28@item @code{@value{DIRPREFIX}semaphore_release} - Release a semaphore
29@item @code{@value{DIRPREFIX}semaphore_flush} - Unblock all tasks waiting on a semaphore
30@end itemize
31
32@section Background
33
34A semaphore can be viewed as a protected variable
35whose value can be modified only with the
36@code{@value{DIRPREFIX}semaphore_create},
37@code{@value{DIRPREFIX}semaphore_obtain}, and
38@code{@value{DIRPREFIX}semaphore_release} directives.  RTEMS
39supports both binary and counting semaphores. A binary semaphore
40is restricted to values of zero or one, while a counting
41semaphore can assume any non-negative integer value.
42
43A binary semaphore can be used to control access to a
44single resource.  In particular, it can be used to enforce
45mutual exclusion for a critical section in user code.  In this
46instance, the semaphore would be created with an initial count
47of one to indicate that no task is executing the critical
48section of code.  Upon entry to the critical section, a task
49must issue the @code{@value{DIRPREFIX}semaphore_obtain}
50directive to prevent other tasks from entering the critical section. 
51Upon exit from the critical section, the task must issue the
52@code{@value{DIRPREFIX}semaphore_release} directive to
53allow another task to execute the critical section.
54
55A counting semaphore can be used to control access to
56a pool of two or more resources.  For example, access to three
57printers could be administered by a semaphore created with an
58initial count of three.  When a task requires access to one of
59the printers, it issues the @code{@value{DIRPREFIX}semaphore_obtain}
60directive to obtain access to a printer.  If a printer is not currently
61available, the task can wait for a printer to become available or return
62immediately.  When the task has completed printing, it should
63issue the @code{@value{DIRPREFIX}semaphore_release}
64directive to allow other tasks access to the printer.
65
66Task synchronization may be achieved by creating a
67semaphore with an initial count of zero.  One task waits for the
68arrival of another task by issuing a @code{@value{DIRPREFIX}semaphore_obtain}
69directive when it reaches a synchronization point.  The other task
70performs a corresponding @code{@value{DIRPREFIX}semaphore_release}
71operation when it reaches its synchronization point, thus unblocking
72the pending task.
73
74@subsection Nested Resource Access
75
76Deadlock occurs when a task owning a binary semaphore
77attempts to acquire that same semaphore and blocks as result.
78Since the semaphore is allocated to a task, it cannot be
79deleted.  Therefore, the task that currently holds the semaphore
80and is also blocked waiting for that semaphore will never
81execute again.
82
83RTEMS addresses this problem by allowing the task
84holding the binary semaphore to obtain the same binary semaphore
85multiple times in a nested manner.  Each
86@code{@value{DIRPREFIX}semaphore_obtain} must be accompanied with a
87@code{@value{DIRPREFIX}semaphore_release}.  The semaphore will
88only be made available for acquisition by other tasks when the
89outermost @code{@value{DIRPREFIX}semaphore_obtain} is matched with
90a @code{@value{DIRPREFIX}semaphore_release}.
91
92
93@subsection Priority Inversion
94
95Priority inversion is a form of indefinite
96postponement which is common in multitasking, preemptive
97executives with shared resources.  Priority inversion occurs
98when a high priority tasks requests access to shared resource
99which is currently allocated to low priority task.  The high
100priority task must block until the low priority task releases
101the resource.  This problem is exacerbated when the low priority
102task is prevented from executing by one or more medium priority
103tasks.  Because the low priority task is not executing, it
104cannot complete its interaction with the resource and release
105that resource.  The high priority task is effectively prevented
106from executing by lower priority tasks.
107
108@subsection Priority Inheritance
109
110Priority inheritance is an algorithm that calls for
111the lower priority task holding a resource to have its priority
112increased to that of the highest priority task blocked waiting
113for that resource.  Each time a task blocks attempting to obtain
114the resource, the task holding the resource may have its
115priority increased.
116
117RTEMS supports priority inheritance for local, binary
118semaphores that use the priority task wait queue blocking
119discipline.   When a task of higher priority than the task
120holding the semaphore blocks, the priority of the task holding
121the semaphore is increased to that of the blocking task.  When
122the task holding the task completely releases the binary
123semaphore (i.e. not for a nested release), the holder's priority
124is restored to the value it had before any higher priority was
125inherited.
126
127The RTEMS implementation of the priority inheritance
128algorithm takes into account the scenario in which a task holds
129more than one binary semaphore.  The holding task will execute
130at the priority of the higher of the highest ceiling priority or
131at the priority of the highest priority task blocked waiting for
132any of the semaphores the task holds.  Only when the task
133releases ALL of the binary semaphores it holds will its priority
134be restored to the normal value.
135
136@subsection Priority Ceiling
137
138Priority ceiling is an algorithm that calls for the
139lower priority task holding a resource to have its priority
140increased to that of the highest priority task which will EVER
141block waiting for that resource.  This algorithm addresses the
142problem of priority inversion although it avoids the possibility
143of changing the priority of the task holding the resource
144multiple times.  The priority ceiling algorithm will only change
145the priority of the task holding the resource a maximum of one
146time.  The ceiling priority is set at creation time and must be
147the priority of the highest priority task which will ever
148attempt to acquire that semaphore.
149
150RTEMS supports priority ceiling for local, binary
151semaphores that use the priority task wait queue blocking
152discipline.   When a task of lower priority than the ceiling
153priority successfully obtains the semaphore, its priority is
154raised to the ceiling priority.  When the task holding the task
155completely releases the binary semaphore (i.e. not for a nested
156release), the holder's priority is restored to the value it had
157before any higher priority was put into effect.
158
159The need to identify the highest priority task which
160will attempt to obtain a particular semaphore can be a difficult
161task in a large, complicated system.  Although the priority
162ceiling algorithm is more efficient than the priority
163inheritance algorithm with respect to the maximum number of task
164priority changes which may occur while a task holds a particular
165semaphore, the priority inheritance algorithm is more forgiving
166in that it does not require this apriori information.
167
168The RTEMS implementation of the priority ceiling
169algorithm takes into account the scenario in which a task holds
170more than one binary semaphore.  The holding task will execute
171at the priority of the higher of the highest ceiling priority or
172at the priority of the highest priority task blocked waiting for
173any of the semaphores the task holds.  Only when the task
174releases ALL of the binary semaphores it holds will its priority
175be restored to the normal value.
176
177@subsection Building a Semaphore Attribute Set
178
179In general, an attribute set is built by a bitwise OR
180of the desired attribute components.  The following table lists
181the set of valid semaphore attributes:
182
183@itemize @bullet
184@item @code{@value{RPREFIX}FIFO} - tasks wait by FIFO (default)
185
186@item @code{@value{RPREFIX}PRIORITY} - tasks wait by priority
187
188@item @code{@value{RPREFIX}BINARY_SEMAPHORE} - restrict values to
1890 and 1 (default)
190
191@item @code{@value{RPREFIX}COUNTING_SEMAPHORE} - no restriction on values
192
193@item @code{@value{RPREFIX}NO_INHERIT_PRIORITY} - do not use priority
194inheritance (default)
195
196@item @code{@value{RPREFIX}INHERIT_PRIORITY} - use priority inheritance
197
198@item @code{@value{RPREFIX}PRIORITY_CEILING} - use priority ceiling
199
200@item @code{@value{RPREFIX}NO_PRIORITY_CEILING} - do not use priority
201ceiling (default)
202
203@item @code{@value{RPREFIX}LOCAL} - local task (default)
204
205@item @code{@value{RPREFIX}GLOBAL} - global task
206@end itemize
207
208Attribute values are specifically designed to be
209mutually exclusive, therefore bitwise OR and addition operations
210are equivalent as long as each attribute appears exactly once in
211the component list.  An attribute listed as a default is not
212required to appear in the attribute list, although it is a good
213programming practice to specify default attributes.  If all
214defaults are desired, the attribute
215@code{@value{RPREFIX}DEFAULT_ATTRIBUTES} should be
216specified on this call.
217
218This example demonstrates the attribute_set parameter needed to create a
219local semaphore with the task priority waiting queue discipline.  The
220attribute_set parameter passed to the
221@code{@value{DIRPREFIX}semaphore_create} directive could be either
222@code{@value{RPREFIX}PRIORITY} or @code{@value{RPREFIX}LOCAL @value{OR}
223@value{RPREFIX}PRIORITY}.  The attribute_set parameter can be set to
224@code{@value{RPREFIX}PRIORITY} because @code{@value{RPREFIX}LOCAL} is the
225default for all created tasks.  If a similar semaphore were to be known
226globally, then the attribute_set parameter would be
227@code{@value{RPREFIX}GLOBAL @value{OR} @value{RPREFIX}PRIORITY}.
228
229@subsection Building a SEMAPHORE_OBTAIN Option Set
230
231In general, an option is built by a bitwise OR of the
232desired option components.  The set of valid options for the
233@code{@value{DIRPREFIX}semaphore_obtain} directive are listed
234in the following table:
235
236@itemize @bullet
237@item @code{@value{RPREFIX}WAIT} - task will wait for semaphore (default)
238@item @code{@value{RPREFIX}NO_WAIT} - task should not wait
239@end itemize
240
241Option values are specifically designed to be mutually exclusive,
242therefore bitwise OR and addition operations are equivalent as long as
243each attribute appears exactly once in the component list.  An option
244listed as a default is not required to appear in the list, although it is
245a good programming practice to specify default options.  If all defaults
246are desired, the option @code{@value{RPREFIX}DEFAULT_OPTIONS} should be
247specified on this call.
248
249This example demonstrates the option parameter needed
250to poll for a semaphore.  The option parameter passed to the
251@code{@value{DIRPREFIX}semaphore_obtain}
252directive should be @code{@value{RPREFIX}NO_WAIT}.
253
254@section Operations
255
256@subsection Creating a Semaphore
257
258The @code{@value{DIRPREFIX}semaphore_create} directive creates a binary or
259counting semaphore with a user-specified name as well as an
260initial count.  If a binary semaphore is created with a count of
261zero (0) to indicate that it has been allocated, then the task
262creating the semaphore is considered the current holder of the
263semaphore.  At create time the method for ordering waiting tasks
264in the semaphore's task wait queue (by FIFO or task priority) is
265specified.  Additionally, the priority inheritance or priority
266ceiling algorithm may be selected for local, binary semaphores
267that use the priority task wait queue blocking discipline.  If
268the priority ceiling algorithm is selected, then the highest
269priority of any task which will attempt to obtain this semaphore
270must be specified.  RTEMS allocates a Semaphore Control Block
271(SMCB) from the SMCB free list.  This data structure is used by
272RTEMS to manage the newly created semaphore.  Also, a unique
273semaphore ID is generated and returned to the calling task.
274
275@subsection Obtaining Semaphore IDs
276
277When a semaphore is created, RTEMS generates a unique
278semaphore ID and assigns it to the created semaphore until it is
279deleted.  The semaphore ID may be obtained by either of two
280methods.  First, as the result of an invocation of the
281@code{@value{DIRPREFIX}semaphore_create} directive, the
282semaphore ID is stored in a user provided location.  Second,
283the semaphore ID may be obtained later using the
284@code{@value{DIRPREFIX}semaphore_ident} directive.  The semaphore ID is
285used by other semaphore manager directives to access this
286semaphore.
287
288@subsection Acquiring a Semaphore
289
290The @code{@value{DIRPREFIX}semaphore_obtain} directive is used to acquire the
291specified semaphore.  A simplified version of the
292@code{@value{DIRPREFIX}semaphore_obtain} directive can be described as follows:
293
294@example
295if semaphore's count is greater than zero
296   then decrement semaphore's count
297   else wait for release of semaphore
298
299return SUCCESSFUL
300@end example
301
302When the semaphore cannot be immediately acquired,
303one of the following situations applies:
304
305@itemize @bullet
306@item By default, the calling task will wait forever to
307acquire the semaphore.
308
309@item Specifying @code{@value{RPREFIX}NO_WAIT} forces an immediate return
310with an error status code.
311
312@item Specifying a timeout limits the interval the task will
313wait before returning with an error status code.
314@end itemize
315
316If the task waits to acquire the semaphore, then it
317is placed in the semaphore's task wait queue in either FIFO or
318task priority order.  If the task blocked waiting for a binary
319semaphore using priority inheritance and the task's priority is
320greater than that of the task currently holding the semaphore,
321then the holding task will inherit the priority of the blocking
322task.  All tasks waiting on a semaphore are returned an error
323code when the semaphore is deleted.
324
325When a task successfully obtains a semaphore using
326priority ceiling and the priority ceiling for this semaphore is
327greater than that of the holder, then the holder's priority will
328be elevated.
329
330@subsection Releasing a Semaphore
331
332The @code{@value{DIRPREFIX}semaphore_release} directive is used to release
333the specified semaphore.  A simplified version of the
334@code{@value{DIRPREFIX}semaphore_release} directive can be described as
335follows:
336
337@example
338if no tasks are waiting on this semaphore
339   then increment semaphore's count
340   else assign semaphore to a waiting task
341
342return SUCCESSFUL
343@end example
344
345If this is the outermost release of a binary
346semaphore that uses priority inheritance or priority ceiling and
347the task does not currently hold any other binary semaphores,
348then the task performing the @code{@value{DIRPREFIX}semaphore_release}
349will have its priority restored to its normal value.
350
351@subsection Deleting a Semaphore
352
353The @code{@value{DIRPREFIX}semaphore_delete} directive removes a semaphore
354from the system and frees its control block.  A semaphore can be
355deleted by any local task that knows the semaphore's ID.  As a
356result of this directive, all tasks blocked waiting to acquire
357the semaphore will be readied and returned a status code which
358indicates that the semaphore was deleted.  Any subsequent
359references to the semaphore's name and ID are invalid.
360
361@section Directives
362
363This section details the semaphore manager's
364directives.  A subsection is dedicated to each of this manager's
365directives and describes the calling sequence, related
366constants, usage, and status codes.
367
368@c
369@c
370@c
371@page
372@subsection SEMAPHORE_CREATE - Create a semaphore
373
374@cindex create a semaphore
375
376@subheading CALLING SEQUENCE:
377
378@ifset is-C
379@findex rtems_semaphore_create
380@example
381rtems_status_code rtems_semaphore_create(
382  rtems_name           name,
383  rtems_unsigned32     count,
384  rtems_attribute      attribute_set,
385  rtems_task_priority  priority_ceiling,
386  rtems_id            *id
387);
388@end example
389@end ifset
390
391@ifset is-Ada
392@example
393procedure Semaphore_Create (
394   Name          : in     RTEMS.Name;
395   Count         : in     RTEMS.Unsigned32;
396   Attribute_Set : in     RTEMS.Attribute;
397   ID            :    out RTEMS.ID;
398   Result        :    out RTEMS.Status_Codes
399);
400@end example
401@end ifset
402
403@subheading DIRECTIVE STATUS CODES:
404@code{@value{RPREFIX}SUCCESSFUL} - semaphore created successfully@*
405@code{@value{RPREFIX}INVALID_NAME} - invalid task name@*
406@code{@value{RPREFIX}TOO_MANY} - too many semaphores created@*
407@code{@value{RPREFIX}NOT_DEFINED} - invalid attribute set@*
408@code{@value{RPREFIX}INVALID_NUMBER} - invalid starting count for binary semaphore@*
409@code{@value{RPREFIX}MP_NOT_CONFIGURED} - multiprocessing not configured@*
410@code{@value{RPREFIX}TOO_MANY} - too many global objects
411
412@subheading DESCRIPTION:
413
414This directive creates a semaphore which resides on
415the local node. The created semaphore has the user-defined name
416specified in name and the initial count specified in count.  For
417control and maintenance of the semaphore, RTEMS allocates and
418initializes a SMCB.  The RTEMS-assigned semaphore id is returned
419in id.  This semaphore id is used with other semaphore related
420directives to access the semaphore.
421
422Specifying PRIORITY in attribute_set causes tasks
423waiting for a semaphore to be serviced according to task
424priority.  When FIFO is selected, tasks are serviced in First
425In-First Out order.
426
427@subheading NOTES:
428
429This directive will not cause the calling task to be
430preempted.
431
432The priority inheritance and priority ceiling
433algorithms are only supported for local, binary semaphores that
434use the priority task wait queue blocking discipline.
435
436The following semaphore attribute constants are
437defined by RTEMS:
438
439@itemize @bullet
440@item @code{@value{RPREFIX}FIFO} - tasks wait by FIFO (default)
441
442@item @code{@value{RPREFIX}PRIORITY} - tasks wait by priority
443
444@item @code{@value{RPREFIX}BINARY_SEMAPHORE} - restrict values to
4450 and 1 (default)
446
447@item @code{@value{RPREFIX}COUNTING_SEMAPHORE} - no restriction on values
448
449@item @code{@value{RPREFIX}NO_INHERIT_PRIORITY} - do not use priority
450inheritance (default)
451
452@item @code{@value{RPREFIX}INHERIT_PRIORITY} - use priority inheritance
453
454@item @code{@value{RPREFIX}PRIORITY_CEILING} - use priority ceiling
455
456@item @code{@value{RPREFIX}NO_PRIORITY_CEILING} - do not use priority
457ceiling (default)
458
459@item @code{@value{RPREFIX}LOCAL} - local task (default)
460
461@item @code{@value{RPREFIX}GLOBAL} - global task
462@end itemize
463
464Semaphores should not be made global unless remote
465tasks must interact with the created semaphore.  This is to
466avoid the system overhead incurred by the creation of a global
467semaphore.  When a global semaphore is created, the semaphore's
468name and id must be transmitted to every node in the system for
469insertion in the local copy of the global object table.
470
471The total number of global objects, including
472semaphores, is limited by the maximum_global_objects field in
473the Configuration Table.
474
475@c
476@c
477@c
478@page
479@subsection SEMAPHORE_IDENT - Get ID of a semaphore
480
481@cindex get ID of a semaphore
482@cindex obtain ID of a semaphore
483
484@subheading CALLING SEQUENCE:
485
486@ifset is-C
487@findex rtems_semaphore_ident
488@example
489rtems_status_code rtems_semaphore_ident(
490  rtems_name        name,
491  rtems_unsigned32  node,
492  rtems_id         *id
493);
494@end example
495@end ifset
496
497@ifset is-Ada
498@example
499procedure Semaphore_Ident (
500   Name   : in     RTEMS.Name;
501   Node   : in     RTEMS.Unsigned32;
502   ID     :    out RTEMS.ID;
503   Result :    out RTEMS.Status_Codes
504);
505@end example
506@end ifset
507
508@subheading DIRECTIVE STATUS CODES:
509@code{@value{RPREFIX}SUCCESSFUL} - semaphore identified successfully@*
510@code{@value{RPREFIX}INVALID_NAME} - semaphore name not found@*
511@code{@value{RPREFIX}INVALID_NODE} - invalid node id
512
513@subheading DESCRIPTION:
514
515This directive obtains the semaphore id associated
516with the semaphore name.  If the semaphore name is not unique,
517then the semaphore id will match one of the semaphores with that
518name.  However, this semaphore id is not guaranteed to
519correspond to the desired semaphore.  The semaphore id is used
520by other semaphore related directives to access the semaphore.
521
522@subheading NOTES:
523
524This directive will not cause the running task to be
525preempted.
526
527If node is @code{@value{RPREFIX}SEARCH_ALL_NODES}, all nodes are searched
528with the local node being searched first.  All other nodes are
529searched with the lowest numbered node searched first.
530
531If node is a valid node number which does not
532represent the local node, then only the semaphores exported by
533the designated node are searched.
534
535This directive does not generate activity on remote
536nodes.  It accesses only the local copy of the global object
537table.
538
539@c
540@c
541@c
542@page
543@subsection SEMAPHORE_DELETE - Delete a semaphore
544
545@cindex delete a semaphore
546
547@subheading CALLING SEQUENCE:
548
549@ifset is-C
550@findex rtems_semaphore_delete
551@example
552rtems_status_code rtems_semaphore_delete(
553  rtems_id id
554);
555@end example
556@end ifset
557
558@ifset is-Ada
559@example
560procedure Semaphore_Delete (
561   ID     : in     RTEMS.ID;
562   Result :    out RTEMS.Status_Codes
563);
564@end example
565@end ifset
566
567@subheading DIRECTIVE STATUS CODES:
568@code{@value{RPREFIX}SUCCESSFUL} -  semaphore deleted successfully@*
569@code{@value{RPREFIX}INVALID_ID} - invalid semaphore id@*
570@code{@value{RPREFIX}ILLEGAL_ON_REMOTE_OBJECT} - cannot delete remote semaphore@*
571@code{@value{RPREFIX}RESOURCE_IN_USE} - binary semaphore is in use
572
573@subheading DESCRIPTION:
574
575This directive deletes the semaphore specified by @code{id}.
576All tasks blocked waiting to acquire the semaphore will be
577readied and returned a status code which indicates that the
578semaphore was deleted.  The SMCB for this semaphore is reclaimed
579by RTEMS.
580
581@subheading NOTES:
582
583The calling task will be preempted if it is enabled
584by the task's execution mode and a higher priority local task is
585waiting on the deleted semaphore.  The calling task will NOT be
586preempted if all of the tasks that are waiting on the semaphore
587are remote tasks.
588
589The calling task does not have to be the task that
590created the semaphore.  Any local task that knows the semaphore
591id can delete the semaphore.
592
593When a global semaphore is deleted, the semaphore id
594must be transmitted to every node in the system for deletion
595from the local copy of the global object table.
596
597The semaphore must reside on the local node, even if
598the semaphore was created with the @code{@value{RPREFIX}GLOBAL} option.
599
600Proxies, used to represent remote tasks, are
601reclaimed when the semaphore is deleted.
602
603@c
604@c
605@c
606@page
607@subsection SEMAPHORE_OBTAIN - Acquire a semaphore
608
609@cindex obtain a semaphore
610@cindex lock a semaphore
611
612@subheading CALLING SEQUENCE:
613
614@ifset is-C
615@findex rtems_semaphore_obtain
616@example
617rtems_status_code rtems_semaphore_obtain(
618  rtems_id         id,
619  rtems_unsigned32 option_set,
620  rtems_interval   timeout
621);
622@end example
623@end ifset
624
625@ifset is-Ada
626@example
627procedure Semaphore_Obtain (
628   ID         : in     RTEMS.ID;
629   Option_Set : in     RTEMS.Option;
630   Timeout    : in     RTEMS.Interval;
631   Result     :    out RTEMS.Status_Codes
632);
633@end example
634@end ifset
635
636@subheading DIRECTIVE STATUS CODES:
637@code{@value{RPREFIX}SUCCESSFUL} - semaphore obtained successfully@*
638@code{@value{RPREFIX}UNSATISFIED} - semaphore not available@*
639@code{@value{RPREFIX}TIMEOUT} - timed out waiting for semaphore@*
640@code{@value{RPREFIX}OBJECT_WAS_DELETED} - semaphore deleted while waiting@*
641@code{@value{RPREFIX}INVALID_ID} - invalid semaphore id
642
643@subheading DESCRIPTION:
644
645This directive acquires the semaphore specified by
646id.  The @code{@value{RPREFIX}WAIT} and @code{@value{RPREFIX}NO_WAIT} components of the options parameter
647indicate whether the calling task wants to wait for the
648semaphore to become available or return immediately if the
649semaphore is not currently available.  With either @code{@value{RPREFIX}WAIT} or
650@code{@value{RPREFIX}NO_WAIT}, if the current semaphore count is positive, then it is
651decremented by one and the semaphore is successfully acquired by
652returning immediately with a successful return code.
653
654If the calling task chooses to return immediately and the current
655semaphore count is zero or negative, then a status code is returned
656indicating that the semaphore is not available. If the calling task
657chooses to wait for a semaphore and the current semaphore count is zero or
658negative, then it is decremented by one and the calling task is placed on
659the semaphore's wait queue and blocked.  If the semaphore was created with
660the @code{@value{RPREFIX}PRIORITY} attribute, then the calling task is
661inserted into the queue according to its priority.  However, if the
662semaphore was created with the @code{@value{RPREFIX}FIFO} attribute, then
663the calling task is placed at the rear of the wait queue.  If the binary
664semaphore was created with the @code{@value{RPREFIX}INHERIT_PRIORITY}
665attribute, then the priority of the task currently holding the binary
666semaphore is guaranteed to be greater than or equal to that of the
667blocking task.  If the binary semaphore was created with the
668@code{@value{RPREFIX}PRIORITY_CEILING} attribute, a task successfully
669obtains the semaphore, and the priority of that task is greater than the
670ceiling priority for this semaphore, then the priority of the task
671obtaining the semaphore is elevated to that of the ceiling.
672
673The timeout parameter specifies the maximum interval the calling task is
674willing to be blocked waiting for the semaphore.  If it is set to
675@code{@value{RPREFIX}NO_TIMEOUT}, then the calling task will wait forever. 
676If the semaphore is available or the @code{@value{RPREFIX}NO_WAIT} option
677component is set, then timeout is ignored.
678
679@subheading NOTES:
680The following semaphore acquisition option constants
681are defined by RTEMS:
682
683@itemize @bullet
684@item @code{@value{RPREFIX}WAIT} - task will wait for semaphore (default)
685@item @code{@value{RPREFIX}NO_WAIT} - task should not wait
686@end itemize
687
688Attempting to obtain a global semaphore which does not reside on the local
689node will generate a request to the remote node to access the semaphore. 
690If the semaphore is not available and @code{@value{RPREFIX}NO_WAIT} was
691not specified, then the task must be blocked until the semaphore is
692released.  A proxy is allocated on the remote node to represent the task
693until the semaphore is released.
694
695A clock tick is required to support the timeout functionality of
696this directive.
697
698@c
699@c
700@c
701@page
702@subsection SEMAPHORE_RELEASE - Release a semaphore
703
704@cindex release a semaphore
705@cindex unlock a semaphore
706
707@subheading CALLING SEQUENCE:
708
709@ifset is-C
710@findex rtems_semaphore_release
711@example
712rtems_status_code rtems_semaphore_release(
713  rtems_id id
714);
715@end example
716@end ifset
717
718@ifset is-Ada
719@example
720procedure Semaphore_Release (
721   ID     : in     RTEMS.ID;
722   Result :    out RTEMS.Status_Codes
723);
724@end example
725@end ifset
726
727@subheading DIRECTIVE STATUS CODES:
728@code{@value{RPREFIX}SUCCESSFUL} - semaphore released successfully@*
729@code{@value{RPREFIX}INVALID_ID} - invalid semaphore id@*
730@code{@value{RPREFIX}NOT_OWNER_OF_RESOURCE} - calling task does not own semaphore
731
732@subheading DESCRIPTION:
733
734This directive releases the semaphore specified by
735id.  The semaphore count is incremented by one.  If the count is
736zero or negative, then the first task on this semaphore's wait
737queue is removed and unblocked.  The unblocked task may preempt
738the running task if the running task's preemption mode is
739enabled and the unblocked task has a higher priority than the
740running task.
741
742@subheading NOTES:
743
744The calling task may be preempted if it causes a
745higher priority task to be made ready for execution.
746
747Releasing a global semaphore which does not reside on
748the local node will generate a request telling the remote node
749to release the semaphore.
750
751If the task to be unblocked resides on a different
752node from the semaphore, then the semaphore allocation is
753forwarded to the appropriate node, the waiting task is
754unblocked, and the proxy used to represent the task is reclaimed.
755
756The outermost release of a local, binary, priority
757inheritance or priority ceiling semaphore may result in the
758calling task having its priority lowered.  This will occur if
759the calling task holds no other binary semaphores and it has
760inherited a higher priority.
761
762@c
763@c
764@c
765@page
766@subsection SEMAPHORE_FLUSH - Unblock all tasks waiting on a semaphore
767
768@cindex flush a semaphore
769@cindex unlock all tasks a semaphore
770
771@subheading CALLING SEQUENCE:
772
773@ifset is-C
774@findex rtems_semaphore_flush
775@example
776rtems_status_code rtems_semaphore_flush(
777  rtems_id id
778);
779@end example
780@end ifset
781
782@ifset is-Ada
783@example
784procedure Semaphore_Flush (
785   ID     : in     RTEMS.ID;
786   Result :    out RTEMS.Status_Codes
787);
788@end example
789@end ifset
790
791@subheading DIRECTIVE STATUS CODES:
792@code{@value{RPREFIX}SUCCESSFUL} - semaphore released successfully@*
793@code{@value{RPREFIX}INVALID_ID} - invalid semaphore id@*
794@code{@value{RPREFIX}ILLEGAL_ON_REMOTE_OBJECT} - not supported for remote semaphores
795
796@subheading DESCRIPTION:
797
798This directive unblocks all tasks waiting on the semaphore specified by
799id.  Since there are tasks blocked on the semaphore, the semaphore's
800count is not changed by this directive and thus is zero before and
801after this directive is executed.  Tasks which are unblocked as the
802result of this directive will return from the
803@code{@value{DIRPREFIX}semaphore_release} directive with a
804status code of @code{@value{RPREFIX}UNSATISFIED} to indicate
805that the semaphore was not obtained.
806
807This directive may unblock any number of tasks.  Any of the unblocked
808tasks may preempt the running task if the running task's preemption mode is
809enabled and an unblocked task has a higher priority than the
810running task.
811
812@subheading NOTES:
813
814The calling task may be preempted if it causes a
815higher priority task to be made ready for execution.
816
817If the task to be unblocked resides on a different
818node from the semaphore, then the waiting task is
819unblocked, and the proxy used to represent the task is reclaimed.
820
821
Note: See TracBrowser for help on using the repository browser.