source: rtems/cpukit/score/include/rtems/score/chainimpl.h @ 864d3475

4.115
Last change on this file since 864d3475 was fbafb8f2, checked in by Josh Oguin <josh.oguin@…>, on 11/19/14 at 20:46:43

chainimpl.h: Add _Assert() to _Chain_Initialize_empty()

CodeSonar? flagged this as a potential NULL deference. That should never
occur but adding the _Assert() ensures we are checking that.

  • Property mode set to 100644
File size: 24.0 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 Extract the specified node from a chain.
84 *
85 *  This routine extracts @a the_node from the chain on which it resides.
86 *  It disables interrupts to ensure the atomicity of the extract operation.
87 *
88 *  @param[in] the_node is the node to be extracted
89 *
90 *  - INTERRUPT LATENCY:
91 *    + single case
92 */
93void _Chain_Extract(
94  Chain_Node *the_node
95);
96
97/**
98 *  @brief Obtain the first node on a chain.
99 *
100 *  This function removes the first node from @a the_chain and returns
101 *  a pointer to that node.  If @a the_chain is empty, then NULL is returned.
102 *
103 *  @retval This method returns a pointer a node.  If a node was removed,
104 *          then a pointer to that node is returned.  If @a the_chain was
105 *          empty, then NULL is returned.
106 *
107 *  @note It disables interrupts to ensure the atomicity of the get operation.
108 */
109Chain_Node *_Chain_Get(
110  Chain_Control *the_chain
111);
112
113/**
114 *  @brief Insert a node on a chain.
115 *
116 *  This routine inserts @a the_node on a chain immediately following
117 *  @a after_node.
118 *
119 *  @param[in] after_node is the pointer to the node in chain to be
120 *             inserted after
121 *  @param[in] the_node is the pointer to the node to be inserted
122 *
123 *  @note It disables interrupts to ensure the atomicity
124 *  of the insert operation.
125 *
126 *  - INTERRUPT LATENCY:
127 *    + single case
128 */
129void _Chain_Insert(
130  Chain_Node *after_node,
131  Chain_Node *the_node
132);
133
134/**
135 *  @brief Append a node on the end of a chain.
136 *
137 *  This routine appends @a the_node onto the end of @a the_chain.
138 *
139 *  @note It disables interrupts to ensure the atomicity of the
140 *  append operation.
141 */
142void _Chain_Append(
143  Chain_Control *the_chain,
144  Chain_Node    *the_node
145);
146
147/**
148 * @brief Append a node and check if the chain was empty before.
149 *
150 * This routine appends the_node onto the end of the_chain.
151 *
152 * @param[in] the_chain is the chain to be operated upon.
153 * @param[in] the_node is the node to be appended.
154 *
155 * @note It disables interrupts to ensure the atomicity of the append
156 * operation.
157 *
158 * @retval true The chain was empty before.
159 * @retval false The chain contained at least one node before.
160 */
161bool _Chain_Append_with_empty_check(
162  Chain_Control *the_chain,
163  Chain_Node    *the_node
164);
165
166/**
167 * @brief Prepend a node and check if the chain was empty before.
168 *
169 * This routine prepends the_node onto the front of the_chain.
170 *
171 * @param[in] the_chain is the chain to be operated upon.
172 * @param[in] the_node is the node to be prepended.
173 *
174 * @note It disables interrupts to ensure the atomicity of the append
175 * operation.
176 *
177 * @retval true The chain was empty before.
178 * @retval false The chain contained at least one node before.
179 */
180bool _Chain_Prepend_with_empty_check(
181  Chain_Control *the_chain,
182  Chain_Node    *the_node
183);
184
185/**
186 * @brief Get the first node and check if the chain is empty afterwards.
187 *
188 * This function removes the first node from the_chain and returns
189 * a pointer to that node in @a the_node.  If the_chain is empty, then NULL is
190 * returned.
191 *
192 * @param[in] the_chain is the chain to attempt to get the first node from.
193 * @param[out] the_node is the first node on the chain or NULL if the chain is
194 * empty.
195 *
196 * @note It disables interrupts to ensure the atomicity of the append
197 * operation.
198 *
199 * @retval true The chain is empty now.
200 * @retval false The chain contains at least one node now.
201 *
202 *  - INTERRUPT LATENCY:
203 *    + single case
204 */
205bool _Chain_Get_with_empty_check(
206  Chain_Control *the_chain,
207  Chain_Node **the_node
208);
209
210/**
211 * @brief Returns the node count of the chain.
212 *
213 * @param[in] chain The chain.
214 *
215 * @note It does NOT disable interrupts to ensure the atomicity of the
216 * operation.
217 *
218 * @retval The node count of the chain.
219 */
220size_t _Chain_Node_count_unprotected( const Chain_Control *chain );
221
222/**
223 * @brief Set off chain.
224 *
225 * This function sets the next field of the @a node to NULL indicating the @a
226 * node is not part of a chain.
227 *
228 * @param[in] node the node set to off chain.
229 */
230RTEMS_INLINE_ROUTINE void _Chain_Set_off_chain(
231  Chain_Node *node
232)
233{
234  node->next = NULL;
235}
236
237/**
238 * @brief Is the node off chain.
239 *
240 * This function returns true if the @a node is not on a chain.  A @a node is
241 * off chain if the next field is set to NULL.
242 *
243 * @param[in] node is the node off chain.
244 *
245 * @retval true The @a node is off chain.
246 * @retval false The @a node is not off chain.
247 */
248RTEMS_INLINE_ROUTINE bool _Chain_Is_node_off_chain(
249  const Chain_Node *node
250)
251{
252  return node->next == NULL;
253}
254
255/**
256 * @brief Are two nodes equal.
257 *
258 * This function returns true if @a left and @a right are equal,
259 * and false otherwise.
260 *
261 * @param[in] left is the node on the left hand side of the comparison.
262 * @param[in] right is the node on the left hand side of the comparison.
263 *
264 * @retval true @a left and @a right are equal.
265 * @retval false @a left and @a right are not equal.
266 */
267RTEMS_INLINE_ROUTINE bool _Chain_Are_nodes_equal(
268  const Chain_Node *left,
269  const Chain_Node *right
270)
271{
272  return left == right;
273}
274
275/**
276 * @brief Is the chain node pointer NULL.
277 *
278 * This function returns true if the_node is NULL and false otherwise.
279 *
280 * @param[in] the_node is the node pointer to check.
281 *
282 * @retval true @a the_node is @c NULL.
283 * @retval false @a the_node is not @c NULL.
284 */
285RTEMS_INLINE_ROUTINE bool _Chain_Is_null_node(
286  const Chain_Node *the_node
287)
288{
289  return (the_node == NULL);
290}
291
292/**
293 * @brief Return pointer to chain head.
294 *
295 * This function returns a pointer to the head node on the chain.
296 *
297 * @param[in] the_chain is the chain to be operated upon.
298 *
299 * @return This method returns the permanent head node of the chain.
300 */
301RTEMS_INLINE_ROUTINE Chain_Node *_Chain_Head(
302  Chain_Control *the_chain
303)
304{
305  return &the_chain->Head.Node;
306}
307
308/**
309 * @brief Return pointer to immutable chain head.
310 *
311 * This function returns a pointer to the head node on the chain.
312 *
313 * @param[in] the_chain is the chain to be operated upon.
314 *
315 * @return This method returns the permanent head node of the chain.
316 */
317RTEMS_INLINE_ROUTINE const Chain_Node *_Chain_Immutable_head(
318  const Chain_Control *the_chain
319)
320{
321  return &the_chain->Head.Node;
322}
323
324/**
325 * @brief Return pointer to chain tail.
326 *
327 * This function returns a pointer to the tail node on the chain.
328 *
329 * @param[in] the_chain is the chain to be operated upon.
330 *
331 * @return This method returns the permanent tail node of the chain.
332 */
333RTEMS_INLINE_ROUTINE Chain_Node *_Chain_Tail(
334  Chain_Control *the_chain
335)
336{
337  return &the_chain->Tail.Node;
338}
339
340/**
341 * @brief Return pointer to immutable chain tail.
342 *
343 * This function returns a pointer to the tail node on the chain.
344 *
345 * @param[in] the_chain is the chain to be operated upon.
346 *
347 * @return This method returns the permanent tail node of the chain.
348 */
349RTEMS_INLINE_ROUTINE const Chain_Node *_Chain_Immutable_tail(
350  const Chain_Control *the_chain
351)
352{
353  return &the_chain->Tail.Node;
354}
355
356/**
357 * @brief Return pointer to chain's first node.
358 *
359 * This function returns a pointer to the first node on the chain after the
360 * head.
361 *
362 * @param[in] the_chain is the chain to be operated upon.
363 *
364 * @return This method returns the first node of the chain.
365 */
366RTEMS_INLINE_ROUTINE Chain_Node *_Chain_First(
367  Chain_Control *the_chain
368)
369{
370  return _Chain_Head( the_chain )->next;
371}
372
373/**
374 * @brief Return pointer to immutable chain's first node.
375 *
376 * This function returns a pointer to the first node on the chain after the
377 * head.
378 *
379 * @param[in] the_chain is the chain to be operated upon.
380 *
381 * @return This method returns the first node of the chain.
382 */
383RTEMS_INLINE_ROUTINE const Chain_Node *_Chain_Immutable_first(
384  const Chain_Control *the_chain
385)
386{
387  return _Chain_Immutable_head( the_chain )->next;
388}
389
390/**
391 * @brief Return pointer to chain's last node.
392 *
393 * This function returns a pointer to the last node on the chain just before
394 * the tail.
395 *
396 * @param[in] the_chain is the chain to be operated upon.
397 *
398 * @return This method returns the last node of the chain.
399 */
400RTEMS_INLINE_ROUTINE Chain_Node *_Chain_Last(
401  Chain_Control *the_chain
402)
403{
404  return _Chain_Tail( the_chain )->previous;
405}
406
407/**
408 * @brief Return pointer to immutable chain's last node.
409 *
410 * This function returns a pointer to the last node on the chain just before
411 * the tail.
412 *
413 * @param[in] the_chain is the chain to be operated upon.
414 *
415 * @return This method returns the last node of the chain.
416 */
417RTEMS_INLINE_ROUTINE const Chain_Node *_Chain_Immutable_last(
418  const Chain_Control *the_chain
419)
420{
421  return _Chain_Immutable_tail( the_chain )->previous;
422}
423
424/**
425 * @brief Return pointer the next node from this node.
426 *
427 * This function returns a pointer to the next node after this node.
428 *
429 * @param[in] the_node is the node to be operated upon.
430 *
431 * @return This method returns the next node on the chain.
432 */
433RTEMS_INLINE_ROUTINE Chain_Node *_Chain_Next(
434  Chain_Node *the_node
435)
436{
437  return the_node->next;
438}
439
440/**
441 * @brief Return pointer the immutable next node from this node.
442 *
443 * This function returns a pointer to the next node after this node.
444 *
445 * @param[in] the_node is the node to be operated upon.
446 *
447 * @return This method returns the next node on the chain.
448 */
449RTEMS_INLINE_ROUTINE const Chain_Node *_Chain_Immutable_next(
450  const Chain_Node *the_node
451)
452{
453  return the_node->next;
454}
455
456/**
457 * @brief Return pointer the previous node from this node.
458 *
459 * This function returns a pointer to the previous node on this chain.
460 *
461 * @param[in] the_node is the node to be operated upon.
462 *
463 * @return This method returns the previous node on the chain.
464 */
465RTEMS_INLINE_ROUTINE Chain_Node *_Chain_Previous(
466  Chain_Node *the_node
467)
468{
469  return the_node->previous;
470}
471
472/**
473 * @brief Return pointer the immutable previous node from this node.
474 *
475 * This function returns a pointer to the previous node on this chain.
476 *
477 * @param[in] the_node is the node to be operated upon.
478 *
479 * @return This method returns the previous node on the chain.
480 */
481RTEMS_INLINE_ROUTINE const Chain_Node *_Chain_Immutable_previous(
482  const Chain_Node *the_node
483)
484{
485  return the_node->previous;
486}
487
488/**
489 * @brief Is the chain empty.
490 *
491 * This function returns true if there a no nodes on @a the_chain and
492 * false otherwise.
493 *
494 * @param[in] the_chain is the chain to be operated upon.
495 *
496 * @retval true There are no nodes on @a the_chain.
497 * @retval false There are nodes on @a the_chain.
498 */
499RTEMS_INLINE_ROUTINE bool _Chain_Is_empty(
500  const Chain_Control *the_chain
501)
502{
503  return _Chain_Immutable_first( the_chain )
504    == _Chain_Immutable_tail( the_chain );
505}
506
507/**
508 * @brief Is this the first node on the chain.
509 *
510 * This function returns true if the_node is the first node on a chain and
511 * false otherwise.
512 *
513 * @param[in] the_node is the node the caller wants to know if it is
514 *            the first node on a chain.
515 *
516 * @retval true @a the_node is the first node on a chain.
517 * @retval false @a the_node is not the first node on a chain.
518 */
519RTEMS_INLINE_ROUTINE bool _Chain_Is_first(
520  const Chain_Node *the_node
521)
522{
523  return (the_node->previous->previous == NULL);
524}
525
526/**
527 * @brief Is this the last node on the chain.
528 *
529 * This function returns true if @a the_node is the last node on a chain and
530 * false otherwise.
531 *
532 * @param[in] the_node is the node to check as the last node.
533 *
534 * @retval true @a the_node is the last node on a chain.
535 * @retval false @a the_node is not the last node on a chain.
536 */
537RTEMS_INLINE_ROUTINE bool _Chain_Is_last(
538  const Chain_Node *the_node
539)
540{
541  return (the_node->next->next == NULL);
542}
543
544/**
545 * @brief Does this chain have only one node.
546 *
547 * This function returns true if there is only one node on @a the_chain and
548 * false otherwise.
549 *
550 * @param[in] the_chain is the chain to be operated upon.
551 *
552 * @return This function returns true if there is only one node on
553 *         @a the_chain and false otherwise.
554 *
555 * @retval true There is only one node on @a the_chain.
556 * @retval false There is more than one node on @a the_chain.
557 */
558RTEMS_INLINE_ROUTINE bool _Chain_Has_only_one_node(
559  const Chain_Control *the_chain
560)
561{
562  return _Chain_Immutable_first( the_chain )
563    == _Chain_Immutable_last( the_chain );
564}
565
566/**
567 * @brief Is this node the chain head.
568 *
569 * This function returns true if @a the_node is the head of @a the_chain and
570 * false otherwise.
571 *
572 * @param[in] the_chain is the chain to be operated upon.
573 * @param[in] the_node is the node to check for being the Chain Head.
574 *
575 * @retval true @a the_node is the head of @a the_chain.
576 * @retval false @a the_node is not the head of @a the_chain.
577 */
578RTEMS_INLINE_ROUTINE bool _Chain_Is_head(
579  const Chain_Control *the_chain,
580  const Chain_Node    *the_node
581)
582{
583  return (the_node == _Chain_Immutable_head( the_chain ));
584}
585
586/**
587 * @brief Is this node the chail tail.
588 *
589 * This function returns true if @a the_node is the tail of @a the_chain and
590 * false otherwise.
591 *
592 * @param[in] the_chain is the chain to be operated upon.
593 * @param[in] the_node is the node to check for being the Chain Tail.
594 *
595 * @retval true @a the_node is the tail of @a the_chain.
596 * @retval false @a the_node is not the tail of @a the_chain.
597 */
598RTEMS_INLINE_ROUTINE bool _Chain_Is_tail(
599  const Chain_Control *the_chain,
600  const Chain_Node    *the_node
601)
602{
603  return (the_node == _Chain_Immutable_tail( the_chain ));
604}
605
606/**
607 * @brief Initialize this chain as empty.
608 *
609 * This routine initializes the specified chain to contain zero nodes.
610 *
611 * @param[in] the_chain is the chain to be initialized.
612 */
613RTEMS_INLINE_ROUTINE void _Chain_Initialize_empty(
614  Chain_Control *the_chain
615)
616{
617  Chain_Node *head;
618  Chain_Node *tail;
619
620  _Assert( the_chain != NULL );
621
622  head = _Chain_Head( the_chain );
623  tail = _Chain_Tail( the_chain );
624
625  head->next = tail;
626  head->previous = NULL;
627  tail->previous = head;
628}
629
630/**
631 * @brief Extract this node (unprotected).
632 *
633 * This routine extracts the_node from the chain on which it resides.
634 * It does NOT disable interrupts to ensure the atomicity of the
635 * extract operation.
636 *
637 * @param[in] the_node is the node to be extracted.
638 */
639RTEMS_INLINE_ROUTINE void _Chain_Extract_unprotected(
640  Chain_Node *the_node
641)
642{
643  Chain_Node *next;
644  Chain_Node *previous;
645
646  next           = the_node->next;
647  previous       = the_node->previous;
648  next->previous = previous;
649  previous->next = next;
650}
651
652/**
653 * @brief Get the first node (unprotected).
654 *
655 * This function removes the first node from the_chain and returns
656 * a pointer to that node.  It does NOT disable interrupts to ensure
657 * the atomicity of the get operation.
658 *
659 * @param[in] the_chain is the chain to attempt to get the first node from.
660 *
661 * @return This method returns the first node on the chain even if it is
662 *         the Chain Tail.
663 *
664 * @note This routine assumes that there is at least one node on the chain
665 *       and always returns a node even if it is the Chain Tail.
666 */
667RTEMS_INLINE_ROUTINE Chain_Node *_Chain_Get_first_unprotected(
668  Chain_Control *the_chain
669)
670{
671  Chain_Node *head = _Chain_Head( the_chain );
672  Chain_Node *old_first = head->next;
673  Chain_Node *new_first = old_first->next;
674
675  head->next = new_first;
676  new_first->previous = head;
677
678  return old_first;
679}
680
681/**
682 * @brief Get the first node (unprotected).
683 *
684 * This function removes the first node from the_chain and returns
685 * a pointer to that node.  If the_chain is empty, then NULL is returned.
686 *
687 * @param[in] the_chain is the chain to attempt to get the first node from.
688 *
689 * @return This method returns the first node on the chain or NULL if the
690 *         chain is empty.
691 *
692 * @note It does NOT disable interrupts to ensure the atomicity of the
693 *       get operation.
694 */
695RTEMS_INLINE_ROUTINE Chain_Node *_Chain_Get_unprotected(
696  Chain_Control *the_chain
697)
698{
699  if ( !_Chain_Is_empty(the_chain))
700    return _Chain_Get_first_unprotected(the_chain);
701  else
702    return NULL;
703}
704
705/**
706 * @brief Insert a node (unprotected).
707 *
708 * This routine inserts the_node on a chain immediately following
709 * after_node.
710 *
711 * @param[in] after_node is the node which will precede @a the_node on the
712 *            chain.
713 * @param[in] the_node is the node to be inserted.
714 *
715 * @note It does NOT disable interrupts to ensure the atomicity
716 *       of the extract operation.
717 */
718RTEMS_INLINE_ROUTINE void _Chain_Insert_unprotected(
719  Chain_Node *after_node,
720  Chain_Node *the_node
721)
722{
723  Chain_Node *before_node;
724
725  the_node->previous    = after_node;
726  before_node           = after_node->next;
727  after_node->next      = the_node;
728  the_node->next        = before_node;
729  before_node->previous = the_node;
730}
731
732/**
733 * @brief Append a node (unprotected).
734 *
735 * This routine appends the_node onto the end of the_chain.
736 *
737 * @param[in] the_chain is the chain to be operated upon.
738 * @param[in] the_node is the node to be appended.
739 *
740 * @note It does NOT disable interrupts to ensure the atomicity of the
741 *       append operation.
742 */
743RTEMS_INLINE_ROUTINE void _Chain_Append_unprotected(
744  Chain_Control *the_chain,
745  Chain_Node    *the_node
746)
747{
748  Chain_Node *tail = _Chain_Tail( the_chain );
749  Chain_Node *old_last = tail->previous;
750
751  the_node->next = tail;
752  tail->previous = the_node;
753  old_last->next = the_node;
754  the_node->previous = old_last;
755}
756
757/**
758 * @brief Append a node on the end of a chain if the node is in the off chain
759 * state (unprotected).
760 *
761 * @note It does NOT disable interrupts to ensure the atomicity of the
762 *       append operation.
763 *
764 * @see _Chain_Append_unprotected() and _Chain_Is_node_off_chain().
765 */
766RTEMS_INLINE_ROUTINE void _Chain_Append_if_is_off_chain_unprotected(
767  Chain_Control *the_chain,
768  Chain_Node    *the_node
769)
770{
771  if ( _Chain_Is_node_off_chain( the_node ) ) {
772    _Chain_Append_unprotected( the_chain, the_node );
773  }
774}
775
776/**
777 * @brief Prepend a node (unprotected).
778 *
779 * This routine prepends the_node onto the front of the_chain.
780 *
781 * @param[in] the_chain is the chain to be operated upon.
782 * @param[in] the_node is the node to be prepended.
783 *
784 * @note It does NOT disable interrupts to ensure the atomicity of the
785 *       prepend operation.
786 */
787RTEMS_INLINE_ROUTINE void _Chain_Prepend_unprotected(
788  Chain_Control *the_chain,
789  Chain_Node    *the_node
790)
791{
792  _Chain_Insert_unprotected(_Chain_Head(the_chain), the_node);
793}
794
795/**
796 * @brief Prepend a node (protected).
797 *
798 * This routine prepends the_node onto the front of the_chain.
799 *
800 * @param[in] the_chain is the chain to be operated upon.
801 * @param[in] the_node is the node to be prepended.
802 *
803 * @note It disables interrupts to ensure the atomicity of the
804 *       prepend operation.
805 */
806RTEMS_INLINE_ROUTINE void _Chain_Prepend(
807  Chain_Control *the_chain,
808  Chain_Node    *the_node
809)
810{
811  _Chain_Insert(_Chain_Head(the_chain), the_node);
812}
813
814/**
815 * @brief Append a node and check if the chain was empty before (unprotected).
816 *
817 * This routine appends the_node onto the end of the_chain.
818 *
819 * @param[in] the_chain is the chain to be operated upon.
820 * @param[in] the_node is the node to be appended.
821 *
822 * @note It does NOT disable interrupts to ensure the atomicity of the
823 *       append operation.
824 *
825 * @retval true The chain was empty before.
826 * @retval false The chain contained at least one node before.
827 */
828RTEMS_INLINE_ROUTINE bool _Chain_Append_with_empty_check_unprotected(
829  Chain_Control *the_chain,
830  Chain_Node    *the_node
831)
832{
833  bool was_empty = _Chain_Is_empty( the_chain );
834
835  _Chain_Append_unprotected( the_chain, the_node );
836
837  return was_empty;
838}
839
840/**
841 * @brief Prepend a node and check if the chain was empty before (unprotected).
842 *
843 * This routine prepends the_node onto the front of the_chain.
844 *
845 * @param[in] the_chain is the chain to be operated upon.
846 * @param[in] the_node is the node to be prepended.
847 *
848 * @note It does NOT disable interrupts to ensure the atomicity of the
849 *       prepend operation.
850 *
851 * @retval true The chain was empty before.
852 * @retval false The chain contained at least one node before.
853 */
854RTEMS_INLINE_ROUTINE bool _Chain_Prepend_with_empty_check_unprotected(
855  Chain_Control *the_chain,
856  Chain_Node    *the_node
857)
858{
859  bool was_empty = _Chain_Is_empty( the_chain );
860
861  _Chain_Prepend_unprotected( the_chain, the_node );
862
863  return was_empty;
864}
865
866/**
867 * @brief Get the first node and check if the chain is empty afterwards
868 * (unprotected).
869 *
870 * This function removes the first node from the_chain and returns
871 * a pointer to that node in @a the_node.  If the_chain is empty, then NULL is
872 * returned.
873 *
874 * @param[in] the_chain is the chain to attempt to get the first node from.
875 * @param[out] the_node is the first node on the chain or NULL if the chain is
876 * empty.
877 *
878 * @note It does NOT disable interrupts to ensure the atomicity of the
879 *       get operation.
880 *
881 * @retval true The chain is empty now.
882 * @retval false The chain contains at least one node now.
883 */
884RTEMS_INLINE_ROUTINE bool _Chain_Get_with_empty_check_unprotected(
885  Chain_Control *the_chain,
886  Chain_Node **the_node
887)
888{
889  bool is_empty_now = true;
890  Chain_Node *head = _Chain_Head( the_chain );
891  Chain_Node *tail = _Chain_Tail( the_chain );
892  Chain_Node *old_first = head->next;
893
894  if ( old_first != tail ) {
895    Chain_Node *new_first = old_first->next;
896
897    head->next = new_first;
898    new_first->previous = head;
899
900    *the_node = old_first;
901
902    is_empty_now = new_first == tail;
903  } else
904    *the_node = NULL;
905
906  return is_empty_now;
907}
908
909/**
910 * @brief Chain node order.
911 *
912 * @param[in] left The left node.
913 * @param[in] right The right node.
914 *
915 * @retval true According to the order the left node precedes the right node.
916 * @retval false Otherwise.
917 */
918typedef bool ( *Chain_Node_order )(
919  const Chain_Node *left,
920  const Chain_Node *right
921);
922
923/**
924 * @brief Inserts a node into the chain according to the order relation.
925 *
926 * After the operation the chain contains the node to insert and the order
927 * relation holds for all nodes from the head up to the inserted node.  Nodes
928 * after the inserted node are not moved.
929 *
930 * @param[in,out] chain The chain.
931 * @param[in,out] to_insert The node to insert.
932 * @param[in] order The order relation.
933 */
934RTEMS_INLINE_ROUTINE void _Chain_Insert_ordered_unprotected(
935  Chain_Control *chain,
936  Chain_Node *to_insert,
937  Chain_Node_order order
938)
939{
940  const Chain_Node *tail = _Chain_Immutable_tail( chain );
941  Chain_Node *next = _Chain_First( chain );
942
943  while ( next != tail && !( *order )( to_insert, next ) ) {
944    next = _Chain_Next( next );
945  }
946
947  _Chain_Insert_unprotected( _Chain_Previous( next ), to_insert );
948}
949
950/** @} */
951
952#ifdef __cplusplus
953}
954#endif
955
956#endif
957/* end of include file */
Note: See TracBrowser for help on using the repository browser.