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

5
Last change on this file since c2ee227 was 0acf49e, checked in by Sebastian Huber <sebastian.huber@…>, on 11/20/17 at 07:25:26

Mention barrier manager in rtems_semaphore_flush()

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