1 | .. comment SPDX-License-Identifier: CC-BY-SA-4.0 |
---|
2 | |
---|
3 | .. COMMENT: COPYRIGHT (c) 1988-2008, 2018. |
---|
4 | .. COMMENT: On-Line Applications Research Corporation (OAR). |
---|
5 | .. COMMENT: All rights reserved. |
---|
6 | |
---|
7 | .. index:: barrier |
---|
8 | |
---|
9 | .. _barrier_manager: |
---|
10 | |
---|
11 | Barrier Manager |
---|
12 | *************** |
---|
13 | |
---|
14 | Introduction |
---|
15 | ============ |
---|
16 | |
---|
17 | The barrier manager provides a unique synchronization capability which can be |
---|
18 | used to have a set of tasks block and be unblocked as a set. The directives |
---|
19 | provided by the barrier manager are: |
---|
20 | |
---|
21 | - rtems_barrier_create_ - Create a barrier |
---|
22 | |
---|
23 | - rtems_barrier_ident_ - Get ID of a barrier |
---|
24 | |
---|
25 | - rtems_barrier_delete_ - Delete a barrier |
---|
26 | |
---|
27 | - rtems_barrier_wait_ - Wait at a barrier |
---|
28 | |
---|
29 | - rtems_barrier_release_ - Release a barrier |
---|
30 | |
---|
31 | Background |
---|
32 | ========== |
---|
33 | |
---|
34 | A barrier can be viewed as a gate at which tasks wait until the gate is opened. |
---|
35 | This has many analogies in the real world. Horses and other farm animals may |
---|
36 | approach a closed gate and gather in front of it, waiting for someone to open |
---|
37 | the gate so they may proceed. Similarly, cticket holders gather at the gates |
---|
38 | of arenas before concerts or sporting events waiting for the arena personnel to |
---|
39 | open the gates so they may enter. |
---|
40 | |
---|
41 | Barriers are useful during application initialization. Each application task |
---|
42 | can perform its local initialization before waiting for the application as a |
---|
43 | whole to be initialized. Once all tasks have completed their independent |
---|
44 | initializations, the "application ready" barrier can be released. |
---|
45 | |
---|
46 | Automatic Versus Manual Barriers |
---|
47 | -------------------------------- |
---|
48 | |
---|
49 | Just as with a real-world gate, barriers may be configured to be manually |
---|
50 | opened or automatically opened. All tasks calling the ``rtems_barrier_wait`` |
---|
51 | directive will block until a controlling task invokes |
---|
52 | the ``rtems_barrier_release`` directive. |
---|
53 | |
---|
54 | Automatic barriers are created with a limit to the number of tasks which may |
---|
55 | simultaneously block at the barrier. Once this limit is reached, all of the |
---|
56 | tasks are released. For example, if the automatic limit is ten tasks, then the |
---|
57 | first nine tasks calling the ``rtems_barrier_wait`` directive will block. When |
---|
58 | the tenth task calls the ``rtems_barrier_wait`` directive, the nine blocked |
---|
59 | tasks will be released and the tenth task returns to the caller without |
---|
60 | blocking. |
---|
61 | |
---|
62 | Building a Barrier Attribute Set |
---|
63 | -------------------------------- |
---|
64 | |
---|
65 | In general, an attribute set is built by a bitwise OR of the desired attribute |
---|
66 | components. The following table lists the set of valid barrier attributes: |
---|
67 | |
---|
68 | ``RTEMS_BARRIER_AUTOMATIC_RELEASE`` |
---|
69 | automatically release the barrier when the configured number of tasks are |
---|
70 | blocked |
---|
71 | |
---|
72 | ``RTEMS_BARRIER_MANUAL_RELEASE`` |
---|
73 | only release the barrier when the application invokes the |
---|
74 | ``rtems_barrier_release`` directive. (default) |
---|
75 | |
---|
76 | .. note:: |
---|
77 | |
---|
78 | Barriers only support FIFO blocking order because all waiting tasks are |
---|
79 | released as a set. Thus the released tasks will all become ready to execute |
---|
80 | at the same time and compete for the processor based upon their priority. |
---|
81 | |
---|
82 | Attribute values are specifically designed to be mutually exclusive, therefore |
---|
83 | bitwise OR and addition operations are equivalent as long as each attribute |
---|
84 | appears exactly once in the component list. An attribute listed as a default |
---|
85 | is not required to appear in the attribute list, although it is a good |
---|
86 | programming practice to specify default attributes. If all defaults are |
---|
87 | desired, the attribute ``RTEMS_DEFAULT_ATTRIBUTES`` should be specified on this |
---|
88 | call. |
---|
89 | |
---|
90 | This example demonstrates the attribute_set parameter needed to create a |
---|
91 | barrier with the automatic release policy. The ``attribute_set`` parameter |
---|
92 | passed to the ``rtems_barrier_create`` directive will be |
---|
93 | ``RTEMS_BARRIER_AUTOMATIC_RELEASE``. In this case, the user must also specify |
---|
94 | the ``maximum_waiters`` parameter. |
---|
95 | |
---|
96 | Operations |
---|
97 | ========== |
---|
98 | |
---|
99 | Creating a Barrier |
---|
100 | ------------------ |
---|
101 | |
---|
102 | The ``rtems_barrier_create`` directive creates a barrier with a user-specified |
---|
103 | name and the desired attributes. RTEMS allocates a Barrier Control Block (BCB) |
---|
104 | from the BCB free list. This data structure is used by RTEMS to manage the |
---|
105 | newly created barrier. Also, a unique barrier ID is generated and returned to |
---|
106 | the calling task. |
---|
107 | |
---|
108 | Obtaining Barrier IDs |
---|
109 | --------------------- |
---|
110 | |
---|
111 | When a barrier is created, RTEMS generates a unique barrier ID and assigns it |
---|
112 | to the created barrier until it is deleted. The barrier ID may be obtained by |
---|
113 | either of two methods. First, as the result of an invocation of the |
---|
114 | ``rtems_barrier_create`` directive, the barrier ID is stored in a user provided |
---|
115 | location. Second, the barrier ID may be obtained later using the |
---|
116 | ``rtems_barrier_ident`` directive. The barrier ID is used by other barrier |
---|
117 | manager directives to access this barrier. |
---|
118 | |
---|
119 | Waiting at a Barrier |
---|
120 | -------------------- |
---|
121 | |
---|
122 | The ``rtems_barrier_wait`` directive is used to wait at |
---|
123 | the specified barrier. Since a barrier is, by definition, never immediately, |
---|
124 | the task may wait forever for the barrier to be released or it may |
---|
125 | specify a timeout. Specifying a timeout limits the interval the task will |
---|
126 | wait before returning with an error status code. |
---|
127 | |
---|
128 | If the barrier is configured as automatic and there are already one less then |
---|
129 | the maximum number of waiters, then the call will unblock all tasks waiting at |
---|
130 | the barrier and the caller will return immediately. |
---|
131 | |
---|
132 | When the task does wait to acquire the barrier, then it is placed in the |
---|
133 | barrier's task wait queue in FIFO order. All tasks waiting on a barrier are |
---|
134 | returned an error code when the barrier is deleted. |
---|
135 | |
---|
136 | Releasing a Barrier |
---|
137 | ------------------- |
---|
138 | |
---|
139 | The ``rtems_barrier_release`` directive is used to release the specified |
---|
140 | barrier. When the ``rtems_barrier_release`` is invoked, all tasks waiting at |
---|
141 | the barrier are immediately made ready to execute and begin to compete for the |
---|
142 | processor to execute. |
---|
143 | |
---|
144 | Deleting a Barrier |
---|
145 | ------------------ |
---|
146 | |
---|
147 | The ``rtems_barrier_delete`` directive removes a barrier from the system and |
---|
148 | frees its control block. A barrier can be deleted by any local task that knows |
---|
149 | the barrier's ID. As a result of this directive, all tasks blocked waiting for |
---|
150 | the barrier to be released, will be readied and returned a status code which |
---|
151 | indicates that the barrier was deleted. Any subsequent references to the |
---|
152 | barrier's name and ID are invalid. |
---|
153 | |
---|
154 | Directives |
---|
155 | ========== |
---|
156 | |
---|
157 | This section details the barrier manager's directives. A subsection is |
---|
158 | dedicated to each of this manager's directives and describes the calling |
---|
159 | sequence, related constants, usage, and status codes. |
---|
160 | |
---|
161 | .. raw:: latex |
---|
162 | |
---|
163 | \clearpage |
---|
164 | |
---|
165 | .. _rtems_barrier_create: |
---|
166 | |
---|
167 | .. index:: create a barrier |
---|
168 | .. index:: rtems_barrier_create |
---|
169 | |
---|
170 | BARRIER_CREATE - Create a barrier |
---|
171 | --------------------------------- |
---|
172 | |
---|
173 | CALLING SEQUENCE: |
---|
174 | .. code-block:: c |
---|
175 | |
---|
176 | rtems_status_code rtems_barrier_create( |
---|
177 | rtems_name name, |
---|
178 | rtems_attribute attribute_set, |
---|
179 | uint32_t maximum_waiters, |
---|
180 | rtems_id *id |
---|
181 | ); |
---|
182 | |
---|
183 | DIRECTIVE STATUS CODES: |
---|
184 | .. list-table:: |
---|
185 | :class: rtems-table |
---|
186 | |
---|
187 | * - ``RTEMS_SUCCESSFUL`` |
---|
188 | - barrier created successfully |
---|
189 | * - ``RTEMS_INVALID_NAME`` |
---|
190 | - invalid barrier name |
---|
191 | * - ``RTEMS_INVALID_ADDRESS`` |
---|
192 | - ``id`` is NULL |
---|
193 | * - ``RTEMS_TOO_MANY`` |
---|
194 | - too many barriers created |
---|
195 | |
---|
196 | DESCRIPTION: |
---|
197 | This directive creates a barrier which resides on the local node. The |
---|
198 | created barrier has the user-defined name specified in ``name`` and the |
---|
199 | initial count specified in ``count``. For control and maintenance of the |
---|
200 | barrier, RTEMS allocates and initializes a BCB. The RTEMS-assigned barrier |
---|
201 | id is returned in ``id``. This barrier id is used with other barrier |
---|
202 | related directives to access the barrier. |
---|
203 | |
---|
204 | .. list-table:: |
---|
205 | :class: rtems-table |
---|
206 | |
---|
207 | * - ``RTEMS_BARRIER_MANUAL_RELEASE`` |
---|
208 | - only release |
---|
209 | |
---|
210 | Specifying ``RTEMS_BARRIER_AUTOMATIC_RELEASE`` in ``attribute_set`` causes |
---|
211 | tasks calling the ``rtems_barrier_wait`` directive to block until there are |
---|
212 | ``maximum_waiters - 1`` tasks waiting at the barrier. When the |
---|
213 | ``maximum_waiters`` task invokes the ``rtems_barrier_wait`` directive, the |
---|
214 | previous ``maximum_waiters - 1`` tasks are automatically released and the |
---|
215 | caller returns. |
---|
216 | |
---|
217 | In contrast, when the ``RTEMS_BARRIER_MANUAL_RELEASE`` attribute is |
---|
218 | specified, there is no limit on the number of tasks that will block at the |
---|
219 | barrier. Only when the ``rtems_barrier_release`` directive is invoked, are |
---|
220 | the tasks waiting at the barrier unblocked. |
---|
221 | |
---|
222 | NOTES: |
---|
223 | This directive will not cause the calling task to be preempted. |
---|
224 | |
---|
225 | The following barrier attribute constants are defined by RTEMS: |
---|
226 | |
---|
227 | .. list-table:: |
---|
228 | :class: rtems-table |
---|
229 | |
---|
230 | * - ``RTEMS_BARRIER_AUTOMATIC_RELEASE`` |
---|
231 | - automatically release the barrier when the configured number of tasks are |
---|
232 | blocked |
---|
233 | * - ``RTEMS_BARRIER_MANUAL_RELEASE`` |
---|
234 | - only release the barrier when the application invokes |
---|
235 | the ``rtems_barrier_release`` directive. (default) |
---|
236 | |
---|
237 | .. raw:: latex |
---|
238 | |
---|
239 | \clearpage |
---|
240 | |
---|
241 | .. _rtems_barrier_ident: |
---|
242 | |
---|
243 | .. index:: get ID of a barrier |
---|
244 | .. index:: obtain ID of a barrier |
---|
245 | .. index:: rtems_barrier_ident |
---|
246 | |
---|
247 | BARRIER_IDENT - Get ID of a barrier |
---|
248 | ----------------------------------- |
---|
249 | |
---|
250 | CALLING SEQUENCE: |
---|
251 | .. code-block:: c |
---|
252 | |
---|
253 | rtems_status_code rtems_barrier_ident( |
---|
254 | rtems_name name, |
---|
255 | rtems_id *id |
---|
256 | ); |
---|
257 | |
---|
258 | DIRECTIVE STATUS CODES: |
---|
259 | .. list-table:: |
---|
260 | :class: rtems-table |
---|
261 | |
---|
262 | * - ``RTEMS_SUCCESSFUL`` |
---|
263 | - barrier identified successfully |
---|
264 | * - ``RTEMS_INVALID_NAME`` |
---|
265 | - barrier name not found |
---|
266 | * - ``RTEMS_INVALID_NODE`` |
---|
267 | - invalid node id |
---|
268 | |
---|
269 | DESCRIPTION: |
---|
270 | This directive obtains the barrier id associated with the barrier name. If |
---|
271 | the barrier name is not unique, then the barrier id will match one of the |
---|
272 | barriers with that name. However, this barrier id is not guaranteed to |
---|
273 | correspond to the desired barrier. The barrier id is used by other barrier |
---|
274 | related directives to access the barrier. |
---|
275 | |
---|
276 | NOTES: |
---|
277 | This directive will not cause the running task to be preempted. |
---|
278 | |
---|
279 | .. raw:: latex |
---|
280 | |
---|
281 | \clearpage |
---|
282 | |
---|
283 | .. _rtems_barrier_delete: |
---|
284 | |
---|
285 | .. index:: delete a barrier |
---|
286 | .. index:: rtems_barrier_delete |
---|
287 | |
---|
288 | BARRIER_DELETE - Delete a barrier |
---|
289 | --------------------------------- |
---|
290 | |
---|
291 | CALLING SEQUENCE: |
---|
292 | .. code-block:: c |
---|
293 | |
---|
294 | rtems_status_code rtems_barrier_delete( |
---|
295 | rtems_id id |
---|
296 | ); |
---|
297 | |
---|
298 | DIRECTIVE STATUS CODES: |
---|
299 | .. list-table:: |
---|
300 | :class: rtems-table |
---|
301 | |
---|
302 | * - ``RTEMS_SUCCESSFUL`` |
---|
303 | - barrier deleted successfully |
---|
304 | * - ``RTEMS_INVALID_ID`` |
---|
305 | - invalid barrier id |
---|
306 | |
---|
307 | DESCRIPTION: |
---|
308 | This directive deletes the barrier specified by ``id``. All tasks blocked |
---|
309 | waiting for the barrier to be released will be readied and returned a |
---|
310 | status code which indicates that the barrier was deleted. The BCB for this |
---|
311 | barrier is reclaimed by RTEMS. |
---|
312 | |
---|
313 | NOTES: |
---|
314 | The calling task will be preempted if it is enabled by the task's execution |
---|
315 | mode and a higher priority local task is waiting on the deleted barrier. |
---|
316 | The calling task will NOT be preempted if all of the tasks that are waiting |
---|
317 | on the barrier are remote tasks. |
---|
318 | |
---|
319 | The calling task does not have to be the task that created the barrier. |
---|
320 | Any local task that knows the barrier id can delete the barrier. |
---|
321 | |
---|
322 | .. raw:: latex |
---|
323 | |
---|
324 | \clearpage |
---|
325 | |
---|
326 | .. _rtems_barrier_wait: |
---|
327 | |
---|
328 | .. index:: obtain a barrier |
---|
329 | .. index:: lock a barrier |
---|
330 | .. index:: rtems_barrier_wait |
---|
331 | |
---|
332 | BARRIER_WAIT - Wait at a barrier |
---|
333 | ---------------------------------- |
---|
334 | |
---|
335 | CALLING SEQUENCE: |
---|
336 | .. code-block:: c |
---|
337 | |
---|
338 | rtems_status_code rtems_barrier_wait( |
---|
339 | rtems_id id, |
---|
340 | rtems_interval timeout |
---|
341 | ); |
---|
342 | |
---|
343 | DIRECTIVE STATUS CODES: |
---|
344 | .. list-table:: |
---|
345 | :class: rtems-table |
---|
346 | |
---|
347 | * - ``RTEMS_SUCCESSFUL`` |
---|
348 | - barrier released and task unblocked |
---|
349 | * - ``RTEMS_UNSATISFIED`` |
---|
350 | - barrier not available |
---|
351 | * - ``RTEMS_TIMEOUT`` |
---|
352 | - timed out waiting for barrier |
---|
353 | * - ``RTEMS_OBJECT_WAS_DELETED`` |
---|
354 | - barrier deleted while waiting |
---|
355 | * - ``RTEMS_INVALID_ID`` |
---|
356 | - invalid barrier id |
---|
357 | |
---|
358 | DESCRIPTION: |
---|
359 | |
---|
360 | This directive acquires the barrier specified by ``id``. The |
---|
361 | ``RTEMS_WAIT`` and ``RTEMS_NO_WAIT`` components of the options parameter |
---|
362 | indicate whether the calling task wants to wait for the barrier to become |
---|
363 | available or return immediately if the barrier is not currently available. |
---|
364 | With either ``RTEMS_WAIT`` or ``RTEMS_NO_WAIT``, if the current barrier |
---|
365 | count is positive, then it is decremented by one and the barrier is |
---|
366 | successfully acquired by returning immediately with a successful return |
---|
367 | code. |
---|
368 | |
---|
369 | Conceptually, the calling task should always be thought of as blocking when |
---|
370 | it makes this call and being unblocked when the barrier is released. If |
---|
371 | the barrier is configured for manual release, this rule of thumb will |
---|
372 | always be valid. If the barrier is configured for automatic release, all |
---|
373 | callers will block except for the one which is the Nth task which trips the |
---|
374 | automatic release condition. |
---|
375 | |
---|
376 | The timeout parameter specifies the maximum interval the calling task is |
---|
377 | willing to be blocked waiting for the barrier. If it is set to |
---|
378 | ``RTEMS_NO_TIMEOUT``, then the calling task will wait forever. If the |
---|
379 | barrier is available or the ``RTEMS_NO_WAIT`` option component is set, then |
---|
380 | timeout is ignored. |
---|
381 | |
---|
382 | NOTES: |
---|
383 | |
---|
384 | The following barrier acquisition option constants are defined by RTEMS: |
---|
385 | |
---|
386 | .. list-table:: |
---|
387 | :class: rtems-table |
---|
388 | |
---|
389 | * - ``RTEMS_WAIT`` |
---|
390 | - task will wait for barrier (default) |
---|
391 | * - ``RTEMS_NO_WAIT`` |
---|
392 | - task should not wait |
---|
393 | |
---|
394 | A clock tick is required to support the timeout functionality of this |
---|
395 | directive. |
---|
396 | |
---|
397 | .. raw:: latex |
---|
398 | |
---|
399 | \clearpage |
---|
400 | |
---|
401 | .. _rtems_barrier_release: |
---|
402 | |
---|
403 | .. index:: wait at a barrier |
---|
404 | .. index:: release a barrier |
---|
405 | .. index:: rtems_barrier_release |
---|
406 | |
---|
407 | BARRIER_RELEASE - Release a barrier |
---|
408 | ----------------------------------- |
---|
409 | |
---|
410 | CALLING SEQUENCE: |
---|
411 | .. code-block:: c |
---|
412 | |
---|
413 | rtems_status_code rtems_barrier_release( |
---|
414 | rtems_id id, |
---|
415 | uint32_t *released |
---|
416 | ); |
---|
417 | |
---|
418 | DIRECTIVE STATUS CODES: |
---|
419 | .. list-table:: |
---|
420 | :class: rtems-table |
---|
421 | |
---|
422 | * - ``RTEMS_SUCCESSFUL`` |
---|
423 | - barrier released successfully |
---|
424 | * - ``RTEMS_INVALID_ID`` |
---|
425 | - invalid barrier id |
---|
426 | |
---|
427 | DESCRIPTION: |
---|
428 | This directive releases the barrier specified by id. All tasks waiting at |
---|
429 | the barrier will be unblocked. If the running task's preemption mode is |
---|
430 | enabled and one of the unblocked tasks has a higher priority than the |
---|
431 | running task. |
---|
432 | |
---|
433 | NOTES: |
---|
434 | The calling task may be preempted if it causes a higher priority task to be |
---|
435 | made ready for execution. |
---|