source: rtems/cpukit/include/rtems/score/chainimpl.h @ 878487b0

5
Last change on this file since 878487b0 was ea0a680, checked in by Sebastian Huber <sebastian.huber@…>, on 09/10/18 at 08:36:41

score: Debug aid for _Chain_Extract_unprotected()

Ensure that a chain node is not off chain while doing the chain extract.

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