source: rtems/doc/user/sem.t @ 9b4422a2

4.115
Last change on this file since 9b4422a2 was 9b4422a2, checked in by Joel Sherrill <joel.sherrill@…>, on 05/03/12 at 15:09:24

Remove All CVS Id Strings Possible Using a Script

Script does what is expected and tries to do it as
smartly as possible.

+ remove occurrences of two blank comment lines

next to each other after Id string line removed.

+ remove entire comment blocks which only exited to

contain CVS Ids

+ If the processing left a blank line at the top of

a file, it was removed.

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