source: rtems-docs/c-user/barrier_manager.rst @ a62dfde

5
Last change on this file since a62dfde was a62dfde, checked in by Martin Erik Werner <martinerikwerner@…>, on 10/02/19 at 21:22:25

barrier: Reword "forever" -> "until .. released"

  • Property mode set to 100644
File size: 13.3 KB
Line 
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
10Barrier Manager
11***************
12
13Introduction
14============
15
16The barrier manager provides a unique synchronization capability which can be
17used to have a set of tasks block and be unblocked as a set.  The directives
18provided 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
30Background
31==========
32
33A barrier can be viewed as a gate at which tasks wait until the gate is opened.
34This has many analogies in the real world.  Horses and other farm animals may
35approach a closed gate and gather in front of it, waiting for someone to open
36the gate so they may proceed.  Similarly, ticket holders gather at the gates of
37arenas before concerts or sporting events waiting for the arena personnel to
38open the gates so they may enter.
39
40Barriers are useful during application initialization.  Each application task
41can perform its local initialization before waiting for the application as a
42whole to be initialized.  Once all tasks have completed their independent
43initializations, the "application ready" barrier can be released.
44
45Automatic Versus Manual Barriers
46--------------------------------
47
48Just as with a real-world gate, barriers may be configured to be manually
49opened or automatically opened.  All tasks calling the ``rtems_barrier_wait``
50directive will block until a controlling task invokes
51the ``rtems_barrier_release`` directive.
52
53Automatic barriers are created with a limit to the number of tasks which may
54simultaneously block at the barrier.  Once this limit is reached, all of the
55tasks are released.  For example, if the automatic limit is ten tasks, then the
56first nine tasks calling the ``rtems_barrier_wait`` directive will block.  When
57the tenth task calls the ``rtems_barrier_wait`` directive, the nine blocked
58tasks will be released and the tenth task returns to the caller without
59blocking.
60
61Building a Barrier Attribute Set
62--------------------------------
63
64In general, an attribute set is built by a bitwise OR of the desired attribute
65components.  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
81Attribute values are specifically designed to be mutually exclusive, therefore
82bitwise OR and addition operations are equivalent as long as each attribute
83appears exactly once in the component list.  An attribute listed as a default
84is not required to appear in the attribute list, although it is a good
85programming practice to specify default attributes.  If all defaults are
86desired, the attribute ``RTEMS_DEFAULT_ATTRIBUTES`` should be specified on this
87call.
88
89This example demonstrates the attribute_set parameter needed to create a
90barrier with the automatic release policy.  The ``attribute_set`` parameter
91passed to the ``rtems_barrier_create`` directive will be
92``RTEMS_BARRIER_AUTOMATIC_RELEASE``.  In this case, the user must also specify
93the ``maximum_waiters`` parameter.
94
95Operations
96==========
97
98Creating a Barrier
99------------------
100
101The ``rtems_barrier_create`` directive creates a barrier with a user-specified
102name and the desired attributes.  RTEMS allocates a Barrier Control Block (BCB)
103from the BCB free list.  This data structure is used by RTEMS to manage the
104newly created barrier.  Also, a unique barrier ID is generated and returned to
105the calling task.
106
107Obtaining Barrier IDs
108---------------------
109
110When a barrier is created, RTEMS generates a unique barrier ID and assigns it
111to the created barrier until it is deleted.  The barrier ID may be obtained by
112either 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
114location.  Second, the barrier ID may be obtained later using the
115``rtems_barrier_ident`` directive.  The barrier ID is used by other barrier
116manager directives to access this barrier.
117
118Waiting at a Barrier
119--------------------
120
121The ``rtems_barrier_wait`` directive is used to wait at
122the specified barrier.  The task may wait forever for the barrier to be
123released or it may specify a timeout.  Specifying a timeout limits the interval
124the task will wait before returning with an error status code.
125
126If the barrier is configured as automatic and there are already one less then
127the maximum number of waiters, then the call will unblock all tasks waiting at
128the barrier and the caller will return immediately.
129
130When the task does wait to acquire the barrier, then it is placed in the
131barrier's task wait queue in FIFO order.  All tasks waiting on a barrier are
132returned an error code when the barrier is deleted.
133
134Releasing a Barrier
135-------------------
136
137The ``rtems_barrier_release`` directive is used to release the specified
138barrier.  When the ``rtems_barrier_release`` is invoked, all tasks waiting at
139the barrier are immediately made ready to execute and begin to compete for the
140processor to execute.
141
142Deleting a Barrier
143------------------
144
145The ``rtems_barrier_delete`` directive removes a barrier from the system and
146frees its control block.  A barrier can be deleted by any local task that knows
147the barrier's ID.  As a result of this directive, all tasks blocked waiting for
148the barrier to be released, will be readied and returned a status code which
149indicates that the barrier was deleted.  Any subsequent references to the
150barrier's name and ID are invalid.
151
152Directives
153==========
154
155This section details the barrier manager's directives.  A subsection is
156dedicated to each of this manager's directives and describes the calling
157sequence, 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
168BARRIER_CREATE - Create a barrier
169---------------------------------
170
171CALLING 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
181DIRECTIVE 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
194DESCRIPTION:
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
220NOTES:
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
245BARRIER_IDENT - Get ID of a barrier
246-----------------------------------
247
248CALLING SEQUENCE:
249    .. code-block:: c
250
251        rtems_status_code rtems_barrier_ident(
252            rtems_name        name,
253            rtems_id         *id
254        );
255
256DIRECTIVE 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
267DESCRIPTION:
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
274NOTES:
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
286BARRIER_DELETE - Delete a barrier
287---------------------------------
288
289CALLING SEQUENCE:
290    .. code-block:: c
291
292        rtems_status_code rtems_barrier_delete(
293            rtems_id id
294        );
295
296DIRECTIVE 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
305DESCRIPTION:
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
311NOTES:
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
329BARRIER_WAIT - Wait at a barrier
330----------------------------------
331
332CALLING SEQUENCE:
333    .. code-block:: c
334
335        rtems_status_code rtems_barrier_wait(
336            rtems_id         id,
337            rtems_interval   timeout
338        );
339
340DIRECTIVE 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
355DESCRIPTION:
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
369NOTES:
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
382BARRIER_RELEASE - Release a barrier
383-----------------------------------
384
385CALLING SEQUENCE:
386    .. code-block:: c
387
388        rtems_status_code rtems_barrier_release(
389            rtems_id  id,
390            uint32_t *released
391        );
392
393DIRECTIVE 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
402DESCRIPTION:
403    This directive releases the barrier specified by id.  All tasks waiting at
404    the barrier will be unblocked.
405
406NOTES:
407    The calling task may be preempted if it causes a higher priority task to be
408    made ready for execution.
Note: See TracBrowser for help on using the repository browser.