source: rtems/cpukit/score/include/rtems/score/chainimpl.h @ 3709987

5
Last change on this file since 3709987 was 3709987, checked in by Sebastian Huber <sebastian.huber@…>, on 08/10/16 at 06:21:54

score: Add _Chain_Initialize_one()

  • Property mode set to 100644
File size: 27.9 KB
RevLine 
[baff4da]1/**
[a19ae9ec]2 * @file
[ac7d5ef0]3 *
[1f0d013]4 * @brief Chain Handler API
[baff4da]5 */
6
7/*
[a19ae9ec]8 *  Copyright (c) 2010 embedded brains GmbH.
9 *
[fbafb8f2]10 *  COPYRIGHT (c) 1989-2014.
[ac7d5ef0]11 *  On-Line Applications Research Corporation (OAR).
12 *
[98e4ebf5]13 *  The license and distribution terms for this file may be
14 *  found in the file LICENSE in this distribution or at
[c499856]15 *  http://www.rtems.org/license/LICENSE.
[ac7d5ef0]16 */
17
[6e93dc4a]18#ifndef _RTEMS_SCORE_CHAINIMPL_H
19#define _RTEMS_SCORE_CHAINIMPL_H
20
21#include <rtems/score/chain.h>
22#include <rtems/score/address.h>
[fbafb8f2]23#include <rtems/score/assert.h>
[ef49476]24
[6e93dc4a]25#ifdef __cplusplus
26extern "C" {
27#endif
[ac7d5ef0]28
[baff4da]29/**
[1f0d013]30 * @addtogroup ScoreChain
[ac7d5ef0]31 */
[b697bc6]32/**@{**/
[ac7d5ef0]33
[6e93dc4a]34/**
35 *  @brief Chain initializer for an empty chain with designator @a name.
36 */
37#define CHAIN_INITIALIZER_EMPTY(name) \
38  { { { &(name).Tail.Node, NULL }, &(name).Head.Node } }
39
[23de794d]40/**
41 *  @brief Chain initializer for a chain with one @a node.
42 *
43 *  @see CHAIN_NODE_INITIALIZER_ONE_NODE_CHAIN().
44 */
45#define CHAIN_INITIALIZER_ONE_NODE( node ) \
46  { { { (node), NULL }, (node) } }
47
48/**
49 *  @brief Chain node initializer for a @a chain containing exactly this node.
50 *
51 *  @see CHAIN_INITIALIZER_ONE_NODE().
52 */
53#define CHAIN_NODE_INITIALIZER_ONE_NODE_CHAIN( chain ) \
54  { &(chain)->Tail.Node, &(chain)->Head.Node }
55
[6e93dc4a]56/**
57 *  @brief Chain definition for an empty chain with designator @a name.
58 */
59#define CHAIN_DEFINE_EMPTY(name) \
60  Chain_Control name = CHAIN_INITIALIZER_EMPTY(name)
61
62/**
63 *  @brief Initialize a chain header.
64 *
65 *  This routine initializes @a the_chain structure to manage the
66 *  contiguous array of @a number_nodes nodes which starts at
67 *  @a starting_address.  Each node is of @a node_size bytes.
68 *
69 *  @param[in] the_chain specifies the chain to initialize
70 *  @param[in] starting_address is the starting address of the array
71 *         of elements
72 *  @param[in] number_nodes is the numebr of nodes that will be in the chain
73 *  @param[in] node_size is the size of each node
74 */
75void _Chain_Initialize(
76  Chain_Control *the_chain,
77  void          *starting_address,
78  size_t         number_nodes,
79  size_t         node_size
80);
81
82/**
83 * @brief Returns the node count of the chain.
84 *
85 * @param[in] chain The chain.
86 *
87 * @note It does NOT disable interrupts to ensure the atomicity of the
88 * operation.
89 *
90 * @retval The node count of the chain.
91 */
92size_t _Chain_Node_count_unprotected( const Chain_Control *chain );
93
[1f0d013]94/**
95 * @brief Set off chain.
[eb649786]96 *
[d6da1b1]97 * This function sets the next field of the @a node to NULL indicating the @a
98 * node is not part of a chain.
[eb649786]99 *
[1f0d013]100 * @param[in] node the node set to off chain.
[eb649786]101 */
102RTEMS_INLINE_ROUTINE void _Chain_Set_off_chain(
103  Chain_Node *node
104)
105{
[d6da1b1]106  node->next = NULL;
[059529e]107#if defined(RTEMS_DEBUG)
108  node->previous = NULL;
109#endif
110}
111
112/**
113 * @brief Initializes a chain node.
114 *
115 * In debug configurations, the node is set off chain.  In all other
116 * configurations, this function does nothing.
117 *
118 * @param[in] the_node The chain node to initialize.
119 */
120RTEMS_INLINE_ROUTINE void _Chain_Initialize_node( Chain_Node *the_node )
121{
122#if defined(RTEMS_DEBUG)
123  _Chain_Set_off_chain( the_node );
124#else
125  (void) the_node;
126#endif
[eb649786]127}
128
[1f0d013]129/**
130 * @brief Is the node off chain.
[eb649786]131 *
[d6da1b1]132 * This function returns true if the @a node is not on a chain.  A @a node is
133 * off chain if the next field is set to NULL.
[eb649786]134 *
[1f0d013]135 * @param[in] node is the node off chain.
[eb649786]136 *
[1f0d013]137 * @retval true The @a node is off chain.
138 * @retval false The @a node is not off chain.
[eb649786]139 */
140RTEMS_INLINE_ROUTINE bool _Chain_Is_node_off_chain(
141  const Chain_Node *node
142)
143{
[d6da1b1]144  return node->next == NULL;
[eb649786]145}
146
[1f0d013]147/**
148 * @brief Are two nodes equal.
[6a07436]149 *
[1f0d013]150 * This function returns true if @a left and @a right are equal,
151 * and false otherwise.
[6a07436]152 *
[1f0d013]153 * @param[in] left is the node on the left hand side of the comparison.
154 * @param[in] right is the node on the left hand side of the comparison.
[6a07436]155 *
[1f0d013]156 * @retval true @a left and @a right are equal.
157 * @retval false @a left and @a right are not equal.
[baff4da]158 */
[484a769]159RTEMS_INLINE_ROUTINE bool _Chain_Are_nodes_equal(
[86aa7980]160  const Chain_Node *left,
161  const Chain_Node *right
[ac7d5ef0]162)
163{
164  return left == right;
165}
166
[1f0d013]167/**
168 * @brief Is the chain node pointer NULL.
[6a07436]169 *
[1f0d013]170 * This function returns true if the_node is NULL and false otherwise.
[6a07436]171 *
[1f0d013]172 * @param[in] the_node is the node pointer to check.
[6a07436]173 *
[1f0d013]174 * @retval true @a the_node is @c NULL.
175 * @retval false @a the_node is not @c NULL.
[ac7d5ef0]176 */
[484a769]177RTEMS_INLINE_ROUTINE bool _Chain_Is_null_node(
[852d53f7]178  const Chain_Node *the_node
[ac7d5ef0]179)
180{
[6a07436]181  return (the_node == NULL);
[ac7d5ef0]182}
183
[1f0d013]184/**
185 * @brief Return pointer to chain head.
[6a07436]186 *
[1f0d013]187 * This function returns a pointer to the head node on the chain.
[6a07436]188 *
[1f0d013]189 * @param[in] the_chain is the chain to be operated upon.
[6a07436]190 *
[1f0d013]191 * @return This method returns the permanent head node of the chain.
[ac7d5ef0]192 */
[503dc058]193RTEMS_INLINE_ROUTINE Chain_Node *_Chain_Head(
[ac7d5ef0]194  Chain_Control *the_chain
195)
196{
[76da5fa8]197  return &the_chain->Head.Node;
198}
199
[1f0d013]200/**
201 * @brief Return pointer to immutable chain head.
[76da5fa8]202 *
[1f0d013]203 * This function returns a pointer to the head node on the chain.
[76da5fa8]204 *
[1f0d013]205 * @param[in] the_chain is the chain to be operated upon.
[76da5fa8]206 *
[1f0d013]207 * @return This method returns the permanent head node of the chain.
[76da5fa8]208 */
209RTEMS_INLINE_ROUTINE const Chain_Node *_Chain_Immutable_head(
210  const Chain_Control *the_chain
211)
212{
213  return &the_chain->Head.Node;
[ac7d5ef0]214}
215
[1f0d013]216/**
217 * @brief Return pointer to chain tail.
[6a07436]218 *
[1f0d013]219 * This function returns a pointer to the tail node on the chain.
[6a07436]220 *
[1f0d013]221 * @param[in] the_chain is the chain to be operated upon.
[6a07436]222 *
[1f0d013]223 * @return This method returns the permanent tail node of the chain.
[ac7d5ef0]224 */
[503dc058]225RTEMS_INLINE_ROUTINE Chain_Node *_Chain_Tail(
[ac7d5ef0]226  Chain_Control *the_chain
227)
228{
[76da5fa8]229  return &the_chain->Tail.Node;
230}
231
[1f0d013]232/**
233 * @brief Return pointer to immutable chain tail.
[76da5fa8]234 *
[1f0d013]235 * This function returns a pointer to the tail node on the chain.
[76da5fa8]236 *
[1f0d013]237 * @param[in] the_chain is the chain to be operated upon.
[76da5fa8]238 *
[1f0d013]239 * @return This method returns the permanent tail node of the chain.
[76da5fa8]240 */
241RTEMS_INLINE_ROUTINE const Chain_Node *_Chain_Immutable_tail(
242  const Chain_Control *the_chain
243)
244{
245  return &the_chain->Tail.Node;
[ac7d5ef0]246}
247
[1f0d013]248/**
249 * @brief Return pointer to chain's first node.
[0d15414e]250 *
[1f0d013]251 * This function returns a pointer to the first node on the chain after the
252 * head.
[0d15414e]253 *
[1f0d013]254 * @param[in] the_chain is the chain to be operated upon.
[0d15414e]255 *
[1f0d013]256 * @return This method returns the first node of the chain.
[0d15414e]257 */
258RTEMS_INLINE_ROUTINE Chain_Node *_Chain_First(
[796f12a]259  const Chain_Control *the_chain
[0d15414e]260)
261{
[796f12a]262  return _Chain_Immutable_head( the_chain )->next;
[76da5fa8]263}
264
[1f0d013]265/**
266 * @brief Return pointer to immutable chain's first node.
[76da5fa8]267 *
[1f0d013]268 * This function returns a pointer to the first node on the chain after the
269 * head.
[76da5fa8]270 *
[1f0d013]271 * @param[in] the_chain is the chain to be operated upon.
[76da5fa8]272 *
[1f0d013]273 * @return This method returns the first node of the chain.
[76da5fa8]274 */
275RTEMS_INLINE_ROUTINE const Chain_Node *_Chain_Immutable_first(
276  const Chain_Control *the_chain
277)
278{
279  return _Chain_Immutable_head( the_chain )->next;
[0d15414e]280}
281
[1f0d013]282/**
283 * @brief Return pointer to chain's last node.
[0d15414e]284 *
[1f0d013]285 * This function returns a pointer to the last node on the chain just before
286 * the tail.
[0d15414e]287 *
[1f0d013]288 * @param[in] the_chain is the chain to be operated upon.
[0d15414e]289 *
[1f0d013]290 * @return This method returns the last node of the chain.
[0d15414e]291 */
292RTEMS_INLINE_ROUTINE Chain_Node *_Chain_Last(
[796f12a]293  const Chain_Control *the_chain
[0d15414e]294)
295{
[796f12a]296  return _Chain_Immutable_tail( the_chain )->previous;
[76da5fa8]297}
298
[1f0d013]299/**
300 * @brief Return pointer to immutable chain's last node.
[76da5fa8]301 *
[1f0d013]302 * This function returns a pointer to the last node on the chain just before
303 * the tail.
[76da5fa8]304 *
[1f0d013]305 * @param[in] the_chain is the chain to be operated upon.
[76da5fa8]306 *
[1f0d013]307 * @return This method returns the last node of the chain.
[76da5fa8]308 */
309RTEMS_INLINE_ROUTINE const Chain_Node *_Chain_Immutable_last(
310  const Chain_Control *the_chain
311)
312{
313  return _Chain_Immutable_tail( the_chain )->previous;
[0d15414e]314}
315
[1f0d013]316/**
317 * @brief Return pointer the next node from this node.
[0d15414e]318 *
[1f0d013]319 * This function returns a pointer to the next node after this node.
[0d15414e]320 *
[1f0d013]321 * @param[in] the_node is the node to be operated upon.
[0d15414e]322 *
[1f0d013]323 * @return This method returns the next node on the chain.
[0d15414e]324 */
325RTEMS_INLINE_ROUTINE Chain_Node *_Chain_Next(
[796f12a]326  const Chain_Node *the_node
[0d15414e]327)
328{
329  return the_node->next;
330}
331
[1f0d013]332/**
333 * @brief Return pointer the immutable next node from this node.
[72a3af3]334 *
[1f0d013]335 * This function returns a pointer to the next node after this node.
[72a3af3]336 *
[1f0d013]337 * @param[in] the_node is the node to be operated upon.
[72a3af3]338 *
[1f0d013]339 * @return This method returns the next node on the chain.
[72a3af3]340 */
341RTEMS_INLINE_ROUTINE const Chain_Node *_Chain_Immutable_next(
342  const Chain_Node *the_node
343)
344{
345  return the_node->next;
346}
347
[1f0d013]348/**
349 * @brief Return pointer the previous node from this node.
[0d15414e]350 *
[1f0d013]351 * This function returns a pointer to the previous node on this chain.
[0d15414e]352 *
[1f0d013]353 * @param[in] the_node is the node to be operated upon.
[0d15414e]354 *
[1f0d013]355 * @return This method returns the previous node on the chain.
[0d15414e]356 */
357RTEMS_INLINE_ROUTINE Chain_Node *_Chain_Previous(
[796f12a]358  const Chain_Node *the_node
[0d15414e]359)
360{
361  return the_node->previous;
362}
363
[1f0d013]364/**
365 * @brief Return pointer the immutable previous node from this node.
[72a3af3]366 *
[1f0d013]367 * This function returns a pointer to the previous node on this chain.
[72a3af3]368 *
[1f0d013]369 * @param[in] the_node is the node to be operated upon.
[72a3af3]370 *
[1f0d013]371 * @return This method returns the previous node on the chain.
[72a3af3]372 */
373RTEMS_INLINE_ROUTINE const Chain_Node *_Chain_Immutable_previous(
374  const Chain_Node *the_node
375)
376{
377  return the_node->previous;
378}
379
[1f0d013]380/**
381 * @brief Is the chain empty.
[6a07436]382 *
[1f0d013]383 * This function returns true if there a no nodes on @a the_chain and
384 * false otherwise.
[6a07436]385 *
[1f0d013]386 * @param[in] the_chain is the chain to be operated upon.
[6a07436]387 *
[1f0d013]388 * @retval true There are no nodes on @a the_chain.
389 * @retval false There are nodes on @a the_chain.
[ac7d5ef0]390 */
[484a769]391RTEMS_INLINE_ROUTINE bool _Chain_Is_empty(
[76da5fa8]392  const Chain_Control *the_chain
[ac7d5ef0]393)
394{
[76da5fa8]395  return _Chain_Immutable_first( the_chain )
396    == _Chain_Immutable_tail( the_chain );
[ac7d5ef0]397}
398
[1f0d013]399/**
400 * @brief Is this the first node on the chain.
[6a07436]401 *
[1f0d013]402 * This function returns true if the_node is the first node on a chain and
403 * false otherwise.
[6a07436]404 *
[1f0d013]405 * @param[in] the_node is the node the caller wants to know if it is
406 *            the first node on a chain.
[6a07436]407 *
[1f0d013]408 * @retval true @a the_node is the first node on a chain.
409 * @retval false @a the_node is not the first node on a chain.
[ac7d5ef0]410 */
[484a769]411RTEMS_INLINE_ROUTINE bool _Chain_Is_first(
[852d53f7]412  const Chain_Node *the_node
[ac7d5ef0]413)
414{
[e3a1488a]415  return (the_node->previous->previous == NULL);
[ac7d5ef0]416}
417
[1f0d013]418/**
419 * @brief Is this the last node on the chain.
[6a07436]420 *
[1f0d013]421 * This function returns true if @a the_node is the last node on a chain and
422 * false otherwise.
[6a07436]423 *
[1f0d013]424 * @param[in] the_node is the node to check as the last node.
[6a07436]425 *
[1f0d013]426 * @retval true @a the_node is the last node on a chain.
427 * @retval false @a the_node is not the last node on a chain.
[ac7d5ef0]428 */
[484a769]429RTEMS_INLINE_ROUTINE bool _Chain_Is_last(
[852d53f7]430  const Chain_Node *the_node
[ac7d5ef0]431)
432{
[e3a1488a]433  return (the_node->next->next == NULL);
[ac7d5ef0]434}
435
[1f0d013]436/**
437 * @brief Does this chain have only one node.
438 *
439 * This function returns true if there is only one node on @a the_chain and
440 * false otherwise.
[6a07436]441 *
[1f0d013]442 * @param[in] the_chain is the chain to be operated upon.
[6a07436]443 *
[1f0d013]444 * @return This function returns true if there is only one node on
445 *         @a the_chain and false otherwise.
[6a07436]446 *
[1f0d013]447 * @retval true There is only one node on @a the_chain.
448 * @retval false There is more than one node on @a the_chain.
[ac7d5ef0]449 */
[484a769]450RTEMS_INLINE_ROUTINE bool _Chain_Has_only_one_node(
[852d53f7]451  const Chain_Control *the_chain
[ac7d5ef0]452)
453{
[76da5fa8]454  return _Chain_Immutable_first( the_chain )
455    == _Chain_Immutable_last( the_chain );
[ac7d5ef0]456}
457
[1f0d013]458/**
459 * @brief Is this node the chain head.
[6a07436]460 *
[1f0d013]461 * This function returns true if @a the_node is the head of @a the_chain and
462 * false otherwise.
[6a07436]463 *
[1f0d013]464 * @param[in] the_chain is the chain to be operated upon.
465 * @param[in] the_node is the node to check for being the Chain Head.
[6a07436]466 *
[1f0d013]467 * @retval true @a the_node is the head of @a the_chain.
468 * @retval false @a the_node is not the head of @a the_chain.
[ac7d5ef0]469 */
[484a769]470RTEMS_INLINE_ROUTINE bool _Chain_Is_head(
[72a3af3]471  const Chain_Control *the_chain,
[852d53f7]472  const Chain_Node    *the_node
[ac7d5ef0]473)
474{
[72a3af3]475  return (the_node == _Chain_Immutable_head( the_chain ));
[ac7d5ef0]476}
477
[1f0d013]478/**
479 * @brief Is this node the chail tail.
[6a07436]480 *
[1f0d013]481 * This function returns true if @a the_node is the tail of @a the_chain and
482 * false otherwise.
[6a07436]483 *
[1f0d013]484 * @param[in] the_chain is the chain to be operated upon.
485 * @param[in] the_node is the node to check for being the Chain Tail.
486 *
487 * @retval true @a the_node is the tail of @a the_chain.
488 * @retval false @a the_node is not the tail of @a the_chain.
[ac7d5ef0]489 */
[484a769]490RTEMS_INLINE_ROUTINE bool _Chain_Is_tail(
[72a3af3]491  const Chain_Control *the_chain,
[852d53f7]492  const Chain_Node    *the_node
[ac7d5ef0]493)
494{
[72a3af3]495  return (the_node == _Chain_Immutable_tail( the_chain ));
[ac7d5ef0]496}
497
[1f0d013]498/**
499 * @brief Initialize this chain as empty.
[6a07436]500 *
[1f0d013]501 * This routine initializes the specified chain to contain zero nodes.
[6a07436]502 *
[1f0d013]503 * @param[in] the_chain is the chain to be initialized.
[ac7d5ef0]504 */
[503dc058]505RTEMS_INLINE_ROUTINE void _Chain_Initialize_empty(
[ac7d5ef0]506  Chain_Control *the_chain
507)
508{
[fbafb8f2]509  Chain_Node *head;
510  Chain_Node *tail;
511
512  _Assert( the_chain != NULL );
513
514  head = _Chain_Head( the_chain );
515  tail = _Chain_Tail( the_chain );
[76da5fa8]516
517  head->next = tail;
518  head->previous = NULL;
519  tail->previous = head;
[ac7d5ef0]520}
521
[3709987]522/**
523 * @brief Initializes this chain to contain exactly the specified node.
524 *
525 * @param[in] the_chain The chain control.
526 * @param[in] the_node The one and only node.
527 */
528RTEMS_INLINE_ROUTINE void _Chain_Initialize_one(
529  Chain_Control *the_chain,
530  Chain_Node    *the_node
531)
532{
533  Chain_Node *head;
534  Chain_Node *tail;
535
536  _Assert( _Chain_Is_node_off_chain( the_node ) );
537
538  head = _Chain_Head( the_chain );
539  tail = _Chain_Tail( the_chain );
540
541  the_node->next = tail;
542  the_node->previous = head;
543
544  head->next = the_node;
545  head->previous = NULL;
546  tail->previous = the_node;
547}
548
[1f0d013]549/**
550 * @brief Extract this node (unprotected).
[6a07436]551 *
[1f0d013]552 * This routine extracts the_node from the chain on which it resides.
553 * It does NOT disable interrupts to ensure the atomicity of the
554 * extract operation.
[6a07436]555 *
[1f0d013]556 * @param[in] the_node is the node to be extracted.
[ac7d5ef0]557 */
[503dc058]558RTEMS_INLINE_ROUTINE void _Chain_Extract_unprotected(
[ac7d5ef0]559  Chain_Node *the_node
560)
561{
562  Chain_Node *next;
563  Chain_Node *previous;
564
565  next           = the_node->next;
566  previous       = the_node->previous;
567  next->previous = previous;
568  previous->next = next;
[059529e]569
570#if defined(RTEMS_DEBUG)
571  _Chain_Set_off_chain( the_node );
572#endif
[ac7d5ef0]573}
574
[1f0d013]575/**
576 * @brief Get the first node (unprotected).
[6a07436]577 *
[1f0d013]578 * This function removes the first node from the_chain and returns
579 * a pointer to that node.  It does NOT disable interrupts to ensure
580 * the atomicity of the get operation.
[6a07436]581 *
[1f0d013]582 * @param[in] the_chain is the chain to attempt to get the first node from.
[6a07436]583 *
[1f0d013]584 * @return This method returns the first node on the chain even if it is
585 *         the Chain Tail.
[6a07436]586 *
[1f0d013]587 * @note This routine assumes that there is at least one node on the chain
588 *       and always returns a node even if it is the Chain Tail.
[ac7d5ef0]589 */
[503dc058]590RTEMS_INLINE_ROUTINE Chain_Node *_Chain_Get_first_unprotected(
[ac7d5ef0]591  Chain_Control *the_chain
592)
593{
[059529e]594  Chain_Node *head;
595  Chain_Node *old_first;
596  Chain_Node *new_first;
597
598  _Assert( !_Chain_Is_empty( the_chain ) );
599
600  head = _Chain_Head( the_chain );
601  old_first = head->next;
602  new_first = old_first->next;
[ac7d5ef0]603
[76da5fa8]604  head->next = new_first;
605  new_first->previous = head;
[ac7d5ef0]606
[059529e]607#if defined(RTEMS_DEBUG)
608  _Chain_Set_off_chain( old_first );
609#endif
610
[76da5fa8]611  return old_first;
[ac7d5ef0]612}
613
[1f0d013]614/**
615 * @brief Get the first node (unprotected).
[6a07436]616 *
[1f0d013]617 * This function removes the first node from the_chain and returns
618 * a pointer to that node.  If the_chain is empty, then NULL is returned.
[6a07436]619 *
[1f0d013]620 * @param[in] the_chain is the chain to attempt to get the first node from.
[6a07436]621 *
[1f0d013]622 * @return This method returns the first node on the chain or NULL if the
623 *         chain is empty.
[6a07436]624 *
[1f0d013]625 * @note It does NOT disable interrupts to ensure the atomicity of the
626 *       get operation.
[ac7d5ef0]627 */
[503dc058]628RTEMS_INLINE_ROUTINE Chain_Node *_Chain_Get_unprotected(
[ac7d5ef0]629  Chain_Control *the_chain
630)
631{
[6a07436]632  if ( !_Chain_Is_empty(the_chain))
633    return _Chain_Get_first_unprotected(the_chain);
[ac7d5ef0]634  else
635    return NULL;
636}
637
[1f0d013]638/**
639 * @brief Insert a node (unprotected).
[6a07436]640 *
[1f0d013]641 * This routine inserts the_node on a chain immediately following
642 * after_node.
[6a07436]643 *
[1f0d013]644 * @param[in] after_node is the node which will precede @a the_node on the
645 *            chain.
646 * @param[in] the_node is the node to be inserted.
[6a07436]647 *
[1f0d013]648 * @note It does NOT disable interrupts to ensure the atomicity
649 *       of the extract operation.
[ac7d5ef0]650 */
[503dc058]651RTEMS_INLINE_ROUTINE void _Chain_Insert_unprotected(
[ac7d5ef0]652  Chain_Node *after_node,
653  Chain_Node *the_node
654)
655{
656  Chain_Node *before_node;
657
[059529e]658  _Assert( _Chain_Is_node_off_chain( the_node ) );
659
[ac7d5ef0]660  the_node->previous    = after_node;
661  before_node           = after_node->next;
662  after_node->next      = the_node;
663  the_node->next        = before_node;
664  before_node->previous = the_node;
665}
666
[1f0d013]667/**
668 * @brief Append a node (unprotected).
[6a07436]669 *
[1f0d013]670 * This routine appends the_node onto the end of the_chain.
[6a07436]671 *
[1f0d013]672 * @param[in] the_chain is the chain to be operated upon.
673 * @param[in] the_node is the node to be appended.
[6a07436]674 *
[1f0d013]675 * @note It does NOT disable interrupts to ensure the atomicity of the
676 *       append operation.
[ac7d5ef0]677 */
[503dc058]678RTEMS_INLINE_ROUTINE void _Chain_Append_unprotected(
[ac7d5ef0]679  Chain_Control *the_chain,
680  Chain_Node    *the_node
681)
682{
[059529e]683  Chain_Node *tail;
684  Chain_Node *old_last;
685
686  _Assert( _Chain_Is_node_off_chain( the_node ) );
687
688  tail = _Chain_Tail( the_chain );
689  old_last = tail->previous;
[ac7d5ef0]690
[76da5fa8]691  the_node->next = tail;
692  tail->previous = the_node;
693  old_last->next = the_node;
694  the_node->previous = old_last;
[ac7d5ef0]695}
696
[0d66dd7]697/**
[1f0d013]698 * @brief Append a node on the end of a chain if the node is in the off chain
699 * state (unprotected).
[0d66dd7]700 *
[1f0d013]701 * @note It does NOT disable interrupts to ensure the atomicity of the
702 *       append operation.
[0d66dd7]703 *
[1f0d013]704 * @see _Chain_Append_unprotected() and _Chain_Is_node_off_chain().
[0d66dd7]705 */
706RTEMS_INLINE_ROUTINE void _Chain_Append_if_is_off_chain_unprotected(
707  Chain_Control *the_chain,
708  Chain_Node    *the_node
709)
710{
711  if ( _Chain_Is_node_off_chain( the_node ) ) {
712    _Chain_Append_unprotected( the_chain, the_node );
713  }
714}
715
[1f0d013]716/**
717 * @brief Prepend a node (unprotected).
[6a07436]718 *
[1f0d013]719 * This routine prepends the_node onto the front of the_chain.
[6a07436]720 *
[1f0d013]721 * @param[in] the_chain is the chain to be operated upon.
722 * @param[in] the_node is the node to be prepended.
[6a07436]723 *
[1f0d013]724 * @note It does NOT disable interrupts to ensure the atomicity of the
725 *       prepend operation.
[ac7d5ef0]726 */
[503dc058]727RTEMS_INLINE_ROUTINE void _Chain_Prepend_unprotected(
[ac7d5ef0]728  Chain_Control *the_chain,
729  Chain_Node    *the_node
730)
731{
[6a07436]732  _Chain_Insert_unprotected(_Chain_Head(the_chain), the_node);
[ac7d5ef0]733}
734
[a19ae9ec]735/**
736 * @brief Append a node and check if the chain was empty before (unprotected).
737 *
738 * This routine appends the_node onto the end of the_chain.
739 *
740 * @param[in] the_chain is the chain to be operated upon.
741 * @param[in] the_node is the node to be appended.
742 *
743 * @note It does NOT disable interrupts to ensure the atomicity of the
744 *       append operation.
745 *
746 * @retval true The chain was empty before.
747 * @retval false The chain contained at least one node before.
748 */
749RTEMS_INLINE_ROUTINE bool _Chain_Append_with_empty_check_unprotected(
750  Chain_Control *the_chain,
751  Chain_Node    *the_node
752)
753{
754  bool was_empty = _Chain_Is_empty( the_chain );
755
756  _Chain_Append_unprotected( the_chain, the_node );
757
758  return was_empty;
759}
760
761/**
762 * @brief Prepend a node and check if the chain was empty before (unprotected).
763 *
764 * This routine prepends the_node onto the front of the_chain.
765 *
766 * @param[in] the_chain is the chain to be operated upon.
767 * @param[in] the_node is the node to be prepended.
768 *
769 * @note It does NOT disable interrupts to ensure the atomicity of the
770 *       prepend operation.
771 *
772 * @retval true The chain was empty before.
773 * @retval false The chain contained at least one node before.
774 */
775RTEMS_INLINE_ROUTINE bool _Chain_Prepend_with_empty_check_unprotected(
776  Chain_Control *the_chain,
777  Chain_Node    *the_node
778)
779{
780  bool was_empty = _Chain_Is_empty( the_chain );
781
782  _Chain_Prepend_unprotected( the_chain, the_node );
783
784  return was_empty;
785}
786
787/**
788 * @brief Get the first node and check if the chain is empty afterwards
789 * (unprotected).
790 *
791 * This function removes the first node from the_chain and returns
792 * a pointer to that node in @a the_node.  If the_chain is empty, then NULL is
793 * returned.
794 *
795 * @param[in] the_chain is the chain to attempt to get the first node from.
796 * @param[out] the_node is the first node on the chain or NULL if the chain is
797 * empty.
798 *
799 * @note It does NOT disable interrupts to ensure the atomicity of the
800 *       get operation.
801 *
802 * @retval true The chain is empty now.
803 * @retval false The chain contains at least one node now.
804 */
805RTEMS_INLINE_ROUTINE bool _Chain_Get_with_empty_check_unprotected(
806  Chain_Control *the_chain,
807  Chain_Node **the_node
808)
809{
810  bool is_empty_now = true;
[76da5fa8]811  Chain_Node *head = _Chain_Head( the_chain );
812  Chain_Node *tail = _Chain_Tail( the_chain );
813  Chain_Node *old_first = head->next;
[a19ae9ec]814
[76da5fa8]815  if ( old_first != tail ) {
816    Chain_Node *new_first = old_first->next;
[a19ae9ec]817
[76da5fa8]818    head->next = new_first;
819    new_first->previous = head;
[a19ae9ec]820
[76da5fa8]821    *the_node = old_first;
[a19ae9ec]822
[76da5fa8]823    is_empty_now = new_first == tail;
[a19ae9ec]824  } else
825    *the_node = NULL;
826
827  return is_empty_now;
828}
829
[ec978d9]830/**
831 * @brief Chain node order.
832 *
833 * @param[in] left The left node.
834 * @param[in] right The right node.
835 *
836 * @retval true According to the order the left node precedes the right node.
837 * @retval false Otherwise.
838 */
839typedef bool ( *Chain_Node_order )(
840  const Chain_Node *left,
841  const Chain_Node *right
842);
843
844/**
845 * @brief Inserts a node into the chain according to the order relation.
846 *
847 * After the operation the chain contains the node to insert and the order
848 * relation holds for all nodes from the head up to the inserted node.  Nodes
849 * after the inserted node are not moved.
850 *
[961669d]851 * @param[in,out] chain The chain.
852 * @param[in,out] to_insert The node to insert.
[ec978d9]853 * @param[in] order The order relation.
854 */
855RTEMS_INLINE_ROUTINE void _Chain_Insert_ordered_unprotected(
856  Chain_Control *chain,
857  Chain_Node *to_insert,
858  Chain_Node_order order
859)
860{
861  const Chain_Node *tail = _Chain_Immutable_tail( chain );
862  Chain_Node *next = _Chain_First( chain );
863
864  while ( next != tail && !( *order )( to_insert, next ) ) {
865    next = _Chain_Next( next );
866  }
867
868  _Chain_Insert_unprotected( _Chain_Previous( next ), to_insert );
869}
870
[8f6c295b]871/**
872 * @brief The chain iterator direction.
873 */
874typedef enum {
875  /**
876   * @brief Iteration from head to tail.
877   */
878  CHAIN_ITERATOR_FORWARD,
879
880  /**
881   * @brief Iteration from tail to head.
882   */
883  CHAIN_ITERATOR_BACKWARD
884} Chain_Iterator_direction;
885
886/**
887 * @brief A chain iterator which is updated during node extraction if it is
888 * properly registered.
889 *
890 * @see _Chain_Iterator_initialize().
891 */
892typedef struct {
893  /**
894   * @brief Node for registration.
895   *
896   * Used during _Chain_Iterator_initialize() and _Chain_Iterator_destroy().
897   */
898  Chain_Node Registry_node;
899
900  /**
901   * @brief The direction of this iterator.
902   *
903   * Immutable after initialization via _Chain_Iterator_initialize().
904   */
905  Chain_Iterator_direction  direction;
906
907  /**
908   * @brief The current position of this iterator.
909   *
910   * The position is initialized via _Chain_Iterator_initialize().  It must be
911   * explicitly set after one valid iteration step, e.g. in case a next node in
912   * the iterator direction existed.  It is updated through the registration in
913   * case a node is extracted via _Chain_Iterator_registry_update().
914   */
915  Chain_Node *position;
916} Chain_Iterator;
917
918/**
919 * @brief A registry for chain iterators.
920 *
921 * Should be attached to a chain control to enable safe iteration through a
922 * chain in case of concurrent node extractions.
923 */
924typedef struct {
925  Chain_Control Iterators;
926} Chain_Iterator_registry;
927
928/**
929 * @brief Chain iterator registry initializer for static initialization.
930 *
931 * @param name The designator of the chain iterator registry.
932 */
933#define CHAIN_ITERATOR_REGISTRY_INITIALIZER( name ) \
934  { CHAIN_INITIALIZER_EMPTY( name.Iterators ) }
935
936/**
937 * @brief Initializes a chain iterator registry.
938 */
939RTEMS_INLINE_ROUTINE void _Chain_Iterator_registry_initialize(
940  Chain_Iterator_registry *the_registry
941)
942{
943  _Chain_Initialize_empty( &the_registry->Iterators );
944}
945
946/**
947 * @brief Updates all iterators present in the chain iterator registry in case
948 * of a node extraction.
949 *
950 * Must be called before _Chain_Extract_unprotected().
951 *
952 * @warning This function will look at all registered chain iterators to
953 *   determine if an update is necessary.
954 */
955RTEMS_INLINE_ROUTINE void _Chain_Iterator_registry_update(
956  Chain_Iterator_registry *the_registry,
957  Chain_Node              *the_node_to_extract
958)
959{
960  Chain_Node *iter_node;
961  Chain_Node *iter_tail;
962
963  iter_node = _Chain_Head( &the_registry->Iterators );
964  iter_tail = _Chain_Tail( &the_registry->Iterators );
965
966  while ( ( iter_node = _Chain_Next( iter_node ) ) != iter_tail ) {
967    Chain_Iterator *iter;
968
969    iter = (Chain_Iterator *) iter_node;
970
971    if ( iter->position == the_node_to_extract ) {
972      if ( iter->direction == CHAIN_ITERATOR_FORWARD ) {
973        iter->position = _Chain_Previous( the_node_to_extract );
974      } else {
975        iter->position = _Chain_Next( the_node_to_extract );
976      }
977    }
978  }
979}
980
981/**
982 * @brief Initializes the chain iterator.
983 *
984 * In the following example nodes inserted during the iteration are visited in
985 * case they are inserted after the current position in iteration order.
986 *
987 * @code
988 * #include <rtems/score/chainimpl.h>
989 * #include <rtems/score/isrlock.h>
990 *
991 * typedef struct {
992 *   Chain_Control           Chain;
993 *   Chain_Iterator_registry Iterators;
994 *   ISR_LOCK_MEMBER(        Lock )
995 * } Some_Control;
996 *
997 * void iterate(
998 *   Some_Control   *the_some,
999 *   void         ( *visitor )( Chain_Node * )
1000 * )
1001 * {
1002 *   ISR_lock_Context  lock_context;
1003 *   Chain_Iterator    iter;
1004 *   Chain_Node       *node;
1005 *   const Chain_Node *end;
1006 *
1007 *   end = _Chain_Immutable_tail( &the_some->Chain );
1008 *
1009 *   _ISR_lock_ISR_disable_and_acquire( &the_some->Lock, &lock_context );
1010 *
1011 *   _Chain_Iterator_initialize(
1012 *     &the_some->Chain,
1013 *     &the_some->Iterators,
1014 *     &iter,
1015 *     CHAIN_ITERATOR_FORWARD
1016 *   );
1017 *
1018 *   while ( ( node = _Chain_Iterator_next( &iter ) ) != end ) {
1019 *     _Chain_Iterator_set_position( &iter, node );
1020 *     _ISR_lock_Release_and_ISR_enable( &the_some->Lock, &lock_context );
1021 *     ( *visitor )( node );
1022 *     _ISR_lock_ISR_disable_and_acquire( &the_some->Lock, &lock_context );
1023 *   }
1024 *
1025 *   _Chain_Iterator_destroy( &iter );
1026 *   _ISR_lock_Release_and_ISR_enable( &the_some->Lock, &lock_context );
1027 * }
1028 * @endcode
1029 *
1030 * @param the_chain The chain to iterate.
1031 * @param the_registry The registry for the chain iterator.
1032 * @param the_iterator The chain iterator to initialize.
1033 * @param direction The iteration direction.
1034 *
1035 * @see _Chain_Iterator_next(), _Chain_Iterator_set_position() and
1036 * Chain_Iterator_destroy().
1037 *
1038 * @warning Think twice before you use a chain iterator.  Its current
1039 *   implementation is unfit for use in performance relevant components, due to
1040 *   the linear time complexity in _Chain_Iterator_registry_update().
1041 */
1042RTEMS_INLINE_ROUTINE void _Chain_Iterator_initialize(
1043  Chain_Control            *the_chain,
1044  Chain_Iterator_registry  *the_registry,
1045  Chain_Iterator           *the_iterator,
1046  Chain_Iterator_direction  direction
1047)
1048{
[059529e]1049  _Chain_Initialize_node( &the_iterator->Registry_node );
[8f6c295b]1050  _Chain_Append_unprotected(
1051    &the_registry->Iterators,
1052    &the_iterator->Registry_node
1053  );
1054
1055  the_iterator->direction = direction;
1056
1057  if ( direction == CHAIN_ITERATOR_FORWARD ) {
1058    the_iterator->position = _Chain_Head( the_chain );
1059  } else {
1060    the_iterator->position = _Chain_Tail( the_chain );
1061  }
1062}
1063
1064/**
1065 * @brief Returns the next node in the iterator direction.
1066 *
1067 * In case a next node exists, then the iterator should be updated via
1068 * _Chain_Iterator_set_position() to continue with the next iteration step.
1069 *
1070 * @param the_iterator The chain iterator.
1071 */
1072RTEMS_INLINE_ROUTINE Chain_Node *_Chain_Iterator_next(
1073  const Chain_Iterator *the_iterator
1074)
1075{
1076  if ( the_iterator->direction == CHAIN_ITERATOR_FORWARD ) {
1077    return _Chain_Next( the_iterator->position );
1078  } else {
1079    return _Chain_Previous( the_iterator->position );
1080  }
1081}
1082
1083/**
1084 * @brief Sets the iterator position.
1085 *
1086 * @param the_iterator The chain iterator.
1087 * @param the_node The new iterator position.
1088 */
1089RTEMS_INLINE_ROUTINE void _Chain_Iterator_set_position(
1090  Chain_Iterator *the_iterator,
1091  Chain_Node     *the_node
1092)
1093{
1094  the_iterator->position = the_node;
1095}
1096
1097/**
1098 * @brief Destroys the iterator.
1099 *
1100 * Removes the iterator from its registry.
1101 *
1102 * @param the_iterator The chain iterator.
1103 */
1104RTEMS_INLINE_ROUTINE void _Chain_Iterator_destroy(
1105  Chain_Iterator *the_iterator
1106)
1107{
1108  _Chain_Extract_unprotected( &the_iterator->Registry_node );
1109}
1110
[1f0d013]1111/** @} */
[baff4da]1112
[6e93dc4a]1113#ifdef __cplusplus
1114}
1115#endif
1116
[ac7d5ef0]1117#endif
1118/* end of include file */
Note: See TracBrowser for help on using the repository browser.