1 | Semaphore Manager |
---|
2 | ################# |
---|
3 | |
---|
4 | .. index:: semaphores |
---|
5 | .. index:: binary semaphores |
---|
6 | .. index:: counting semaphores |
---|
7 | .. index:: mutual exclusion |
---|
8 | |
---|
9 | Introduction |
---|
10 | ============ |
---|
11 | |
---|
12 | The semaphore manager utilizes standard Dijkstra |
---|
13 | counting semaphores to provide synchronization and mutual |
---|
14 | exclusion capabilities. The directives provided by the |
---|
15 | semaphore manager are: |
---|
16 | |
---|
17 | - ``rtems_semaphore_create`` - Create a semaphore |
---|
18 | |
---|
19 | - ``rtems_semaphore_ident`` - Get ID of a semaphore |
---|
20 | |
---|
21 | - ``rtems_semaphore_delete`` - Delete a semaphore |
---|
22 | |
---|
23 | - ``rtems_semaphore_obtain`` - Acquire a semaphore |
---|
24 | |
---|
25 | - ``rtems_semaphore_release`` - Release a semaphore |
---|
26 | |
---|
27 | - ``rtems_semaphore_flush`` - Unblock all tasks waiting on a semaphore |
---|
28 | |
---|
29 | - ``rtems_semaphore_set_priority`` - Set priority by |
---|
30 | scheduler for a semaphore |
---|
31 | |
---|
32 | Background |
---|
33 | ========== |
---|
34 | |
---|
35 | A semaphore can be viewed as a protected variable |
---|
36 | whose value can be modified only with the``rtems_semaphore_create``,``rtems_semaphore_obtain``, and``rtems_semaphore_release`` directives. RTEMS |
---|
37 | supports both binary and counting semaphores. A binary semaphore |
---|
38 | is restricted to values of zero or one, while a counting |
---|
39 | semaphore can assume any non-negative integer value. |
---|
40 | |
---|
41 | A binary semaphore can be used to control access to a |
---|
42 | single resource. In particular, it can be used to enforce |
---|
43 | mutual exclusion for a critical section in user code. In this |
---|
44 | instance, the semaphore would be created with an initial count |
---|
45 | of one to indicate that no task is executing the critical |
---|
46 | section of code. Upon entry to the critical section, a task |
---|
47 | must issue the ``rtems_semaphore_obtain`` |
---|
48 | directive to prevent other tasks from entering the critical section. |
---|
49 | Upon exit from the critical section, the task must issue the``rtems_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 ``rtems_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 ``rtems_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 ``rtems_semaphore_obtain`` |
---|
66 | directive when it reaches a synchronization point. The other task |
---|
67 | performs a corresponding ``rtems_semaphore_release`` |
---|
68 | operation when it reaches its synchronization point, thus unblocking |
---|
69 | the pending task. |
---|
70 | |
---|
71 | Nested Resource Access |
---|
72 | ---------------------- |
---|
73 | |
---|
74 | Deadlock occurs when a task owning a binary semaphore |
---|
75 | attempts to acquire that same semaphore and blocks as result. |
---|
76 | Since the semaphore is allocated to a task, it cannot be |
---|
77 | deleted. Therefore, the task that currently holds the semaphore |
---|
78 | and is also blocked waiting for that semaphore will never |
---|
79 | execute again. |
---|
80 | |
---|
81 | RTEMS addresses this problem by allowing the task |
---|
82 | holding the binary semaphore to obtain the same binary semaphore |
---|
83 | multiple times in a nested manner. Each``rtems_semaphore_obtain`` must be accompanied with a``rtems_semaphore_release``. The semaphore will |
---|
84 | only be made available for acquisition by other tasks when the |
---|
85 | outermost ``rtems_semaphore_obtain`` is matched with |
---|
86 | a ``rtems_semaphore_release``. |
---|
87 | |
---|
88 | Simple binary semaphores do not allow nested access and so can be used for task synchronization. |
---|
89 | |
---|
90 | Priority Inversion |
---|
91 | ------------------ |
---|
92 | |
---|
93 | Priority inversion is a form of indefinite |
---|
94 | postponement which is common in multitasking, preemptive |
---|
95 | executives with shared resources. Priority inversion occurs |
---|
96 | when a high priority tasks requests access to shared resource |
---|
97 | which is currently allocated to low priority task. The high |
---|
98 | priority task must block until the low priority task releases |
---|
99 | the resource. This problem is exacerbated when the low priority |
---|
100 | task is prevented from executing by one or more medium priority |
---|
101 | tasks. Because the low priority task is not executing, it |
---|
102 | cannot complete its interaction with the resource and release |
---|
103 | that resource. The high priority task is effectively prevented |
---|
104 | from executing by lower priority tasks. |
---|
105 | |
---|
106 | |
---|
107 | Priority Inheritance |
---|
108 | -------------------- |
---|
109 | |
---|
110 | Priority inheritance is an algorithm that calls for |
---|
111 | the lower priority task holding a resource to have its priority |
---|
112 | increased to that of the highest priority task blocked waiting |
---|
113 | for that resource. Each time a task blocks attempting to obtain |
---|
114 | the resource, the task holding the resource may have its |
---|
115 | priority increased. |
---|
116 | |
---|
117 | On SMP configurations, in case the task holding the resource and the task that |
---|
118 | blocks attempting to obtain the resource are in different scheduler instances, |
---|
119 | the priority of the holder is raised to the pseudo-interrupt priority (priority |
---|
120 | boosting). The pseudo-interrupt priority is the highest priority. |
---|
121 | |
---|
122 | RTEMS supports priority inheritance for local, binary |
---|
123 | semaphores that use the priority task wait queue blocking |
---|
124 | discipline. When a task of higher priority than the task |
---|
125 | holding the semaphore blocks, the priority of the task holding |
---|
126 | the semaphore is increased to that of the blocking task. When |
---|
127 | the task holding the task completely releases the binary |
---|
128 | semaphore (i.e. not for a nested release), the holderâs priority |
---|
129 | is restored to the value it had before any higher priority was |
---|
130 | inherited. |
---|
131 | |
---|
132 | The RTEMS implementation of the priority inheritance |
---|
133 | algorithm takes into account the scenario in which a task holds |
---|
134 | more than one binary semaphore. The holding task will execute |
---|
135 | at the priority of the higher of the highest ceiling priority or |
---|
136 | at the priority of the highest priority task blocked waiting for |
---|
137 | any of the semaphores the task holds. Only when the task |
---|
138 | releases ALL of the binary semaphores it holds will its priority |
---|
139 | be restored to the normal value. |
---|
140 | |
---|
141 | Priority Ceiling |
---|
142 | ---------------- |
---|
143 | |
---|
144 | Priority ceiling is an algorithm that calls for the |
---|
145 | lower priority task holding a resource to have its priority |
---|
146 | increased to that of the highest priority task which will EVER |
---|
147 | block waiting for that resource. This algorithm addresses the |
---|
148 | problem of priority inversion although it avoids the possibility |
---|
149 | of changing the priority of the task holding the resource |
---|
150 | multiple times. The priority ceiling algorithm will only change |
---|
151 | the priority of the task holding the resource a maximum of one |
---|
152 | time. The ceiling priority is set at creation time and must be |
---|
153 | the priority of the highest priority task which will ever |
---|
154 | attempt to acquire that semaphore. |
---|
155 | |
---|
156 | RTEMS supports priority ceiling for local, binary |
---|
157 | semaphores that use the priority task wait queue blocking |
---|
158 | discipline. When a task of lower priority than the ceiling |
---|
159 | priority successfully obtains the semaphore, its priority is |
---|
160 | raised to the ceiling priority. When the task holding the task |
---|
161 | completely releases the binary semaphore (i.e. not for a nested |
---|
162 | release), the holderâs priority is restored to the value it had |
---|
163 | before any higher priority was put into effect. |
---|
164 | |
---|
165 | The need to identify the highest priority task which |
---|
166 | will attempt to obtain a particular semaphore can be a difficult |
---|
167 | task in a large, complicated system. Although the priority |
---|
168 | ceiling algorithm is more efficient than the priority |
---|
169 | inheritance algorithm with respect to the maximum number of task |
---|
170 | priority changes which may occur while a task holds a particular |
---|
171 | semaphore, the priority inheritance algorithm is more forgiving |
---|
172 | in that it does not require this apriori information. |
---|
173 | |
---|
174 | The RTEMS implementation of the priority ceiling |
---|
175 | algorithm takes into account the scenario in which a task holds |
---|
176 | more than one binary semaphore. The holding task will execute |
---|
177 | at the priority of the higher of the highest ceiling priority or |
---|
178 | at the priority of the highest priority task blocked waiting for |
---|
179 | any of the semaphores the task holds. Only when the task |
---|
180 | releases ALL of the binary semaphores it holds will its priority |
---|
181 | be restored to the normal value. |
---|
182 | |
---|
183 | |
---|
184 | Multiprocessor Resource Sharing Protocol |
---|
185 | ---------------------------------------- |
---|
186 | |
---|
187 | The Multiprocessor Resource Sharing Protocol (MrsP) is defined in *A. |
---|
188 | Burns and A.J. Wellings, A Schedulability Compatible Multiprocessor Resource |
---|
189 | Sharing Protocol - MrsP, Proceedings of the 25th Euromicro Conference on |
---|
190 | Real-Time Systems (ECRTS 2013), July 2013*. It is a generalization of the |
---|
191 | Priority Ceiling Protocol to SMP systems. Each MrsP semaphore uses a ceiling |
---|
192 | priority per scheduler instance. These ceiling priorities can be specified |
---|
193 | with ``rtems_semaphore_set_priority()``. A task obtaining or owning a MrsP |
---|
194 | semaphore will execute with the ceiling priority for its scheduler instance as |
---|
195 | specified by the MrsP semaphore object. Tasks waiting to get ownership of a |
---|
196 | MrsP semaphore will not relinquish the processor voluntarily. In case the |
---|
197 | owner of a MrsP semaphore gets preempted it can ask all tasks waiting for this |
---|
198 | semaphore to help out and temporarily borrow the right to execute on one of |
---|
199 | their assigned processors. |
---|
200 | |
---|
201 | Building a Semaphore Attribute Set |
---|
202 | ---------------------------------- |
---|
203 | |
---|
204 | In general, an attribute set is built by a bitwise OR |
---|
205 | of the desired attribute components. The following table lists |
---|
206 | the set of valid semaphore attributes: |
---|
207 | |
---|
208 | - ``RTEMS_FIFO`` - tasks wait by FIFO (default) |
---|
209 | |
---|
210 | - ``RTEMS_PRIORITY`` - tasks wait by priority |
---|
211 | |
---|
212 | - ``RTEMS_BINARY_SEMAPHORE`` - restrict values to |
---|
213 | 0 and 1 |
---|
214 | |
---|
215 | - ``RTEMS_COUNTING_SEMAPHORE`` - no restriction on values |
---|
216 | (default) |
---|
217 | |
---|
218 | - ``RTEMS_SIMPLE_BINARY_SEMAPHORE`` - restrict values to |
---|
219 | 0 and 1, do not allow nested access, allow deletion of locked semaphore. |
---|
220 | |
---|
221 | - ``RTEMS_NO_INHERIT_PRIORITY`` - do not use priority |
---|
222 | inheritance (default) |
---|
223 | |
---|
224 | - ``RTEMS_INHERIT_PRIORITY`` - use priority inheritance |
---|
225 | |
---|
226 | - ``RTEMS_NO_PRIORITY_CEILING`` - do not use priority |
---|
227 | ceiling (default) |
---|
228 | |
---|
229 | - ``RTEMS_PRIORITY_CEILING`` - use priority ceiling |
---|
230 | |
---|
231 | - ``RTEMS_NO_MULTIPROCESSOR_RESOURCE_SHARING`` - do not use |
---|
232 | Multiprocessor Resource Sharing Protocol (default) |
---|
233 | |
---|
234 | - ``RTEMS_MULTIPROCESSOR_RESOURCE_SHARING`` - use |
---|
235 | Multiprocessor Resource Sharing Protocol |
---|
236 | |
---|
237 | - ``RTEMS_LOCAL`` - local semaphore (default) |
---|
238 | |
---|
239 | - ``RTEMS_GLOBAL`` - global semaphore |
---|
240 | |
---|
241 | Attribute values are specifically designed to be |
---|
242 | mutually exclusive, therefore bitwise OR and addition operations |
---|
243 | are equivalent as long as each attribute appears exactly once in |
---|
244 | the component list. An attribute listed as a default is not |
---|
245 | required to appear in the attribute list, although it is a good |
---|
246 | programming practice to specify default attributes. If all |
---|
247 | defaults are desired, the attribute``RTEMS_DEFAULT_ATTRIBUTES`` should be |
---|
248 | specified on this call. |
---|
249 | |
---|
250 | This example demonstrates the attribute_set parameter needed to create a |
---|
251 | local semaphore with the task priority waiting queue discipline. The |
---|
252 | attribute_set parameter passed to the``rtems_semaphore_create`` directive could be either``RTEMS_PRIORITY`` or ``RTEMS_LOCAL | |
---|
253 | RTEMS_PRIORITY``. The attribute_set parameter can be set to``RTEMS_PRIORITY`` because ``RTEMS_LOCAL`` is the |
---|
254 | default for all created tasks. If a similar semaphore were to be known |
---|
255 | globally, then the attribute_set parameter would be``RTEMS_GLOBAL | RTEMS_PRIORITY``. |
---|
256 | |
---|
257 | Some combinatinos of these attributes are invalid. For example, priority |
---|
258 | ordered blocking discipline must be applied to a binary semaphore in order |
---|
259 | to use either the priority inheritance or priority ceiling functionality. |
---|
260 | The following tree figure illustrates the valid combinations. |
---|
261 | |
---|
262 | .. code:: c |
---|
263 | |
---|
264 | Not available in ASCII representation |
---|
265 | |
---|
266 | Building a SEMAPHORE_OBTAIN Option Set |
---|
267 | -------------------------------------- |
---|
268 | |
---|
269 | In general, an option is built by a bitwise OR of the |
---|
270 | desired option components. The set of valid options for the``rtems_semaphore_obtain`` directive are listed |
---|
271 | in the following table: |
---|
272 | |
---|
273 | - ``RTEMS_WAIT`` - task will wait for semaphore (default) |
---|
274 | |
---|
275 | - ``RTEMS_NO_WAIT`` - task should not wait |
---|
276 | |
---|
277 | Option values are specifically designed to be mutually exclusive, |
---|
278 | therefore bitwise OR and addition operations are equivalent as long as |
---|
279 | each attribute appears exactly once in the component list. An option |
---|
280 | listed as a default is not required to appear in the list, although it is |
---|
281 | a good programming practice to specify default options. If all defaults |
---|
282 | are desired, the option ``RTEMS_DEFAULT_OPTIONS`` should be |
---|
283 | specified on this call. |
---|
284 | |
---|
285 | This example demonstrates the option parameter needed |
---|
286 | to poll for a semaphore. The option parameter passed to the``rtems_semaphore_obtain`` |
---|
287 | directive should be ``RTEMS_NO_WAIT``. |
---|
288 | |
---|
289 | Operations |
---|
290 | ========== |
---|
291 | |
---|
292 | Creating a Semaphore |
---|
293 | -------------------- |
---|
294 | |
---|
295 | The ``rtems_semaphore_create`` directive creates a binary or |
---|
296 | counting semaphore with a user-specified name as well as an |
---|
297 | initial count. If a binary semaphore is created with a count of |
---|
298 | zero (0) to indicate that it has been allocated, then the task |
---|
299 | creating the semaphore is considered the current holder of the |
---|
300 | semaphore. At create time the method for ordering waiting tasks |
---|
301 | in the semaphoreâs task wait queue (by FIFO or task priority) is |
---|
302 | specified. Additionally, the priority inheritance or priority |
---|
303 | ceiling algorithm may be selected for local, binary semaphores |
---|
304 | that use the priority task wait queue blocking discipline. If |
---|
305 | the priority ceiling algorithm is selected, then the highest |
---|
306 | priority of any task which will attempt to obtain this semaphore |
---|
307 | must be specified. RTEMS allocates a Semaphore Control Block |
---|
308 | (SMCB) from the SMCB free list. This data structure is used by |
---|
309 | RTEMS to manage the newly created semaphore. Also, a unique |
---|
310 | semaphore ID is generated and returned to the calling task. |
---|
311 | |
---|
312 | Obtaining Semaphore IDs |
---|
313 | ----------------------- |
---|
314 | |
---|
315 | When a semaphore is created, RTEMS generates a unique |
---|
316 | semaphore ID and assigns it to the created semaphore until it is |
---|
317 | deleted. The semaphore ID may be obtained by either of two |
---|
318 | methods. First, as the result of an invocation of the``rtems_semaphore_create`` directive, the |
---|
319 | semaphore ID is stored in a user provided location. Second, |
---|
320 | the semaphore ID may be obtained later using the``rtems_semaphore_ident`` directive. The semaphore ID is |
---|
321 | used by other semaphore manager directives to access this |
---|
322 | semaphore. |
---|
323 | |
---|
324 | Acquiring a Semaphore |
---|
325 | --------------------- |
---|
326 | |
---|
327 | The ``rtems_semaphore_obtain`` directive is used to acquire the |
---|
328 | specified semaphore. A simplified version of the``rtems_semaphore_obtain`` directive can be described as follows: |
---|
329 | .. code:: c |
---|
330 | |
---|
331 | if semaphore's count is greater than zero |
---|
332 | then decrement semaphore's count |
---|
333 | else wait for release of semaphore |
---|
334 | return SUCCESSFUL |
---|
335 | |
---|
336 | When the semaphore cannot be immediately acquired, |
---|
337 | one of the following situations applies: |
---|
338 | |
---|
339 | - By default, the calling task will wait forever to |
---|
340 | acquire the semaphore. |
---|
341 | |
---|
342 | - Specifying ``RTEMS_NO_WAIT`` forces an immediate return |
---|
343 | with an error status code. |
---|
344 | |
---|
345 | - Specifying a timeout limits the interval the task will |
---|
346 | wait before returning with an error status code. |
---|
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 | Releasing a Semaphore |
---|
363 | --------------------- |
---|
364 | |
---|
365 | The ``rtems_semaphore_release`` directive is used to release |
---|
366 | the specified semaphore. A simplified version of the``rtems_semaphore_release`` directive can be described as |
---|
367 | follows: |
---|
368 | .. code:: c |
---|
369 | |
---|
370 | if no tasks are waiting on this semaphore |
---|
371 | then increment semaphore's count |
---|
372 | else assign semaphore to a waiting task |
---|
373 | return SUCCESSFUL |
---|
374 | |
---|
375 | If this is the outermost release of a binary |
---|
376 | semaphore that uses priority inheritance or priority ceiling and |
---|
377 | the task does not currently hold any other binary semaphores, |
---|
378 | then the task performing the ``rtems_semaphore_release`` |
---|
379 | will have its priority restored to its normal value. |
---|
380 | |
---|
381 | Deleting a Semaphore |
---|
382 | -------------------- |
---|
383 | |
---|
384 | The ``rtems_semaphore_delete`` directive removes a semaphore |
---|
385 | from the system and frees its control block. A semaphore can be |
---|
386 | deleted by any local task that knows the semaphoreâs ID. As a |
---|
387 | result of this directive, all tasks blocked waiting to acquire |
---|
388 | the semaphore will be readied and returned a status code which |
---|
389 | indicates that the semaphore was deleted. Any subsequent |
---|
390 | references to the semaphoreâs name and ID are invalid. |
---|
391 | |
---|
392 | Directives |
---|
393 | ========== |
---|
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 | SEMAPHORE_CREATE - Create a semaphore |
---|
401 | ------------------------------------- |
---|
402 | .. index:: create a semaphore |
---|
403 | |
---|
404 | **CALLING SEQUENCE:** |
---|
405 | |
---|
406 | .. index:: rtems_semaphore_create |
---|
407 | |
---|
408 | .. code:: c |
---|
409 | |
---|
410 | rtems_status_code rtems_semaphore_create( |
---|
411 | rtems_name name, |
---|
412 | uint32_t count, |
---|
413 | rtems_attribute attribute_set, |
---|
414 | rtems_task_priority priority_ceiling, |
---|
415 | rtems_id \*id |
---|
416 | ); |
---|
417 | |
---|
418 | **DIRECTIVE STATUS CODES:** |
---|
419 | |
---|
420 | ``RTEMS_SUCCESSFUL`` - semaphore created successfully |
---|
421 | ``RTEMS_INVALID_NAME`` - invalid semaphore name |
---|
422 | ``RTEMS_INVALID_ADDRESS`` - ``id`` is NULL |
---|
423 | ``RTEMS_TOO_MANY`` - too many semaphores created |
---|
424 | ``RTEMS_NOT_DEFINED`` - invalid attribute set |
---|
425 | ``RTEMS_INVALID_NUMBER`` - invalid starting count for binary semaphore |
---|
426 | ``RTEMS_MP_NOT_CONFIGURED`` - multiprocessing not configured |
---|
427 | ``RTEMS_TOO_MANY`` - too many global objects |
---|
428 | |
---|
429 | **DESCRIPTION:** |
---|
430 | |
---|
431 | This directive creates a semaphore which resides on |
---|
432 | the local node. The created semaphore has the user-defined name |
---|
433 | specified in name and the initial count specified in count. For |
---|
434 | control and maintenance of the semaphore, RTEMS allocates and |
---|
435 | initializes a SMCB. The RTEMS-assigned semaphore id is returned |
---|
436 | in id. This semaphore id is used with other semaphore related |
---|
437 | directives to access the semaphore. |
---|
438 | |
---|
439 | Specifying PRIORITY in attribute_set causes tasks |
---|
440 | waiting for a semaphore to be serviced according to task |
---|
441 | priority. When FIFO is selected, tasks are serviced in First |
---|
442 | In-First Out order. |
---|
443 | |
---|
444 | **NOTES:** |
---|
445 | |
---|
446 | This directive will not cause the calling task to be |
---|
447 | preempted. |
---|
448 | |
---|
449 | The priority inheritance and priority ceiling |
---|
450 | algorithms are only supported for local, binary semaphores that |
---|
451 | use the priority task wait queue blocking discipline. |
---|
452 | |
---|
453 | The following semaphore attribute constants are |
---|
454 | defined by RTEMS: |
---|
455 | |
---|
456 | - ``RTEMS_FIFO`` - tasks wait by FIFO (default) |
---|
457 | |
---|
458 | - ``RTEMS_PRIORITY`` - tasks wait by priority |
---|
459 | |
---|
460 | - ``RTEMS_BINARY_SEMAPHORE`` - restrict values to |
---|
461 | 0 and 1 |
---|
462 | |
---|
463 | - ``RTEMS_COUNTING_SEMAPHORE`` - no restriction on values |
---|
464 | (default) |
---|
465 | |
---|
466 | - ``RTEMS_SIMPLE_BINARY_SEMAPHORE`` - restrict values to |
---|
467 | 0 and 1, block on nested access, allow deletion of locked semaphore. |
---|
468 | |
---|
469 | - ``RTEMS_NO_INHERIT_PRIORITY`` - do not use priority |
---|
470 | inheritance (default) |
---|
471 | |
---|
472 | - ``RTEMS_INHERIT_PRIORITY`` - use priority inheritance |
---|
473 | |
---|
474 | - ``RTEMS_NO_PRIORITY_CEILING`` - do not use priority |
---|
475 | ceiling (default) |
---|
476 | |
---|
477 | - ``RTEMS_PRIORITY_CEILING`` - use priority ceiling |
---|
478 | |
---|
479 | - ``RTEMS_NO_MULTIPROCESSOR_RESOURCE_SHARING`` - do not use |
---|
480 | Multiprocessor Resource Sharing Protocol (default) |
---|
481 | |
---|
482 | - ``RTEMS_MULTIPROCESSOR_RESOURCE_SHARING`` - use |
---|
483 | Multiprocessor Resource Sharing Protocol |
---|
484 | |
---|
485 | - ``RTEMS_LOCAL`` - local semaphore (default) |
---|
486 | |
---|
487 | - ``RTEMS_GLOBAL`` - global semaphore |
---|
488 | |
---|
489 | Semaphores should not be made global unless remote |
---|
490 | tasks must interact with the created semaphore. This is to |
---|
491 | avoid the system overhead incurred by the creation of a global |
---|
492 | semaphore. When a global semaphore is created, the semaphoreâs |
---|
493 | name and id must be transmitted to every node in the system for |
---|
494 | insertion in the local copy of the global object table. |
---|
495 | |
---|
496 | Note that some combinations of attributes are not valid. See the |
---|
497 | earlier discussion on this. |
---|
498 | |
---|
499 | The total number of global objects, including semaphores, is limited by |
---|
500 | the maximum_global_objects field in the Configuration Table. |
---|
501 | |
---|
502 | It is not allowed to create an initially locked MrsP semaphore and the``RTEMS_INVALID_NUMBER`` status code will be returned on SMP |
---|
503 | configurations in this case. This prevents lock order reversal problems with |
---|
504 | the allocator mutex. |
---|
505 | |
---|
506 | SEMAPHORE_IDENT - Get ID of a semaphore |
---|
507 | --------------------------------------- |
---|
508 | .. index:: get ID of a semaphore |
---|
509 | .. index:: obtain ID of a semaphore |
---|
510 | |
---|
511 | **CALLING SEQUENCE:** |
---|
512 | |
---|
513 | .. index:: rtems_semaphore_ident |
---|
514 | |
---|
515 | .. code:: c |
---|
516 | |
---|
517 | rtems_status_code rtems_semaphore_ident( |
---|
518 | rtems_name name, |
---|
519 | uint32_t node, |
---|
520 | rtems_id \*id |
---|
521 | ); |
---|
522 | |
---|
523 | **DIRECTIVE STATUS CODES:** |
---|
524 | |
---|
525 | ``RTEMS_SUCCESSFUL`` - semaphore identified successfully |
---|
526 | ``RTEMS_INVALID_NAME`` - semaphore name not found |
---|
527 | ``RTEMS_INVALID_NODE`` - invalid node id |
---|
528 | |
---|
529 | **DESCRIPTION:** |
---|
530 | |
---|
531 | This directive obtains the semaphore id associated |
---|
532 | with the semaphore name. If the semaphore name is not unique, |
---|
533 | then the semaphore id will match one of the semaphores with that |
---|
534 | name. However, this semaphore id is not guaranteed to |
---|
535 | correspond to the desired semaphore. The semaphore id is used |
---|
536 | by other semaphore related directives to access the semaphore. |
---|
537 | |
---|
538 | **NOTES:** |
---|
539 | |
---|
540 | This directive will not cause the running task to be |
---|
541 | preempted. |
---|
542 | |
---|
543 | If node is ``RTEMS_SEARCH_ALL_NODES``, all nodes are searched |
---|
544 | with the local node being searched first. All other nodes are |
---|
545 | searched with the lowest numbered node searched first. |
---|
546 | |
---|
547 | If node is a valid node number which does not |
---|
548 | represent the local node, then only the semaphores exported by |
---|
549 | the designated node are searched. |
---|
550 | |
---|
551 | This directive does not generate activity on remote |
---|
552 | nodes. It accesses only the local copy of the global object |
---|
553 | table. |
---|
554 | |
---|
555 | SEMAPHORE_DELETE - Delete a semaphore |
---|
556 | ------------------------------------- |
---|
557 | .. index:: delete a semaphore |
---|
558 | |
---|
559 | **CALLING SEQUENCE:** |
---|
560 | |
---|
561 | .. index:: rtems_semaphore_delete |
---|
562 | |
---|
563 | .. code:: c |
---|
564 | |
---|
565 | rtems_status_code rtems_semaphore_delete( |
---|
566 | rtems_id id |
---|
567 | ); |
---|
568 | |
---|
569 | **DIRECTIVE STATUS CODES:** |
---|
570 | |
---|
571 | ``RTEMS_SUCCESSFUL`` - semaphore deleted successfully |
---|
572 | ``RTEMS_INVALID_ID`` - invalid semaphore id |
---|
573 | ``RTEMS_RESOURCE_IN_USE`` - binary semaphore is in use |
---|
574 | ``RTEMS_ILLEGAL_ON_REMOTE_OBJECT`` - cannot delete remote semaphore |
---|
575 | |
---|
576 | **DESCRIPTION:** |
---|
577 | |
---|
578 | This directive deletes the semaphore specified by ``id``. |
---|
579 | All tasks blocked waiting to acquire the semaphore will be |
---|
580 | readied and returned a status code which indicates that the |
---|
581 | semaphore was deleted. The SMCB for this semaphore is reclaimed |
---|
582 | by RTEMS. |
---|
583 | |
---|
584 | **NOTES:** |
---|
585 | |
---|
586 | The calling task will be preempted if it is enabled |
---|
587 | by the taskâs execution mode and a higher priority local task is |
---|
588 | waiting on the deleted semaphore. The calling task will NOT be |
---|
589 | preempted if all of the tasks that are waiting on the semaphore |
---|
590 | are remote tasks. |
---|
591 | |
---|
592 | The calling task does not have to be the task that |
---|
593 | created the semaphore. Any local task that knows the semaphore |
---|
594 | id can delete the semaphore. |
---|
595 | |
---|
596 | When a global semaphore is deleted, the semaphore id |
---|
597 | must be transmitted to every node in the system for deletion |
---|
598 | from the local copy of the global object table. |
---|
599 | |
---|
600 | The semaphore must reside on the local node, even if |
---|
601 | the semaphore was created with the ``RTEMS_GLOBAL`` option. |
---|
602 | |
---|
603 | Proxies, used to represent remote tasks, are |
---|
604 | reclaimed when the semaphore is deleted. |
---|
605 | |
---|
606 | SEMAPHORE_OBTAIN - Acquire a semaphore |
---|
607 | -------------------------------------- |
---|
608 | .. index:: obtain a semaphore |
---|
609 | .. index:: lock a semaphore |
---|
610 | |
---|
611 | **CALLING SEQUENCE:** |
---|
612 | |
---|
613 | .. index:: rtems_semaphore_obtain |
---|
614 | |
---|
615 | .. code:: c |
---|
616 | |
---|
617 | rtems_status_code rtems_semaphore_obtain( |
---|
618 | rtems_id id, |
---|
619 | rtems_option option_set, |
---|
620 | rtems_interval timeout |
---|
621 | ); |
---|
622 | |
---|
623 | **DIRECTIVE STATUS CODES:** |
---|
624 | |
---|
625 | ``RTEMS_SUCCESSFUL`` - semaphore obtained successfully |
---|
626 | ``RTEMS_UNSATISFIED`` - semaphore not available |
---|
627 | ``RTEMS_TIMEOUT`` - timed out waiting for semaphore |
---|
628 | ``RTEMS_OBJECT_WAS_DELETED`` - semaphore deleted while waiting |
---|
629 | ``RTEMS_INVALID_ID`` - invalid semaphore id |
---|
630 | |
---|
631 | **DESCRIPTION:** |
---|
632 | |
---|
633 | This directive acquires the semaphore specified by |
---|
634 | id. The ``RTEMS_WAIT`` and ``RTEMS_NO_WAIT`` components of the options parameter |
---|
635 | indicate whether the calling task wants to wait for the |
---|
636 | semaphore to become available or return immediately if the |
---|
637 | semaphore is not currently available. With either ``RTEMS_WAIT`` or``RTEMS_NO_WAIT``, if the current semaphore count is positive, then it is |
---|
638 | decremented by one and the semaphore is successfully acquired by |
---|
639 | returning immediately with a successful return code. |
---|
640 | |
---|
641 | If the calling task chooses to return immediately and the current |
---|
642 | semaphore count is zero or negative, then a status code is returned |
---|
643 | indicating that the semaphore is not available. If the calling task |
---|
644 | chooses to wait for a semaphore and the current semaphore count is zero or |
---|
645 | negative, then it is decremented by one and the calling task is placed on |
---|
646 | the semaphoreâs wait queue and blocked. If the semaphore was created with |
---|
647 | the ``RTEMS_PRIORITY`` attribute, then the calling task is |
---|
648 | inserted into the queue according to its priority. However, if the |
---|
649 | semaphore was created with the ``RTEMS_FIFO`` attribute, then |
---|
650 | the calling task is placed at the rear of the wait queue. If the binary |
---|
651 | semaphore was created with the ``RTEMS_INHERIT_PRIORITY`` |
---|
652 | attribute, then the priority of the task currently holding the binary |
---|
653 | semaphore is guaranteed to be greater than or equal to that of the |
---|
654 | blocking task. If the binary semaphore was created with the``RTEMS_PRIORITY_CEILING`` attribute, a task successfully |
---|
655 | obtains the semaphore, and the priority of that task is greater than the |
---|
656 | ceiling priority for this semaphore, then the priority of the task |
---|
657 | obtaining the semaphore is elevated to that of the ceiling. |
---|
658 | |
---|
659 | The timeout parameter specifies the maximum interval the calling task is |
---|
660 | willing to be blocked waiting for the semaphore. If it is set to``RTEMS_NO_TIMEOUT``, then the calling task will wait forever. |
---|
661 | If the semaphore is available or the ``RTEMS_NO_WAIT`` option |
---|
662 | component is set, then timeout is ignored. |
---|
663 | |
---|
664 | Deadlock situations are detected for MrsP semaphores and the``RTEMS_UNSATISFIED`` status code will be returned on SMP |
---|
665 | configurations in this case. |
---|
666 | |
---|
667 | **NOTES:** |
---|
668 | |
---|
669 | The following semaphore acquisition option constants |
---|
670 | are defined by RTEMS: |
---|
671 | |
---|
672 | - ``RTEMS_WAIT`` - task will wait for semaphore (default) |
---|
673 | |
---|
674 | - ``RTEMS_NO_WAIT`` - task should not wait |
---|
675 | |
---|
676 | Attempting to obtain a global semaphore which does not reside on the local |
---|
677 | node will generate a request to the remote node to access the semaphore. |
---|
678 | If the semaphore is not available and ``RTEMS_NO_WAIT`` was |
---|
679 | not specified, then the task must be blocked until the semaphore is |
---|
680 | released. A proxy is allocated on the remote node to represent the task |
---|
681 | until the semaphore is released. |
---|
682 | |
---|
683 | A clock tick is required to support the timeout functionality of |
---|
684 | this directive. |
---|
685 | |
---|
686 | It is not allowed to obtain a MrsP semaphore more than once by one task at a |
---|
687 | time (nested access) and the ``RTEMS_UNSATISFIED`` status code will |
---|
688 | be returned on SMP configurations in this case. |
---|
689 | |
---|
690 | SEMAPHORE_RELEASE - Release a semaphore |
---|
691 | --------------------------------------- |
---|
692 | .. index:: release a semaphore |
---|
693 | .. index:: unlock a semaphore |
---|
694 | |
---|
695 | **CALLING SEQUENCE:** |
---|
696 | |
---|
697 | .. index:: rtems_semaphore_release |
---|
698 | |
---|
699 | .. code:: c |
---|
700 | |
---|
701 | rtems_status_code rtems_semaphore_release( |
---|
702 | rtems_id id |
---|
703 | ); |
---|
704 | |
---|
705 | **DIRECTIVE STATUS CODES:** |
---|
706 | |
---|
707 | ``RTEMS_SUCCESSFUL`` - semaphore released successfully |
---|
708 | ``RTEMS_INVALID_ID`` - invalid semaphore id |
---|
709 | ``RTEMS_NOT_OWNER_OF_RESOURCE`` - calling task does not own semaphore |
---|
710 | ``RTEMS_INCORRECT_STATE`` - invalid unlock order |
---|
711 | |
---|
712 | **DESCRIPTION:** |
---|
713 | |
---|
714 | This directive releases the semaphore specified by |
---|
715 | id. The semaphore count is incremented by one. If the count is |
---|
716 | zero or negative, then the first task on this semaphoreâs wait |
---|
717 | queue is removed and unblocked. The unblocked task may preempt |
---|
718 | the running task if the running taskâs preemption mode is |
---|
719 | enabled and the unblocked task has a higher priority than the |
---|
720 | running task. |
---|
721 | |
---|
722 | **NOTES:** |
---|
723 | |
---|
724 | The calling task may be preempted if it causes a |
---|
725 | higher priority task to be made ready for execution. |
---|
726 | |
---|
727 | Releasing a global semaphore which does not reside on |
---|
728 | the local node will generate a request telling the remote node |
---|
729 | to release the semaphore. |
---|
730 | |
---|
731 | If the task to be unblocked resides on a different |
---|
732 | node from the semaphore, then the semaphore allocation is |
---|
733 | forwarded to the appropriate node, the waiting task is |
---|
734 | unblocked, and the proxy used to represent the task is reclaimed. |
---|
735 | |
---|
736 | The outermost release of a local, binary, priority |
---|
737 | inheritance or priority ceiling semaphore may result in the |
---|
738 | calling task having its priority lowered. This will occur if |
---|
739 | the calling task holds no other binary semaphores and it has |
---|
740 | inherited a higher priority. |
---|
741 | |
---|
742 | The MrsP semaphores must be released in the reversed obtain order, otherwise |
---|
743 | the ``RTEMS_INCORRECT_STATE`` status code will be returned on SMP |
---|
744 | configurations in this case. |
---|
745 | |
---|
746 | SEMAPHORE_FLUSH - Unblock all tasks waiting on a semaphore |
---|
747 | ---------------------------------------------------------- |
---|
748 | .. index:: flush a semaphore |
---|
749 | .. index:: unblock all tasks waiting on a semaphore |
---|
750 | |
---|
751 | **CALLING SEQUENCE:** |
---|
752 | |
---|
753 | .. index:: rtems_semaphore_flush |
---|
754 | |
---|
755 | .. code:: c |
---|
756 | |
---|
757 | rtems_status_code rtems_semaphore_flush( |
---|
758 | rtems_id id |
---|
759 | ); |
---|
760 | |
---|
761 | **DIRECTIVE STATUS CODES:** |
---|
762 | |
---|
763 | ``RTEMS_SUCCESSFUL`` - semaphore released successfully |
---|
764 | ``RTEMS_INVALID_ID`` - invalid semaphore id |
---|
765 | ``RTEMS_NOT_DEFINED`` - operation not defined for the protocol of |
---|
766 | the semaphore |
---|
767 | ``RTEMS_ILLEGAL_ON_REMOTE_OBJECT`` - not supported for remote semaphores |
---|
768 | |
---|
769 | **DESCRIPTION:** |
---|
770 | |
---|
771 | This directive unblocks all tasks waiting on the semaphore specified by |
---|
772 | id. Since there are tasks blocked on the semaphore, the semaphoreâs |
---|
773 | count is not changed by this directive and thus is zero before and |
---|
774 | after this directive is executed. Tasks which are unblocked as the |
---|
775 | result of this directive will return from the``rtems_semaphore_obtain`` directive with a |
---|
776 | status code of ``RTEMS_UNSATISFIED`` to indicate |
---|
777 | that the semaphore was not obtained. |
---|
778 | |
---|
779 | This directive may unblock any number of tasks. Any of the unblocked |
---|
780 | tasks may preempt the running task if the running taskâs preemption mode is |
---|
781 | enabled and an unblocked task has a higher priority than the |
---|
782 | running task. |
---|
783 | |
---|
784 | **NOTES:** |
---|
785 | |
---|
786 | The calling task may be preempted if it causes a |
---|
787 | higher priority task to be made ready for execution. |
---|
788 | |
---|
789 | If the task to be unblocked resides on a different |
---|
790 | node from the semaphore, then the waiting task is |
---|
791 | unblocked, and the proxy used to represent the task is reclaimed. |
---|
792 | |
---|
793 | It is not allowed to flush a MrsP semaphore and the``RTEMS_NOT_DEFINED`` status code will be returned on SMP |
---|
794 | configurations in this case. |
---|
795 | |
---|
796 | SEMAPHORE_SET_PRIORITY - Set priority by scheduler for a semaphore |
---|
797 | ------------------------------------------------------------------ |
---|
798 | .. index:: set priority by scheduler for a semaphore |
---|
799 | |
---|
800 | **CALLING SEQUENCE:** |
---|
801 | |
---|
802 | .. index:: rtems_semaphore_set_priority |
---|
803 | |
---|
804 | .. code:: c |
---|
805 | |
---|
806 | rtems_status_code rtems_semaphore_set_priority( |
---|
807 | rtems_id semaphore_id, |
---|
808 | rtems_id scheduler_id, |
---|
809 | rtems_task_priority new_priority, |
---|
810 | rtems_task_priority \*old_priority |
---|
811 | ); |
---|
812 | |
---|
813 | **DIRECTIVE STATUS CODES:** |
---|
814 | |
---|
815 | ``RTEMS_SUCCESSFUL`` - successful operation |
---|
816 | ``RTEMS_INVALID_ID`` - invalid semaphore or scheduler id |
---|
817 | ``RTEMS_INVALID_ADDRESS`` - ``old_priority`` is NULL |
---|
818 | ``RTEMS_INVALID_PRIORITY`` - invalid new priority value |
---|
819 | ``RTEMS_NOT_DEFINED`` - operation not defined for the protocol of |
---|
820 | the semaphore |
---|
821 | ``RTEMS_ILLEGAL_ON_REMOTE_OBJECT`` - not supported for remote semaphores |
---|
822 | |
---|
823 | **DESCRIPTION:** |
---|
824 | |
---|
825 | This directive sets the priority value with respect to the specified scheduler |
---|
826 | of a semaphore. |
---|
827 | |
---|
828 | The special priority value ``RTEMS_CURRENT_PRIORITY`` can be used to get the |
---|
829 | current priority value without changing it. |
---|
830 | |
---|
831 | The interpretation of the priority value depends on the protocol of the |
---|
832 | semaphore object. |
---|
833 | |
---|
834 | - The Multiprocessor Resource Sharing Protocol needs a ceiling priority per |
---|
835 | scheduler instance. This operation can be used to specify these priority |
---|
836 | values. |
---|
837 | |
---|
838 | - For the Priority Ceiling Protocol the ceiling priority is used with this |
---|
839 | operation. |
---|
840 | |
---|
841 | - For other protocols this operation is not defined. |
---|
842 | |
---|
843 | **EXAMPLE:** |
---|
844 | |
---|
845 | .. code:: c |
---|
846 | |
---|
847 | #include <assert.h> |
---|
848 | #include <stdlib.h> |
---|
849 | #include <rtems.h> |
---|
850 | #define SCHED_A rtems_build_name(' ', ' ', ' ', 'A') |
---|
851 | #define SCHED_B rtems_build_name(' ', ' ', ' ', 'B') |
---|
852 | static void Init(rtems_task_argument arg) |
---|
853 | { |
---|
854 | rtems_status_code sc; |
---|
855 | rtems_id semaphore_id; |
---|
856 | rtems_id scheduler_a_id; |
---|
857 | rtems_id scheduler_b_id; |
---|
858 | rtems_task_priority prio; |
---|
859 | /* Get the scheduler identifiers \*/ |
---|
860 | sc = rtems_scheduler_ident(SCHED_A, &scheduler_a_id); |
---|
861 | assert(sc == RTEMS_SUCCESSFUL); |
---|
862 | sc = rtems_scheduler_ident(SCHED_B, &scheduler_b_id); |
---|
863 | assert(sc == RTEMS_SUCCESSFUL); |
---|
864 | /* Create a MrsP semaphore object \*/ |
---|
865 | sc = rtems_semaphore_create( |
---|
866 | rtems_build_name('M', 'R', 'S', 'P'), |
---|
867 | 1, |
---|
868 | RTEMS_MULTIPROCESSOR_RESOURCE_SHARING |
---|
869 | | RTEMS_BINARY_SEMAPHORE, |
---|
870 | 1, |
---|
871 | &semaphore_id |
---|
872 | ); |
---|
873 | assert(sc == RTEMS_SUCCESSFUL); |
---|
874 | /* |
---|
875 | * The ceiling priority values per scheduler are equal to the value specified |
---|
876 | * for object creation. |
---|
877 | \*/ |
---|
878 | prio = RTEMS_CURRENT_PRIORITY; |
---|
879 | sc = rtems_semaphore_set_priority(semaphore_id, scheduler_a_id, prio, &prio); |
---|
880 | assert(sc == RTEMS_SUCCESSFUL); |
---|
881 | assert(prio == 1); |
---|
882 | /* Check the old value and set a new ceiling priority for scheduler B \*/ |
---|
883 | prio = 2; |
---|
884 | sc = rtems_semaphore_set_priority(semaphore_id, scheduler_b_id, prio, &prio); |
---|
885 | assert(sc == RTEMS_SUCCESSFUL); |
---|
886 | assert(prio == 1); |
---|
887 | /* Check the ceiling priority values \*/ |
---|
888 | prio = RTEMS_CURRENT_PRIORITY; |
---|
889 | sc = rtems_semaphore_set_priority(semaphore_id, scheduler_a_id, prio, &prio); |
---|
890 | assert(sc == RTEMS_SUCCESSFUL); |
---|
891 | assert(prio == 1); |
---|
892 | prio = RTEMS_CURRENT_PRIORITY; |
---|
893 | sc = rtems_semaphore_set_priority(semaphore_id, scheduler_b_id, prio, &prio); |
---|
894 | assert(sc == RTEMS_SUCCESSFUL); |
---|
895 | assert(prio == 2); |
---|
896 | sc = rtems_semaphore_delete(semaphore_id); |
---|
897 | assert(sc == RTEMS_SUCCESSFUL); |
---|
898 | exit(0); |
---|
899 | } |
---|
900 | #define CONFIGURE_SMP_APPLICATION |
---|
901 | #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER |
---|
902 | #define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER |
---|
903 | #define CONFIGURE_MAXIMUM_TASKS 1 |
---|
904 | #define CONFIGURE_MAXIMUM_SEMAPHORES 1 |
---|
905 | #define CONFIGURE_MAXIMUM_MRSP_SEMAPHORES 1 |
---|
906 | #define CONFIGURE_SMP_MAXIMUM_PROCESSORS 2 |
---|
907 | #define CONFIGURE_SCHEDULER_SIMPLE_SMP |
---|
908 | #include <rtems/scheduler.h> |
---|
909 | RTEMS_SCHEDULER_CONTEXT_SIMPLE_SMP(a); |
---|
910 | RTEMS_SCHEDULER_CONTEXT_SIMPLE_SMP(b); |
---|
911 | #define CONFIGURE_SCHEDULER_CONTROLS \\ |
---|
912 | RTEMS_SCHEDULER_CONTROL_SIMPLE_SMP(a, SCHED_A), \\ |
---|
913 | RTEMS_SCHEDULER_CONTROL_SIMPLE_SMP(b, SCHED_B) |
---|
914 | #define CONFIGURE_SMP_SCHEDULER_ASSIGNMENTS \\ |
---|
915 | RTEMS_SCHEDULER_ASSIGN(0, RTEMS_SCHEDULER_ASSIGN_PROCESSOR_MANDATORY), \\ |
---|
916 | RTEMS_SCHEDULER_ASSIGN(1, RTEMS_SCHEDULER_ASSIGN_PROCESSOR_MANDATORY) |
---|
917 | #define CONFIGURE_RTEMS_INIT_TASKS_TABLE |
---|
918 | #define CONFIGURE_INIT |
---|
919 | #include <rtems/confdefs.h> |
---|
920 | |
---|
921 | .. COMMENT: COPYRIGHT (c) 1988-2007. |
---|
922 | |
---|
923 | .. COMMENT: On-Line Applications Research Corporation (OAR). |
---|
924 | |
---|
925 | .. COMMENT: All rights reserved. |
---|
926 | |
---|