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