source: rtems/cpukit/include/rtems/score/chainimpl.h @ 956d76cc

5
Last change on this file since 956d76cc was 956d76cc, checked in by Sebastian Huber <sebastian.huber@…>, on 01/07/19 at 08:30:43

score: Remove superfluous include from chainimpl.h

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