source: rtems-docs/c_user/barrier_manager.rst @ 01a36ee

4.115
Last change on this file since 01a36ee was 489740f, checked in by Chris Johns <chrisj@…>, on 05/20/16 at 02:47:09

Set SPDX License Identifier in each source file.

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