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