1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief Chain Handler API |
---|
5 | */ |
---|
6 | |
---|
7 | /* |
---|
8 | * Copyright (c) 2010 embedded brains GmbH. |
---|
9 | * |
---|
10 | * COPYRIGHT (c) 1989-2006. |
---|
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.com/license/LICENSE. |
---|
16 | */ |
---|
17 | |
---|
18 | #ifndef _RTEMS_SCORE_CHAIN_H |
---|
19 | # error "Never use <rtems/score/chain.inl> directly; include <rtems/score/chain.h> instead." |
---|
20 | #endif |
---|
21 | |
---|
22 | #ifndef _RTEMS_SCORE_CHAIN_INL |
---|
23 | #define _RTEMS_SCORE_CHAIN_INL |
---|
24 | |
---|
25 | /** |
---|
26 | * @addtogroup ScoreChain |
---|
27 | */ |
---|
28 | /**@{**/ |
---|
29 | |
---|
30 | /** |
---|
31 | * @brief Set off chain. |
---|
32 | * |
---|
33 | * This function sets the next and previous fields of the @a node to NULL |
---|
34 | * indicating the @a node is not part of a chain. |
---|
35 | * |
---|
36 | * @param[in] node the node set to off chain. |
---|
37 | */ |
---|
38 | RTEMS_INLINE_ROUTINE void _Chain_Set_off_chain( |
---|
39 | Chain_Node *node |
---|
40 | ) |
---|
41 | { |
---|
42 | node->next = node->previous = NULL; |
---|
43 | } |
---|
44 | |
---|
45 | /** |
---|
46 | * @brief Is the node off chain. |
---|
47 | * |
---|
48 | * This function returns true if the @a node is not on a chain. A @a node is |
---|
49 | * off chain if the next and previous fields are set to NULL. |
---|
50 | * |
---|
51 | * @param[in] node is the node off chain. |
---|
52 | * |
---|
53 | * @retval true The @a node is off chain. |
---|
54 | * @retval false The @a node is not off chain. |
---|
55 | */ |
---|
56 | RTEMS_INLINE_ROUTINE bool _Chain_Is_node_off_chain( |
---|
57 | const Chain_Node *node |
---|
58 | ) |
---|
59 | { |
---|
60 | return (node->next == NULL) && (node->previous == NULL); |
---|
61 | } |
---|
62 | |
---|
63 | /** |
---|
64 | * @brief Are two nodes equal. |
---|
65 | * |
---|
66 | * This function returns true if @a left and @a right are equal, |
---|
67 | * and false otherwise. |
---|
68 | * |
---|
69 | * @param[in] left is the node on the left hand side of the comparison. |
---|
70 | * @param[in] right is the node on the left hand side of the comparison. |
---|
71 | * |
---|
72 | * @retval true @a left and @a right are equal. |
---|
73 | * @retval false @a left and @a right are not equal. |
---|
74 | */ |
---|
75 | RTEMS_INLINE_ROUTINE bool _Chain_Are_nodes_equal( |
---|
76 | const Chain_Node *left, |
---|
77 | const Chain_Node *right |
---|
78 | ) |
---|
79 | { |
---|
80 | return left == right; |
---|
81 | } |
---|
82 | |
---|
83 | /** |
---|
84 | * @brief Is this chain control pointer NULL. |
---|
85 | * |
---|
86 | * This function returns true if the_chain is NULL and false otherwise. |
---|
87 | * |
---|
88 | * @param[in] the_chain is the chain to be checked for empty status. |
---|
89 | * |
---|
90 | * @retval true @a the_chain is @c NULL. |
---|
91 | * @retval false @a the_chain is not @c NULL. |
---|
92 | */ |
---|
93 | RTEMS_INLINE_ROUTINE bool _Chain_Is_null( |
---|
94 | const Chain_Control *the_chain |
---|
95 | ) |
---|
96 | { |
---|
97 | return (the_chain == NULL); |
---|
98 | } |
---|
99 | |
---|
100 | /** |
---|
101 | * @brief Is the chain node pointer NULL. |
---|
102 | * |
---|
103 | * This function returns true if the_node is NULL and false otherwise. |
---|
104 | * |
---|
105 | * @param[in] the_node is the node pointer to check. |
---|
106 | * |
---|
107 | * @retval true @a the_node is @c NULL. |
---|
108 | * @retval false @a the_node is not @c NULL. |
---|
109 | */ |
---|
110 | RTEMS_INLINE_ROUTINE bool _Chain_Is_null_node( |
---|
111 | const Chain_Node *the_node |
---|
112 | ) |
---|
113 | { |
---|
114 | return (the_node == NULL); |
---|
115 | } |
---|
116 | |
---|
117 | /** |
---|
118 | * @brief Return pointer to chain head. |
---|
119 | * |
---|
120 | * This function returns a pointer to the head node on the chain. |
---|
121 | * |
---|
122 | * @param[in] the_chain is the chain to be operated upon. |
---|
123 | * |
---|
124 | * @return This method returns the permanent head node of the chain. |
---|
125 | */ |
---|
126 | RTEMS_INLINE_ROUTINE Chain_Node *_Chain_Head( |
---|
127 | Chain_Control *the_chain |
---|
128 | ) |
---|
129 | { |
---|
130 | return &the_chain->Head.Node; |
---|
131 | } |
---|
132 | |
---|
133 | /** |
---|
134 | * @brief Return pointer to immutable chain head. |
---|
135 | * |
---|
136 | * This function returns a pointer to the head node on the chain. |
---|
137 | * |
---|
138 | * @param[in] the_chain is the chain to be operated upon. |
---|
139 | * |
---|
140 | * @return This method returns the permanent head node of the chain. |
---|
141 | */ |
---|
142 | RTEMS_INLINE_ROUTINE const Chain_Node *_Chain_Immutable_head( |
---|
143 | const Chain_Control *the_chain |
---|
144 | ) |
---|
145 | { |
---|
146 | return &the_chain->Head.Node; |
---|
147 | } |
---|
148 | |
---|
149 | /** |
---|
150 | * @brief Return pointer to chain tail. |
---|
151 | * |
---|
152 | * This function returns a pointer to the tail node on the chain. |
---|
153 | * |
---|
154 | * @param[in] the_chain is the chain to be operated upon. |
---|
155 | * |
---|
156 | * @return This method returns the permanent tail node of the chain. |
---|
157 | */ |
---|
158 | RTEMS_INLINE_ROUTINE Chain_Node *_Chain_Tail( |
---|
159 | Chain_Control *the_chain |
---|
160 | ) |
---|
161 | { |
---|
162 | return &the_chain->Tail.Node; |
---|
163 | } |
---|
164 | |
---|
165 | /** |
---|
166 | * @brief Return pointer to immutable chain tail. |
---|
167 | * |
---|
168 | * This function returns a pointer to the tail node on the chain. |
---|
169 | * |
---|
170 | * @param[in] the_chain is the chain to be operated upon. |
---|
171 | * |
---|
172 | * @return This method returns the permanent tail node of the chain. |
---|
173 | */ |
---|
174 | RTEMS_INLINE_ROUTINE const Chain_Node *_Chain_Immutable_tail( |
---|
175 | const Chain_Control *the_chain |
---|
176 | ) |
---|
177 | { |
---|
178 | return &the_chain->Tail.Node; |
---|
179 | } |
---|
180 | |
---|
181 | /** |
---|
182 | * @brief Return pointer to chain's first node. |
---|
183 | * |
---|
184 | * This function returns a pointer to the first node on the chain after the |
---|
185 | * head. |
---|
186 | * |
---|
187 | * @param[in] the_chain is the chain to be operated upon. |
---|
188 | * |
---|
189 | * @return This method returns the first node of the chain. |
---|
190 | */ |
---|
191 | RTEMS_INLINE_ROUTINE Chain_Node *_Chain_First( |
---|
192 | Chain_Control *the_chain |
---|
193 | ) |
---|
194 | { |
---|
195 | return _Chain_Head( the_chain )->next; |
---|
196 | } |
---|
197 | |
---|
198 | /** |
---|
199 | * @brief Return pointer to immutable chain's first node. |
---|
200 | * |
---|
201 | * This function returns a pointer to the first node on the chain after the |
---|
202 | * head. |
---|
203 | * |
---|
204 | * @param[in] the_chain is the chain to be operated upon. |
---|
205 | * |
---|
206 | * @return This method returns the first node of the chain. |
---|
207 | */ |
---|
208 | RTEMS_INLINE_ROUTINE const Chain_Node *_Chain_Immutable_first( |
---|
209 | const Chain_Control *the_chain |
---|
210 | ) |
---|
211 | { |
---|
212 | return _Chain_Immutable_head( the_chain )->next; |
---|
213 | } |
---|
214 | |
---|
215 | /** |
---|
216 | * @brief Return pointer to chain's last node. |
---|
217 | * |
---|
218 | * This function returns a pointer to the last node on the chain just before |
---|
219 | * the tail. |
---|
220 | * |
---|
221 | * @param[in] the_chain is the chain to be operated upon. |
---|
222 | * |
---|
223 | * @return This method returns the last node of the chain. |
---|
224 | */ |
---|
225 | RTEMS_INLINE_ROUTINE Chain_Node *_Chain_Last( |
---|
226 | Chain_Control *the_chain |
---|
227 | ) |
---|
228 | { |
---|
229 | return _Chain_Tail( the_chain )->previous; |
---|
230 | } |
---|
231 | |
---|
232 | /** |
---|
233 | * @brief Return pointer to immutable chain's last node. |
---|
234 | * |
---|
235 | * This function returns a pointer to the last node on the chain just before |
---|
236 | * the tail. |
---|
237 | * |
---|
238 | * @param[in] the_chain is the chain to be operated upon. |
---|
239 | * |
---|
240 | * @return This method returns the last node of the chain. |
---|
241 | */ |
---|
242 | RTEMS_INLINE_ROUTINE const Chain_Node *_Chain_Immutable_last( |
---|
243 | const Chain_Control *the_chain |
---|
244 | ) |
---|
245 | { |
---|
246 | return _Chain_Immutable_tail( the_chain )->previous; |
---|
247 | } |
---|
248 | |
---|
249 | /** |
---|
250 | * @brief Return pointer the next node from this node. |
---|
251 | * |
---|
252 | * This function returns a pointer to the next node after this node. |
---|
253 | * |
---|
254 | * @param[in] the_node is the node to be operated upon. |
---|
255 | * |
---|
256 | * @return This method returns the next node on the chain. |
---|
257 | */ |
---|
258 | RTEMS_INLINE_ROUTINE Chain_Node *_Chain_Next( |
---|
259 | Chain_Node *the_node |
---|
260 | ) |
---|
261 | { |
---|
262 | return the_node->next; |
---|
263 | } |
---|
264 | |
---|
265 | /** |
---|
266 | * @brief Return pointer the immutable next node from this node. |
---|
267 | * |
---|
268 | * This function returns a pointer to the next node after this node. |
---|
269 | * |
---|
270 | * @param[in] the_node is the node to be operated upon. |
---|
271 | * |
---|
272 | * @return This method returns the next node on the chain. |
---|
273 | */ |
---|
274 | RTEMS_INLINE_ROUTINE const Chain_Node *_Chain_Immutable_next( |
---|
275 | const Chain_Node *the_node |
---|
276 | ) |
---|
277 | { |
---|
278 | return the_node->next; |
---|
279 | } |
---|
280 | |
---|
281 | /** |
---|
282 | * @brief Return pointer the previous node from this node. |
---|
283 | * |
---|
284 | * This function returns a pointer to the previous node on this chain. |
---|
285 | * |
---|
286 | * @param[in] the_node is the node to be operated upon. |
---|
287 | * |
---|
288 | * @return This method returns the previous node on the chain. |
---|
289 | */ |
---|
290 | RTEMS_INLINE_ROUTINE Chain_Node *_Chain_Previous( |
---|
291 | Chain_Node *the_node |
---|
292 | ) |
---|
293 | { |
---|
294 | return the_node->previous; |
---|
295 | } |
---|
296 | |
---|
297 | /** |
---|
298 | * @brief Return pointer the immutable previous node from this node. |
---|
299 | * |
---|
300 | * This function returns a pointer to the previous node on this chain. |
---|
301 | * |
---|
302 | * @param[in] the_node is the node to be operated upon. |
---|
303 | * |
---|
304 | * @return This method returns the previous node on the chain. |
---|
305 | */ |
---|
306 | RTEMS_INLINE_ROUTINE const Chain_Node *_Chain_Immutable_previous( |
---|
307 | const Chain_Node *the_node |
---|
308 | ) |
---|
309 | { |
---|
310 | return the_node->previous; |
---|
311 | } |
---|
312 | |
---|
313 | /** |
---|
314 | * @brief Is the chain empty. |
---|
315 | * |
---|
316 | * This function returns true if there a no nodes on @a the_chain and |
---|
317 | * false otherwise. |
---|
318 | * |
---|
319 | * @param[in] the_chain is the chain to be operated upon. |
---|
320 | * |
---|
321 | * @retval true There are no nodes on @a the_chain. |
---|
322 | * @retval false There are nodes on @a the_chain. |
---|
323 | */ |
---|
324 | RTEMS_INLINE_ROUTINE bool _Chain_Is_empty( |
---|
325 | const Chain_Control *the_chain |
---|
326 | ) |
---|
327 | { |
---|
328 | return _Chain_Immutable_first( the_chain ) |
---|
329 | == _Chain_Immutable_tail( the_chain ); |
---|
330 | } |
---|
331 | |
---|
332 | /** |
---|
333 | * @brief Is this the first node on the chain. |
---|
334 | * |
---|
335 | * This function returns true if the_node is the first node on a chain and |
---|
336 | * false otherwise. |
---|
337 | * |
---|
338 | * @param[in] the_node is the node the caller wants to know if it is |
---|
339 | * the first node on a chain. |
---|
340 | * |
---|
341 | * @retval true @a the_node is the first node on a chain. |
---|
342 | * @retval false @a the_node is not the first node on a chain. |
---|
343 | */ |
---|
344 | RTEMS_INLINE_ROUTINE bool _Chain_Is_first( |
---|
345 | const Chain_Node *the_node |
---|
346 | ) |
---|
347 | { |
---|
348 | return (the_node->previous->previous == NULL); |
---|
349 | } |
---|
350 | |
---|
351 | /** |
---|
352 | * @brief Is this the last node on the chain. |
---|
353 | * |
---|
354 | * This function returns true if @a the_node is the last node on a chain and |
---|
355 | * false otherwise. |
---|
356 | * |
---|
357 | * @param[in] the_node is the node to check as the last node. |
---|
358 | * |
---|
359 | * @retval true @a the_node is the last node on a chain. |
---|
360 | * @retval false @a the_node is not the last node on a chain. |
---|
361 | */ |
---|
362 | RTEMS_INLINE_ROUTINE bool _Chain_Is_last( |
---|
363 | const Chain_Node *the_node |
---|
364 | ) |
---|
365 | { |
---|
366 | return (the_node->next->next == NULL); |
---|
367 | } |
---|
368 | |
---|
369 | /** |
---|
370 | * @brief Does this chain have only one node. |
---|
371 | * |
---|
372 | * This function returns true if there is only one node on @a the_chain and |
---|
373 | * false otherwise. |
---|
374 | * |
---|
375 | * @param[in] the_chain is the chain to be operated upon. |
---|
376 | * |
---|
377 | * @return This function returns true if there is only one node on |
---|
378 | * @a the_chain and false otherwise. |
---|
379 | * |
---|
380 | * @retval true There is only one node on @a the_chain. |
---|
381 | * @retval false There is more than one node on @a the_chain. |
---|
382 | */ |
---|
383 | RTEMS_INLINE_ROUTINE bool _Chain_Has_only_one_node( |
---|
384 | const Chain_Control *the_chain |
---|
385 | ) |
---|
386 | { |
---|
387 | return _Chain_Immutable_first( the_chain ) |
---|
388 | == _Chain_Immutable_last( the_chain ); |
---|
389 | } |
---|
390 | |
---|
391 | /** |
---|
392 | * @brief Is this node the chain head. |
---|
393 | * |
---|
394 | * This function returns true if @a the_node is the head of @a the_chain and |
---|
395 | * false otherwise. |
---|
396 | * |
---|
397 | * @param[in] the_chain is the chain to be operated upon. |
---|
398 | * @param[in] the_node is the node to check for being the Chain Head. |
---|
399 | * |
---|
400 | * @retval true @a the_node is the head of @a the_chain. |
---|
401 | * @retval false @a the_node is not the head of @a the_chain. |
---|
402 | */ |
---|
403 | RTEMS_INLINE_ROUTINE bool _Chain_Is_head( |
---|
404 | const Chain_Control *the_chain, |
---|
405 | const Chain_Node *the_node |
---|
406 | ) |
---|
407 | { |
---|
408 | return (the_node == _Chain_Immutable_head( the_chain )); |
---|
409 | } |
---|
410 | |
---|
411 | /** |
---|
412 | * @brief Is this node the chail tail. |
---|
413 | * |
---|
414 | * This function returns true if @a the_node is the tail of @a the_chain and |
---|
415 | * false otherwise. |
---|
416 | * |
---|
417 | * @param[in] the_chain is the chain to be operated upon. |
---|
418 | * @param[in] the_node is the node to check for being the Chain Tail. |
---|
419 | * |
---|
420 | * @retval true @a the_node is the tail of @a the_chain. |
---|
421 | * @retval false @a the_node is not the tail of @a the_chain. |
---|
422 | */ |
---|
423 | RTEMS_INLINE_ROUTINE bool _Chain_Is_tail( |
---|
424 | const Chain_Control *the_chain, |
---|
425 | const Chain_Node *the_node |
---|
426 | ) |
---|
427 | { |
---|
428 | return (the_node == _Chain_Immutable_tail( the_chain )); |
---|
429 | } |
---|
430 | |
---|
431 | /** |
---|
432 | * @brief Initialize this chain as empty. |
---|
433 | * |
---|
434 | * This routine initializes the specified chain to contain zero nodes. |
---|
435 | * |
---|
436 | * @param[in] the_chain is the chain to be initialized. |
---|
437 | */ |
---|
438 | RTEMS_INLINE_ROUTINE void _Chain_Initialize_empty( |
---|
439 | Chain_Control *the_chain |
---|
440 | ) |
---|
441 | { |
---|
442 | Chain_Node *head = _Chain_Head( the_chain ); |
---|
443 | Chain_Node *tail = _Chain_Tail( the_chain ); |
---|
444 | |
---|
445 | head->next = tail; |
---|
446 | head->previous = NULL; |
---|
447 | tail->previous = head; |
---|
448 | } |
---|
449 | |
---|
450 | /** |
---|
451 | * @brief Extract this node (unprotected). |
---|
452 | * |
---|
453 | * This routine extracts the_node from the chain on which it resides. |
---|
454 | * It does NOT disable interrupts to ensure the atomicity of the |
---|
455 | * extract operation. |
---|
456 | * |
---|
457 | * @param[in] the_node is the node to be extracted. |
---|
458 | */ |
---|
459 | RTEMS_INLINE_ROUTINE void _Chain_Extract_unprotected( |
---|
460 | Chain_Node *the_node |
---|
461 | ) |
---|
462 | { |
---|
463 | Chain_Node *next; |
---|
464 | Chain_Node *previous; |
---|
465 | |
---|
466 | next = the_node->next; |
---|
467 | previous = the_node->previous; |
---|
468 | next->previous = previous; |
---|
469 | previous->next = next; |
---|
470 | } |
---|
471 | |
---|
472 | /** |
---|
473 | * @brief Get the first node (unprotected). |
---|
474 | * |
---|
475 | * This function removes the first node from the_chain and returns |
---|
476 | * a pointer to that node. It does NOT disable interrupts to ensure |
---|
477 | * the atomicity of the get operation. |
---|
478 | * |
---|
479 | * @param[in] the_chain is the chain to attempt to get the first node from. |
---|
480 | * |
---|
481 | * @return This method returns the first node on the chain even if it is |
---|
482 | * the Chain Tail. |
---|
483 | * |
---|
484 | * @note This routine assumes that there is at least one node on the chain |
---|
485 | * and always returns a node even if it is the Chain Tail. |
---|
486 | */ |
---|
487 | RTEMS_INLINE_ROUTINE Chain_Node *_Chain_Get_first_unprotected( |
---|
488 | Chain_Control *the_chain |
---|
489 | ) |
---|
490 | { |
---|
491 | Chain_Node *head = _Chain_Head( the_chain ); |
---|
492 | Chain_Node *old_first = head->next; |
---|
493 | Chain_Node *new_first = old_first->next; |
---|
494 | |
---|
495 | head->next = new_first; |
---|
496 | new_first->previous = head; |
---|
497 | |
---|
498 | return old_first; |
---|
499 | } |
---|
500 | |
---|
501 | /** |
---|
502 | * @brief Get the first node (unprotected). |
---|
503 | * |
---|
504 | * This function removes the first node from the_chain and returns |
---|
505 | * a pointer to that node. If the_chain is empty, then NULL is returned. |
---|
506 | * |
---|
507 | * @param[in] the_chain is the chain to attempt to get the first node from. |
---|
508 | * |
---|
509 | * @return This method returns the first node on the chain or NULL if the |
---|
510 | * chain is empty. |
---|
511 | * |
---|
512 | * @note It does NOT disable interrupts to ensure the atomicity of the |
---|
513 | * get operation. |
---|
514 | */ |
---|
515 | RTEMS_INLINE_ROUTINE Chain_Node *_Chain_Get_unprotected( |
---|
516 | Chain_Control *the_chain |
---|
517 | ) |
---|
518 | { |
---|
519 | if ( !_Chain_Is_empty(the_chain)) |
---|
520 | return _Chain_Get_first_unprotected(the_chain); |
---|
521 | else |
---|
522 | return NULL; |
---|
523 | } |
---|
524 | |
---|
525 | /** |
---|
526 | * @brief Insert a node (unprotected). |
---|
527 | * |
---|
528 | * This routine inserts the_node on a chain immediately following |
---|
529 | * after_node. |
---|
530 | * |
---|
531 | * @param[in] after_node is the node which will precede @a the_node on the |
---|
532 | * chain. |
---|
533 | * @param[in] the_node is the node to be inserted. |
---|
534 | * |
---|
535 | * @note It does NOT disable interrupts to ensure the atomicity |
---|
536 | * of the extract operation. |
---|
537 | */ |
---|
538 | RTEMS_INLINE_ROUTINE void _Chain_Insert_unprotected( |
---|
539 | Chain_Node *after_node, |
---|
540 | Chain_Node *the_node |
---|
541 | ) |
---|
542 | { |
---|
543 | Chain_Node *before_node; |
---|
544 | |
---|
545 | the_node->previous = after_node; |
---|
546 | before_node = after_node->next; |
---|
547 | after_node->next = the_node; |
---|
548 | the_node->next = before_node; |
---|
549 | before_node->previous = the_node; |
---|
550 | } |
---|
551 | |
---|
552 | /** |
---|
553 | * @brief Append a node (unprotected). |
---|
554 | * |
---|
555 | * This routine appends the_node onto the end of the_chain. |
---|
556 | * |
---|
557 | * @param[in] the_chain is the chain to be operated upon. |
---|
558 | * @param[in] the_node is the node to be appended. |
---|
559 | * |
---|
560 | * @note It does NOT disable interrupts to ensure the atomicity of the |
---|
561 | * append operation. |
---|
562 | */ |
---|
563 | RTEMS_INLINE_ROUTINE void _Chain_Append_unprotected( |
---|
564 | Chain_Control *the_chain, |
---|
565 | Chain_Node *the_node |
---|
566 | ) |
---|
567 | { |
---|
568 | Chain_Node *tail = _Chain_Tail( the_chain ); |
---|
569 | Chain_Node *old_last = tail->previous; |
---|
570 | |
---|
571 | the_node->next = tail; |
---|
572 | tail->previous = the_node; |
---|
573 | old_last->next = the_node; |
---|
574 | the_node->previous = old_last; |
---|
575 | } |
---|
576 | |
---|
577 | /** |
---|
578 | * @brief Append a node on the end of a chain if the node is in the off chain |
---|
579 | * state (unprotected). |
---|
580 | * |
---|
581 | * @note It does NOT disable interrupts to ensure the atomicity of the |
---|
582 | * append operation. |
---|
583 | * |
---|
584 | * @see _Chain_Append_unprotected() and _Chain_Is_node_off_chain(). |
---|
585 | */ |
---|
586 | RTEMS_INLINE_ROUTINE void _Chain_Append_if_is_off_chain_unprotected( |
---|
587 | Chain_Control *the_chain, |
---|
588 | Chain_Node *the_node |
---|
589 | ) |
---|
590 | { |
---|
591 | if ( _Chain_Is_node_off_chain( the_node ) ) { |
---|
592 | _Chain_Append_unprotected( the_chain, the_node ); |
---|
593 | } |
---|
594 | } |
---|
595 | |
---|
596 | /** |
---|
597 | * @brief Prepend a node (unprotected). |
---|
598 | * |
---|
599 | * This routine prepends the_node onto the front of the_chain. |
---|
600 | * |
---|
601 | * @param[in] the_chain is the chain to be operated upon. |
---|
602 | * @param[in] the_node is the node to be prepended. |
---|
603 | * |
---|
604 | * @note It does NOT disable interrupts to ensure the atomicity of the |
---|
605 | * prepend operation. |
---|
606 | */ |
---|
607 | RTEMS_INLINE_ROUTINE void _Chain_Prepend_unprotected( |
---|
608 | Chain_Control *the_chain, |
---|
609 | Chain_Node *the_node |
---|
610 | ) |
---|
611 | { |
---|
612 | _Chain_Insert_unprotected(_Chain_Head(the_chain), the_node); |
---|
613 | } |
---|
614 | |
---|
615 | /** |
---|
616 | * @brief Prepend a node (protected). |
---|
617 | * |
---|
618 | * This routine prepends the_node onto the front of the_chain. |
---|
619 | * |
---|
620 | * @param[in] the_chain is the chain to be operated upon. |
---|
621 | * @param[in] the_node is the node to be prepended. |
---|
622 | * |
---|
623 | * @note It disables interrupts to ensure the atomicity of the |
---|
624 | * prepend operation. |
---|
625 | */ |
---|
626 | RTEMS_INLINE_ROUTINE void _Chain_Prepend( |
---|
627 | Chain_Control *the_chain, |
---|
628 | Chain_Node *the_node |
---|
629 | ) |
---|
630 | { |
---|
631 | _Chain_Insert(_Chain_Head(the_chain), the_node); |
---|
632 | } |
---|
633 | |
---|
634 | /** |
---|
635 | * @brief Append a node and check if the chain was empty before (unprotected). |
---|
636 | * |
---|
637 | * This routine appends the_node onto the end of the_chain. |
---|
638 | * |
---|
639 | * @param[in] the_chain is the chain to be operated upon. |
---|
640 | * @param[in] the_node is the node to be appended. |
---|
641 | * |
---|
642 | * @note It does NOT disable interrupts to ensure the atomicity of the |
---|
643 | * append operation. |
---|
644 | * |
---|
645 | * @retval true The chain was empty before. |
---|
646 | * @retval false The chain contained at least one node before. |
---|
647 | */ |
---|
648 | RTEMS_INLINE_ROUTINE bool _Chain_Append_with_empty_check_unprotected( |
---|
649 | Chain_Control *the_chain, |
---|
650 | Chain_Node *the_node |
---|
651 | ) |
---|
652 | { |
---|
653 | bool was_empty = _Chain_Is_empty( the_chain ); |
---|
654 | |
---|
655 | _Chain_Append_unprotected( the_chain, the_node ); |
---|
656 | |
---|
657 | return was_empty; |
---|
658 | } |
---|
659 | |
---|
660 | /** |
---|
661 | * @brief Prepend a node and check if the chain was empty before (unprotected). |
---|
662 | * |
---|
663 | * This routine prepends the_node onto the front of the_chain. |
---|
664 | * |
---|
665 | * @param[in] the_chain is the chain to be operated upon. |
---|
666 | * @param[in] the_node is the node to be prepended. |
---|
667 | * |
---|
668 | * @note It does NOT disable interrupts to ensure the atomicity of the |
---|
669 | * prepend operation. |
---|
670 | * |
---|
671 | * @retval true The chain was empty before. |
---|
672 | * @retval false The chain contained at least one node before. |
---|
673 | */ |
---|
674 | RTEMS_INLINE_ROUTINE bool _Chain_Prepend_with_empty_check_unprotected( |
---|
675 | Chain_Control *the_chain, |
---|
676 | Chain_Node *the_node |
---|
677 | ) |
---|
678 | { |
---|
679 | bool was_empty = _Chain_Is_empty( the_chain ); |
---|
680 | |
---|
681 | _Chain_Prepend_unprotected( the_chain, the_node ); |
---|
682 | |
---|
683 | return was_empty; |
---|
684 | } |
---|
685 | |
---|
686 | /** |
---|
687 | * @brief Get the first node and check if the chain is empty afterwards |
---|
688 | * (unprotected). |
---|
689 | * |
---|
690 | * This function removes the first node from the_chain and returns |
---|
691 | * a pointer to that node in @a the_node. If the_chain is empty, then NULL is |
---|
692 | * returned. |
---|
693 | * |
---|
694 | * @param[in] the_chain is the chain to attempt to get the first node from. |
---|
695 | * @param[out] the_node is the first node on the chain or NULL if the chain is |
---|
696 | * empty. |
---|
697 | * |
---|
698 | * @note It does NOT disable interrupts to ensure the atomicity of the |
---|
699 | * get operation. |
---|
700 | * |
---|
701 | * @retval true The chain is empty now. |
---|
702 | * @retval false The chain contains at least one node now. |
---|
703 | */ |
---|
704 | RTEMS_INLINE_ROUTINE bool _Chain_Get_with_empty_check_unprotected( |
---|
705 | Chain_Control *the_chain, |
---|
706 | Chain_Node **the_node |
---|
707 | ) |
---|
708 | { |
---|
709 | bool is_empty_now = true; |
---|
710 | Chain_Node *head = _Chain_Head( the_chain ); |
---|
711 | Chain_Node *tail = _Chain_Tail( the_chain ); |
---|
712 | Chain_Node *old_first = head->next; |
---|
713 | |
---|
714 | if ( old_first != tail ) { |
---|
715 | Chain_Node *new_first = old_first->next; |
---|
716 | |
---|
717 | head->next = new_first; |
---|
718 | new_first->previous = head; |
---|
719 | |
---|
720 | *the_node = old_first; |
---|
721 | |
---|
722 | is_empty_now = new_first == tail; |
---|
723 | } else |
---|
724 | *the_node = NULL; |
---|
725 | |
---|
726 | return is_empty_now; |
---|
727 | } |
---|
728 | |
---|
729 | /** |
---|
730 | * @brief Chain node order. |
---|
731 | * |
---|
732 | * @param[in] left The left node. |
---|
733 | * @param[in] right The right node. |
---|
734 | * |
---|
735 | * @retval true According to the order the left node precedes the right node. |
---|
736 | * @retval false Otherwise. |
---|
737 | */ |
---|
738 | typedef bool ( *Chain_Node_order )( |
---|
739 | const Chain_Node *left, |
---|
740 | const Chain_Node *right |
---|
741 | ); |
---|
742 | |
---|
743 | /** |
---|
744 | * @brief Inserts a node into the chain according to the order relation. |
---|
745 | * |
---|
746 | * After the operation the chain contains the node to insert and the order |
---|
747 | * relation holds for all nodes from the head up to the inserted node. Nodes |
---|
748 | * after the inserted node are not moved. |
---|
749 | * |
---|
750 | * @param[in,out] chain The chain. |
---|
751 | * @param[in,out] to_insert The node to insert. |
---|
752 | * @param[in] order The order relation. |
---|
753 | */ |
---|
754 | RTEMS_INLINE_ROUTINE void _Chain_Insert_ordered_unprotected( |
---|
755 | Chain_Control *chain, |
---|
756 | Chain_Node *to_insert, |
---|
757 | Chain_Node_order order |
---|
758 | ) |
---|
759 | { |
---|
760 | const Chain_Node *tail = _Chain_Immutable_tail( chain ); |
---|
761 | Chain_Node *next = _Chain_First( chain ); |
---|
762 | |
---|
763 | while ( next != tail && !( *order )( to_insert, next ) ) { |
---|
764 | next = _Chain_Next( next ); |
---|
765 | } |
---|
766 | |
---|
767 | _Chain_Insert_unprotected( _Chain_Previous( next ), to_insert ); |
---|
768 | } |
---|
769 | |
---|
770 | /** @} */ |
---|
771 | |
---|
772 | #endif |
---|
773 | /* end of include file */ |
---|