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 | |
---|
15 | The semaphore manager utilizes standard Dijkstra |
---|
16 | counting semaphores to provide synchronization and mutual |
---|
17 | exclusion capabilities. The directives provided by the |
---|
18 | semaphore 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 | |
---|
31 | A semaphore can be viewed as a protected variable |
---|
32 | whose 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 |
---|
36 | supports both binary and counting semaphores. A binary semaphore |
---|
37 | is restricted to values of zero or one, while a counting |
---|
38 | semaphore can assume any non-negative integer value. |
---|
39 | |
---|
40 | A binary semaphore can be used to control access to a |
---|
41 | single resource. In particular, it can be used to enforce |
---|
42 | mutual exclusion for a critical section in user code. In this |
---|
43 | instance, the semaphore would be created with an initial count |
---|
44 | of one to indicate that no task is executing the critical |
---|
45 | section of code. Upon entry to the critical section, a task |
---|
46 | must issue the @code{@value{DIRPREFIX}semaphore_obtain} |
---|
47 | directive to prevent other tasks from entering the critical section. |
---|
48 | Upon exit from the critical section, the task must issue the |
---|
49 | @code{@value{DIRPREFIX}semaphore_release} directive to |
---|
50 | allow another task to execute the critical section. |
---|
51 | |
---|
52 | A counting semaphore can be used to control access to |
---|
53 | a pool of two or more resources. For example, access to three |
---|
54 | printers could be administered by a semaphore created with an |
---|
55 | initial count of three. When a task requires access to one of |
---|
56 | the printers, it issues the @code{@value{DIRPREFIX}semaphore_obtain} |
---|
57 | directive to obtain access to a printer. If a printer is not currently |
---|
58 | available, the task can wait for a printer to become available or return |
---|
59 | immediately. When the task has completed printing, it should |
---|
60 | issue the @code{@value{DIRPREFIX}semaphore_release} |
---|
61 | directive to allow other tasks access to the printer. |
---|
62 | |
---|
63 | Task synchronization may be achieved by creating a |
---|
64 | semaphore with an initial count of zero. One task waits for the |
---|
65 | arrival of another task by issuing a @code{@value{DIRPREFIX}semaphore_obtain} |
---|
66 | directive when it reaches a synchronization point. The other task |
---|
67 | performs a corresponding @code{@value{DIRPREFIX}semaphore_release} |
---|
68 | operation when it reaches its synchronization point, thus unblocking |
---|
69 | the pending task. |
---|
70 | |
---|
71 | @subsection Nested Resource Access |
---|
72 | |
---|
73 | Deadlock occurs when a task owning a binary semaphore |
---|
74 | attempts to acquire that same semaphore and blocks as result. |
---|
75 | Since the semaphore is allocated to a task, it cannot be |
---|
76 | deleted. Therefore, the task that currently holds the semaphore |
---|
77 | and is also blocked waiting for that semaphore will never |
---|
78 | execute again. |
---|
79 | |
---|
80 | RTEMS addresses this problem by allowing the task |
---|
81 | holding the binary semaphore to obtain the same binary semaphore |
---|
82 | multiple 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 |
---|
85 | only be made available for acquisition by other tasks when the |
---|
86 | outermost @code{@value{DIRPREFIX}semaphore_obtain} is matched with |
---|
87 | a @code{@value{DIRPREFIX}semaphore_release}. |
---|
88 | |
---|
89 | Simple binary semaphores do not allow nested access and so can be used for task synchronization. |
---|
90 | |
---|
91 | |
---|
92 | @subsection Priority Inversion |
---|
93 | |
---|
94 | Priority inversion is a form of indefinite |
---|
95 | postponement which is common in multitasking, preemptive |
---|
96 | executives with shared resources. Priority inversion occurs |
---|
97 | when a high priority tasks requests access to shared resource |
---|
98 | which is currently allocated to low priority task. The high |
---|
99 | priority task must block until the low priority task releases |
---|
100 | the resource. This problem is exacerbated when the low priority |
---|
101 | task is prevented from executing by one or more medium priority |
---|
102 | tasks. Because the low priority task is not executing, it |
---|
103 | cannot complete its interaction with the resource and release |
---|
104 | that resource. The high priority task is effectively prevented |
---|
105 | from executing by lower priority tasks. |
---|
106 | |
---|
107 | @subsection Priority Inheritance |
---|
108 | |
---|
109 | Priority inheritance is an algorithm that calls for |
---|
110 | the lower priority task holding a resource to have its priority |
---|
111 | increased to that of the highest priority task blocked waiting |
---|
112 | for that resource. Each time a task blocks attempting to obtain |
---|
113 | the resource, the task holding the resource may have its |
---|
114 | priority increased. |
---|
115 | |
---|
116 | RTEMS supports priority inheritance for local, binary |
---|
117 | semaphores that use the priority task wait queue blocking |
---|
118 | discipline. When a task of higher priority than the task |
---|
119 | holding the semaphore blocks, the priority of the task holding |
---|
120 | the semaphore is increased to that of the blocking task. When |
---|
121 | the task holding the task completely releases the binary |
---|
122 | semaphore (i.e. not for a nested release), the holder's priority |
---|
123 | is restored to the value it had before any higher priority was |
---|
124 | inherited. |
---|
125 | |
---|
126 | The RTEMS implementation of the priority inheritance |
---|
127 | algorithm takes into account the scenario in which a task holds |
---|
128 | more than one binary semaphore. The holding task will execute |
---|
129 | at the priority of the higher of the highest ceiling priority or |
---|
130 | at the priority of the highest priority task blocked waiting for |
---|
131 | any of the semaphores the task holds. Only when the task |
---|
132 | releases ALL of the binary semaphores it holds will its priority |
---|
133 | be restored to the normal value. |
---|
134 | |
---|
135 | @subsection Priority Ceiling |
---|
136 | |
---|
137 | Priority ceiling is an algorithm that calls for the |
---|
138 | lower priority task holding a resource to have its priority |
---|
139 | increased to that of the highest priority task which will EVER |
---|
140 | block waiting for that resource. This algorithm addresses the |
---|
141 | problem of priority inversion although it avoids the possibility |
---|
142 | of changing the priority of the task holding the resource |
---|
143 | multiple times. The priority ceiling algorithm will only change |
---|
144 | the priority of the task holding the resource a maximum of one |
---|
145 | time. The ceiling priority is set at creation time and must be |
---|
146 | the priority of the highest priority task which will ever |
---|
147 | attempt to acquire that semaphore. |
---|
148 | |
---|
149 | RTEMS supports priority ceiling for local, binary |
---|
150 | semaphores that use the priority task wait queue blocking |
---|
151 | discipline. When a task of lower priority than the ceiling |
---|
152 | priority successfully obtains the semaphore, its priority is |
---|
153 | raised to the ceiling priority. When the task holding the task |
---|
154 | completely releases the binary semaphore (i.e. not for a nested |
---|
155 | release), the holder's priority is restored to the value it had |
---|
156 | before any higher priority was put into effect. |
---|
157 | |
---|
158 | The need to identify the highest priority task which |
---|
159 | will attempt to obtain a particular semaphore can be a difficult |
---|
160 | task in a large, complicated system. Although the priority |
---|
161 | ceiling algorithm is more efficient than the priority |
---|
162 | inheritance algorithm with respect to the maximum number of task |
---|
163 | priority changes which may occur while a task holds a particular |
---|
164 | semaphore, the priority inheritance algorithm is more forgiving |
---|
165 | in that it does not require this apriori information. |
---|
166 | |
---|
167 | The RTEMS implementation of the priority ceiling |
---|
168 | algorithm takes into account the scenario in which a task holds |
---|
169 | more than one binary semaphore. The holding task will execute |
---|
170 | at the priority of the higher of the highest ceiling priority or |
---|
171 | at the priority of the highest priority task blocked waiting for |
---|
172 | any of the semaphores the task holds. Only when the task |
---|
173 | releases ALL of the binary semaphores it holds will its priority |
---|
174 | be restored to the normal value. |
---|
175 | |
---|
176 | @subsection Building a Semaphore Attribute Set |
---|
177 | |
---|
178 | In general, an attribute set is built by a bitwise OR |
---|
179 | of the desired attribute components. The following table lists |
---|
180 | the 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 |
---|
188 | 0 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 |
---|
194 | 0 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 |
---|
197 | inheritance (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 |
---|
204 | ceiling (default) |
---|
205 | |
---|
206 | @item @code{@value{RPREFIX}LOCAL} - local semaphore (default) |
---|
207 | |
---|
208 | @item @code{@value{RPREFIX}GLOBAL} - global semaphore |
---|
209 | @end itemize |
---|
210 | |
---|
211 | Attribute values are specifically designed to be |
---|
212 | mutually exclusive, therefore bitwise OR and addition operations |
---|
213 | are equivalent as long as each attribute appears exactly once in |
---|
214 | the component list. An attribute listed as a default is not |
---|
215 | required to appear in the attribute list, although it is a good |
---|
216 | programming practice to specify default attributes. If all |
---|
217 | defaults are desired, the attribute |
---|
218 | @code{@value{RPREFIX}DEFAULT_ATTRIBUTES} should be |
---|
219 | specified on this call. |
---|
220 | |
---|
221 | This example demonstrates the attribute_set parameter needed to create a |
---|
222 | local semaphore with the task priority waiting queue discipline. The |
---|
223 | attribute_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 |
---|
228 | default for all created tasks. If a similar semaphore were to be known |
---|
229 | globally, then the attribute_set parameter would be |
---|
230 | @code{@value{RPREFIX}GLOBAL @value{OR} @value{RPREFIX}PRIORITY}. |
---|
231 | |
---|
232 | Some combinatinos of these attributes are invalid. For example, priority |
---|
233 | ordered blocking discipline must be applied to a binary semaphore in order |
---|
234 | to use either the priority inheritance or priority ceiling functionality. |
---|
235 | The 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 |
---|
243 | Not 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 | |
---|
263 | In general, an option is built by a bitwise OR of the |
---|
264 | desired option components. The set of valid options for the |
---|
265 | @code{@value{DIRPREFIX}semaphore_obtain} directive are listed |
---|
266 | in 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 | |
---|
273 | Option values are specifically designed to be mutually exclusive, |
---|
274 | therefore bitwise OR and addition operations are equivalent as long as |
---|
275 | each attribute appears exactly once in the component list. An option |
---|
276 | listed as a default is not required to appear in the list, although it is |
---|
277 | a good programming practice to specify default options. If all defaults |
---|
278 | are desired, the option @code{@value{RPREFIX}DEFAULT_OPTIONS} should be |
---|
279 | specified on this call. |
---|
280 | |
---|
281 | This example demonstrates the option parameter needed |
---|
282 | to poll for a semaphore. The option parameter passed to the |
---|
283 | @code{@value{DIRPREFIX}semaphore_obtain} |
---|
284 | directive should be @code{@value{RPREFIX}NO_WAIT}. |
---|
285 | |
---|
286 | @section Operations |
---|
287 | |
---|
288 | @subsection Creating a Semaphore |
---|
289 | |
---|
290 | The @code{@value{DIRPREFIX}semaphore_create} directive creates a binary or |
---|
291 | counting semaphore with a user-specified name as well as an |
---|
292 | initial count. If a binary semaphore is created with a count of |
---|
293 | zero (0) to indicate that it has been allocated, then the task |
---|
294 | creating the semaphore is considered the current holder of the |
---|
295 | semaphore. At create time the method for ordering waiting tasks |
---|
296 | in the semaphore's task wait queue (by FIFO or task priority) is |
---|
297 | specified. Additionally, the priority inheritance or priority |
---|
298 | ceiling algorithm may be selected for local, binary semaphores |
---|
299 | that use the priority task wait queue blocking discipline. If |
---|
300 | the priority ceiling algorithm is selected, then the highest |
---|
301 | priority of any task which will attempt to obtain this semaphore |
---|
302 | must be specified. RTEMS allocates a Semaphore Control Block |
---|
303 | (SMCB) from the SMCB free list. This data structure is used by |
---|
304 | RTEMS to manage the newly created semaphore. Also, a unique |
---|
305 | semaphore ID is generated and returned to the calling task. |
---|
306 | |
---|
307 | @subsection Obtaining Semaphore IDs |
---|
308 | |
---|
309 | When a semaphore is created, RTEMS generates a unique |
---|
310 | semaphore ID and assigns it to the created semaphore until it is |
---|
311 | deleted. The semaphore ID may be obtained by either of two |
---|
312 | methods. First, as the result of an invocation of the |
---|
313 | @code{@value{DIRPREFIX}semaphore_create} directive, the |
---|
314 | semaphore ID is stored in a user provided location. Second, |
---|
315 | the semaphore ID may be obtained later using the |
---|
316 | @code{@value{DIRPREFIX}semaphore_ident} directive. The semaphore ID is |
---|
317 | used by other semaphore manager directives to access this |
---|
318 | semaphore. |
---|
319 | |
---|
320 | @subsection Acquiring a Semaphore |
---|
321 | |
---|
322 | The @code{@value{DIRPREFIX}semaphore_obtain} directive is used to acquire the |
---|
323 | specified semaphore. A simplified version of the |
---|
324 | @code{@value{DIRPREFIX}semaphore_obtain} directive can be described as follows: |
---|
325 | |
---|
326 | @example |
---|
327 | if semaphore's count is greater than zero |
---|
328 | then decrement semaphore's count |
---|
329 | else wait for release of semaphore |
---|
330 | |
---|
331 | return SUCCESSFUL |
---|
332 | @end example |
---|
333 | |
---|
334 | When the semaphore cannot be immediately acquired, |
---|
335 | one of the following situations applies: |
---|
336 | |
---|
337 | @itemize @bullet |
---|
338 | @item By default, the calling task will wait forever to |
---|
339 | acquire the semaphore. |
---|
340 | |
---|
341 | @item Specifying @code{@value{RPREFIX}NO_WAIT} forces an immediate return |
---|
342 | with an error status code. |
---|
343 | |
---|
344 | @item Specifying a timeout limits the interval the task will |
---|
345 | wait before returning with an error status code. |
---|
346 | @end itemize |
---|
347 | |
---|
348 | If the task waits to acquire the semaphore, then it |
---|
349 | is placed in the semaphore's task wait queue in either FIFO or |
---|
350 | task priority order. If the task blocked waiting for a binary |
---|
351 | semaphore using priority inheritance and the task's priority is |
---|
352 | greater than that of the task currently holding the semaphore, |
---|
353 | then the holding task will inherit the priority of the blocking |
---|
354 | task. All tasks waiting on a semaphore are returned an error |
---|
355 | code when the semaphore is deleted. |
---|
356 | |
---|
357 | When a task successfully obtains a semaphore using |
---|
358 | priority ceiling and the priority ceiling for this semaphore is |
---|
359 | greater than that of the holder, then the holder's priority will |
---|
360 | be elevated. |
---|
361 | |
---|
362 | @subsection Releasing a Semaphore |
---|
363 | |
---|
364 | The @code{@value{DIRPREFIX}semaphore_release} directive is used to release |
---|
365 | the specified semaphore. A simplified version of the |
---|
366 | @code{@value{DIRPREFIX}semaphore_release} directive can be described as |
---|
367 | follows: |
---|
368 | |
---|
369 | @example |
---|
370 | if no tasks are waiting on this semaphore |
---|
371 | then increment semaphore's count |
---|
372 | else assign semaphore to a waiting task |
---|
373 | |
---|
374 | return SUCCESSFUL |
---|
375 | @end example |
---|
376 | |
---|
377 | If this is the outermost release of a binary |
---|
378 | semaphore that uses priority inheritance or priority ceiling and |
---|
379 | the task does not currently hold any other binary semaphores, |
---|
380 | then the task performing the @code{@value{DIRPREFIX}semaphore_release} |
---|
381 | will have its priority restored to its normal value. |
---|
382 | |
---|
383 | @subsection Deleting a Semaphore |
---|
384 | |
---|
385 | The @code{@value{DIRPREFIX}semaphore_delete} directive removes a semaphore |
---|
386 | from the system and frees its control block. A semaphore can be |
---|
387 | deleted by any local task that knows the semaphore's ID. As a |
---|
388 | result of this directive, all tasks blocked waiting to acquire |
---|
389 | the semaphore will be readied and returned a status code which |
---|
390 | indicates that the semaphore was deleted. Any subsequent |
---|
391 | references to the semaphore's name and ID are invalid. |
---|
392 | |
---|
393 | @section Directives |
---|
394 | |
---|
395 | This section details the semaphore manager's |
---|
396 | directives. A subsection is dedicated to each of this manager's |
---|
397 | directives and describes the calling sequence, related |
---|
398 | constants, 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 |
---|
413 | rtems_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 |
---|
425 | procedure 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 | |
---|
448 | This directive creates a semaphore which resides on |
---|
449 | the local node. The created semaphore has the user-defined name |
---|
450 | specified in name and the initial count specified in count. For |
---|
451 | control and maintenance of the semaphore, RTEMS allocates and |
---|
452 | initializes a SMCB. The RTEMS-assigned semaphore id is returned |
---|
453 | in id. This semaphore id is used with other semaphore related |
---|
454 | directives to access the semaphore. |
---|
455 | |
---|
456 | Specifying PRIORITY in attribute_set causes tasks |
---|
457 | waiting for a semaphore to be serviced according to task |
---|
458 | priority. When FIFO is selected, tasks are serviced in First |
---|
459 | In-First Out order. |
---|
460 | |
---|
461 | @subheading NOTES: |
---|
462 | |
---|
463 | This directive will not cause the calling task to be |
---|
464 | preempted. |
---|
465 | |
---|
466 | The priority inheritance and priority ceiling |
---|
467 | algorithms are only supported for local, binary semaphores that |
---|
468 | use the priority task wait queue blocking discipline. |
---|
469 | |
---|
470 | The following semaphore attribute constants are |
---|
471 | defined 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 |
---|
479 | 0 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 |
---|
485 | 0 and 1, block on nested access, allow deletion of locked semaphore. |
---|
486 | |
---|
487 | @item @code{@value{RPREFIX}NO_INHERIT_PRIORITY} - do not use priority |
---|
488 | inheritance (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 |
---|
495 | ceiling (default) |
---|
496 | |
---|
497 | @item @code{@value{RPREFIX}LOCAL} - local semaphore (default) |
---|
498 | |
---|
499 | @item @code{@value{RPREFIX}GLOBAL} - global semaphore |
---|
500 | @end itemize |
---|
501 | |
---|
502 | Semaphores should not be made global unless remote |
---|
503 | tasks must interact with the created semaphore. This is to |
---|
504 | avoid the system overhead incurred by the creation of a global |
---|
505 | semaphore. When a global semaphore is created, the semaphore's |
---|
506 | name and id must be transmitted to every node in the system for |
---|
507 | insertion in the local copy of the global object table. |
---|
508 | |
---|
509 | Note that some combinations of attributes are not valid. See the |
---|
510 | earlier discussion on this. |
---|
511 | |
---|
512 | The total number of global objects, including semaphores, is limited by |
---|
513 | the 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 |
---|
529 | rtems_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 |
---|
539 | procedure 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 | |
---|
555 | This directive obtains the semaphore id associated |
---|
556 | with the semaphore name. If the semaphore name is not unique, |
---|
557 | then the semaphore id will match one of the semaphores with that |
---|
558 | name. However, this semaphore id is not guaranteed to |
---|
559 | correspond to the desired semaphore. The semaphore id is used |
---|
560 | by other semaphore related directives to access the semaphore. |
---|
561 | |
---|
562 | @subheading NOTES: |
---|
563 | |
---|
564 | This directive will not cause the running task to be |
---|
565 | preempted. |
---|
566 | |
---|
567 | If node is @code{@value{RPREFIX}SEARCH_ALL_NODES}, all nodes are searched |
---|
568 | with the local node being searched first. All other nodes are |
---|
569 | searched with the lowest numbered node searched first. |
---|
570 | |
---|
571 | If node is a valid node number which does not |
---|
572 | represent the local node, then only the semaphores exported by |
---|
573 | the designated node are searched. |
---|
574 | |
---|
575 | This directive does not generate activity on remote |
---|
576 | nodes. It accesses only the local copy of the global object |
---|
577 | table. |
---|
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 |
---|
592 | rtems_status_code rtems_semaphore_delete( |
---|
593 | rtems_id id |
---|
594 | ); |
---|
595 | @end example |
---|
596 | @end ifset |
---|
597 | |
---|
598 | @ifset is-Ada |
---|
599 | @example |
---|
600 | procedure 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 | |
---|
615 | This directive deletes the semaphore specified by @code{id}. |
---|
616 | All tasks blocked waiting to acquire the semaphore will be |
---|
617 | readied and returned a status code which indicates that the |
---|
618 | semaphore was deleted. The SMCB for this semaphore is reclaimed |
---|
619 | by RTEMS. |
---|
620 | |
---|
621 | @subheading NOTES: |
---|
622 | |
---|
623 | The calling task will be preempted if it is enabled |
---|
624 | by the task's execution mode and a higher priority local task is |
---|
625 | waiting on the deleted semaphore. The calling task will NOT be |
---|
626 | preempted if all of the tasks that are waiting on the semaphore |
---|
627 | are remote tasks. |
---|
628 | |
---|
629 | The calling task does not have to be the task that |
---|
630 | created the semaphore. Any local task that knows the semaphore |
---|
631 | id can delete the semaphore. |
---|
632 | |
---|
633 | When a global semaphore is deleted, the semaphore id |
---|
634 | must be transmitted to every node in the system for deletion |
---|
635 | from the local copy of the global object table. |
---|
636 | |
---|
637 | The semaphore must reside on the local node, even if |
---|
638 | the semaphore was created with the @code{@value{RPREFIX}GLOBAL} option. |
---|
639 | |
---|
640 | Proxies, used to represent remote tasks, are |
---|
641 | reclaimed 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 |
---|
657 | rtems_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 |
---|
667 | procedure 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 | |
---|
685 | This directive acquires the semaphore specified by |
---|
686 | id. The @code{@value{RPREFIX}WAIT} and @code{@value{RPREFIX}NO_WAIT} components of the options parameter |
---|
687 | indicate whether the calling task wants to wait for the |
---|
688 | semaphore to become available or return immediately if the |
---|
689 | semaphore 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 |
---|
691 | decremented by one and the semaphore is successfully acquired by |
---|
692 | returning immediately with a successful return code. |
---|
693 | |
---|
694 | If the calling task chooses to return immediately and the current |
---|
695 | semaphore count is zero or negative, then a status code is returned |
---|
696 | indicating that the semaphore is not available. If the calling task |
---|
697 | chooses to wait for a semaphore and the current semaphore count is zero or |
---|
698 | negative, then it is decremented by one and the calling task is placed on |
---|
699 | the semaphore's wait queue and blocked. If the semaphore was created with |
---|
700 | the @code{@value{RPREFIX}PRIORITY} attribute, then the calling task is |
---|
701 | inserted into the queue according to its priority. However, if the |
---|
702 | semaphore was created with the @code{@value{RPREFIX}FIFO} attribute, then |
---|
703 | the calling task is placed at the rear of the wait queue. If the binary |
---|
704 | semaphore was created with the @code{@value{RPREFIX}INHERIT_PRIORITY} |
---|
705 | attribute, then the priority of the task currently holding the binary |
---|
706 | semaphore is guaranteed to be greater than or equal to that of the |
---|
707 | blocking task. If the binary semaphore was created with the |
---|
708 | @code{@value{RPREFIX}PRIORITY_CEILING} attribute, a task successfully |
---|
709 | obtains the semaphore, and the priority of that task is greater than the |
---|
710 | ceiling priority for this semaphore, then the priority of the task |
---|
711 | obtaining the semaphore is elevated to that of the ceiling. |
---|
712 | |
---|
713 | The timeout parameter specifies the maximum interval the calling task is |
---|
714 | willing 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. |
---|
716 | If the semaphore is available or the @code{@value{RPREFIX}NO_WAIT} option |
---|
717 | component is set, then timeout is ignored. |
---|
718 | |
---|
719 | @subheading NOTES: |
---|
720 | The following semaphore acquisition option constants |
---|
721 | are 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 | |
---|
728 | Attempting to obtain a global semaphore which does not reside on the local |
---|
729 | node will generate a request to the remote node to access the semaphore. |
---|
730 | If the semaphore is not available and @code{@value{RPREFIX}NO_WAIT} was |
---|
731 | not specified, then the task must be blocked until the semaphore is |
---|
732 | released. A proxy is allocated on the remote node to represent the task |
---|
733 | until the semaphore is released. |
---|
734 | |
---|
735 | A clock tick is required to support the timeout functionality of |
---|
736 | this 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 |
---|
752 | rtems_status_code rtems_semaphore_release( |
---|
753 | rtems_id id |
---|
754 | ); |
---|
755 | @end example |
---|
756 | @end ifset |
---|
757 | |
---|
758 | @ifset is-Ada |
---|
759 | @example |
---|
760 | procedure 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 | |
---|
774 | This directive releases the semaphore specified by |
---|
775 | id. The semaphore count is incremented by one. If the count is |
---|
776 | zero or negative, then the first task on this semaphore's wait |
---|
777 | queue is removed and unblocked. The unblocked task may preempt |
---|
778 | the running task if the running task's preemption mode is |
---|
779 | enabled and the unblocked task has a higher priority than the |
---|
780 | running task. |
---|
781 | |
---|
782 | @subheading NOTES: |
---|
783 | |
---|
784 | The calling task may be preempted if it causes a |
---|
785 | higher priority task to be made ready for execution. |
---|
786 | |
---|
787 | Releasing a global semaphore which does not reside on |
---|
788 | the local node will generate a request telling the remote node |
---|
789 | to release the semaphore. |
---|
790 | |
---|
791 | If the task to be unblocked resides on a different |
---|
792 | node from the semaphore, then the semaphore allocation is |
---|
793 | forwarded to the appropriate node, the waiting task is |
---|
794 | unblocked, and the proxy used to represent the task is reclaimed. |
---|
795 | |
---|
796 | The outermost release of a local, binary, priority |
---|
797 | inheritance or priority ceiling semaphore may result in the |
---|
798 | calling task having its priority lowered. This will occur if |
---|
799 | the calling task holds no other binary semaphores and it has |
---|
800 | inherited 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 |
---|
816 | rtems_status_code rtems_semaphore_flush( |
---|
817 | rtems_id id |
---|
818 | ); |
---|
819 | @end example |
---|
820 | @end ifset |
---|
821 | |
---|
822 | @ifset is-Ada |
---|
823 | @example |
---|
824 | procedure 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 | |
---|
838 | This directive unblocks all tasks waiting on the semaphore specified by |
---|
839 | id. Since there are tasks blocked on the semaphore, the semaphore's |
---|
840 | count is not changed by this directive and thus is zero before and |
---|
841 | after this directive is executed. Tasks which are unblocked as the |
---|
842 | result of this directive will return from the |
---|
843 | @code{@value{DIRPREFIX}semaphore_obtain} directive with a |
---|
844 | status code of @code{@value{RPREFIX}UNSATISFIED} to indicate |
---|
845 | that the semaphore was not obtained. |
---|
846 | |
---|
847 | This directive may unblock any number of tasks. Any of the unblocked |
---|
848 | tasks may preempt the running task if the running task's preemption mode is |
---|
849 | enabled and an unblocked task has a higher priority than the |
---|
850 | running task. |
---|
851 | |
---|
852 | @subheading NOTES: |
---|
853 | |
---|
854 | The calling task may be preempted if it causes a |
---|
855 | higher priority task to be made ready for execution. |
---|
856 | |
---|
857 | If the task to be unblocked resides on a different |
---|
858 | node from the semaphore, then the waiting task is |
---|
859 | unblocked, and the proxy used to represent the task is reclaimed. |
---|
860 | |
---|
861 | |
---|