RT-AC66 3.0.0.4.374.130 core
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / include / linux / list.h
blob93810fd3d5fce07abfac9643646382da71e1d47e
1 #ifndef _LINUX_LIST_H
2 #define _LINUX_LIST_H
4 #ifdef __KERNEL__
6 #include <linux/stddef.h>
7 #include <linux/poison.h>
8 #include <linux/prefetch.h>
9 #include <asm/system.h>
12 * Simple doubly linked list implementation.
14 * Some of the internal functions ("__xxx") are useful when
15 * manipulating whole lists rather than single entries, as
16 * sometimes we already know the next/prev entries and we can
17 * generate better code by using them directly rather than
18 * using the generic single-entry routines.
21 struct list_head {
22 struct list_head *next, *prev;
25 #define LIST_HEAD_INIT(name) { &(name), &(name) }
27 #define LIST_HEAD(name) \
28 struct list_head name = LIST_HEAD_INIT(name)
30 static inline void INIT_LIST_HEAD(struct list_head *list)
32 list->next = list;
33 list->prev = list;
37 * Insert a new entry between two known consecutive entries.
39 * This is only for internal list manipulation where we know
40 * the prev/next entries already!
42 #ifndef CONFIG_DEBUG_LIST
43 static inline void __list_add(struct list_head *new,
44 struct list_head *prev,
45 struct list_head *next)
47 next->prev = new;
48 new->next = next;
49 new->prev = prev;
50 prev->next = new;
52 #else
53 extern void __list_add(struct list_head *new,
54 struct list_head *prev,
55 struct list_head *next);
56 #endif
58 /**
59 * list_add - add a new entry
60 * @new: new entry to be added
61 * @head: list head to add it after
63 * Insert a new entry after the specified head.
64 * This is good for implementing stacks.
66 #ifndef CONFIG_DEBUG_LIST
67 static inline void list_add(struct list_head *new, struct list_head *head)
69 __list_add(new, head, head->next);
71 #else
72 extern void list_add(struct list_head *new, struct list_head *head);
73 #endif
76 /**
77 * list_add_tail - add a new entry
78 * @new: new entry to be added
79 * @head: list head to add it before
81 * Insert a new entry before the specified head.
82 * This is useful for implementing queues.
84 static inline void list_add_tail(struct list_head *new, struct list_head *head)
86 __list_add(new, head->prev, head);
90 * Insert a new entry between two known consecutive entries.
92 * This is only for internal list manipulation where we know
93 * the prev/next entries already!
95 static inline void __list_add_rcu(struct list_head * new,
96 struct list_head * prev, struct list_head * next)
98 new->next = next;
99 new->prev = prev;
100 smp_wmb();
101 next->prev = new;
102 prev->next = new;
106 * list_add_rcu - add a new entry to rcu-protected list
107 * @new: new entry to be added
108 * @head: list head to add it after
110 * Insert a new entry after the specified head.
111 * This is good for implementing stacks.
113 * The caller must take whatever precautions are necessary
114 * (such as holding appropriate locks) to avoid racing
115 * with another list-mutation primitive, such as list_add_rcu()
116 * or list_del_rcu(), running on this same list.
117 * However, it is perfectly legal to run concurrently with
118 * the _rcu list-traversal primitives, such as
119 * list_for_each_entry_rcu().
121 static inline void list_add_rcu(struct list_head *new, struct list_head *head)
123 __list_add_rcu(new, head, head->next);
127 * list_add_tail_rcu - add a new entry to rcu-protected list
128 * @new: new entry to be added
129 * @head: list head to add it before
131 * Insert a new entry before the specified head.
132 * This is useful for implementing queues.
134 * The caller must take whatever precautions are necessary
135 * (such as holding appropriate locks) to avoid racing
136 * with another list-mutation primitive, such as list_add_tail_rcu()
137 * or list_del_rcu(), running on this same list.
138 * However, it is perfectly legal to run concurrently with
139 * the _rcu list-traversal primitives, such as
140 * list_for_each_entry_rcu().
142 static inline void list_add_tail_rcu(struct list_head *new,
143 struct list_head *head)
145 __list_add_rcu(new, head->prev, head);
149 * Delete a list entry by making the prev/next entries
150 * point to each other.
152 * This is only for internal list manipulation where we know
153 * the prev/next entries already!
155 static inline void __list_del(struct list_head * prev, struct list_head * next)
157 next->prev = prev;
158 prev->next = next;
162 * list_del - deletes entry from list.
163 * @entry: the element to delete from the list.
164 * Note: list_empty() on entry does not return true after this, the entry is
165 * in an undefined state.
167 #ifndef CONFIG_DEBUG_LIST
168 static inline void list_del(struct list_head *entry)
170 __list_del(entry->prev, entry->next);
171 entry->next = LIST_POISON1;
172 entry->prev = LIST_POISON2;
174 #else
175 extern void list_del(struct list_head *entry);
176 #endif
179 * list_del_rcu - deletes entry from list without re-initialization
180 * @entry: the element to delete from the list.
182 * Note: list_empty() on entry does not return true after this,
183 * the entry is in an undefined state. It is useful for RCU based
184 * lockfree traversal.
186 * In particular, it means that we can not poison the forward
187 * pointers that may still be used for walking the list.
189 * The caller must take whatever precautions are necessary
190 * (such as holding appropriate locks) to avoid racing
191 * with another list-mutation primitive, such as list_del_rcu()
192 * or list_add_rcu(), running on this same list.
193 * However, it is perfectly legal to run concurrently with
194 * the _rcu list-traversal primitives, such as
195 * list_for_each_entry_rcu().
197 * Note that the caller is not permitted to immediately free
198 * the newly deleted entry. Instead, either synchronize_rcu()
199 * or call_rcu() must be used to defer freeing until an RCU
200 * grace period has elapsed.
202 static inline void list_del_rcu(struct list_head *entry)
204 __list_del(entry->prev, entry->next);
205 entry->prev = LIST_POISON2;
209 * list_replace - replace old entry by new one
210 * @old : the element to be replaced
211 * @new : the new element to insert
213 * If @old was empty, it will be overwritten.
215 static inline void list_replace(struct list_head *old,
216 struct list_head *new)
218 new->next = old->next;
219 new->next->prev = new;
220 new->prev = old->prev;
221 new->prev->next = new;
224 static inline void list_replace_init(struct list_head *old,
225 struct list_head *new)
227 list_replace(old, new);
228 INIT_LIST_HEAD(old);
232 * list_replace_rcu - replace old entry by new one
233 * @old : the element to be replaced
234 * @new : the new element to insert
236 * The @old entry will be replaced with the @new entry atomically.
237 * Note: @old should not be empty.
239 static inline void list_replace_rcu(struct list_head *old,
240 struct list_head *new)
242 new->next = old->next;
243 new->prev = old->prev;
244 smp_wmb();
245 new->next->prev = new;
246 new->prev->next = new;
247 old->prev = LIST_POISON2;
251 * list_del_init - deletes entry from list and reinitialize it.
252 * @entry: the element to delete from the list.
254 static inline void list_del_init(struct list_head *entry)
256 __list_del(entry->prev, entry->next);
257 INIT_LIST_HEAD(entry);
261 * list_move - delete from one list and add as another's head
262 * @list: the entry to move
263 * @head: the head that will precede our entry
265 static inline void list_move(struct list_head *list, struct list_head *head)
267 __list_del(list->prev, list->next);
268 list_add(list, head);
272 * list_move_tail - delete from one list and add as another's tail
273 * @list: the entry to move
274 * @head: the head that will follow our entry
276 static inline void list_move_tail(struct list_head *list,
277 struct list_head *head)
279 __list_del(list->prev, list->next);
280 list_add_tail(list, head);
284 * list_is_last - tests whether @list is the last entry in list @head
285 * @list: the entry to test
286 * @head: the head of the list
288 static inline int list_is_last(const struct list_head *list,
289 const struct list_head *head)
291 return list->next == head;
295 * list_empty - tests whether a list is empty
296 * @head: the list to test.
298 static inline int list_empty(const struct list_head *head)
300 return head->next == head;
304 * list_empty_careful - tests whether a list is empty and not being modified
305 * @head: the list to test
307 * Description:
308 * tests whether a list is empty _and_ checks that no other CPU might be
309 * in the process of modifying either member (next or prev)
311 * NOTE: using list_empty_careful() without synchronization
312 * can only be safe if the only activity that can happen
313 * to the list entry is list_del_init(). Eg. it cannot be used
314 * if another CPU could re-list_add() it.
316 static inline int list_empty_careful(const struct list_head *head)
318 struct list_head *next = head->next;
319 return (next == head) && (next == head->prev);
323 * list_is_singular - tests whether a list has just one entry.
324 * @head: the list to test.
326 static inline int list_is_singular(const struct list_head *head)
328 return !list_empty(head) && (head->next == head->prev);
331 static inline void __list_splice(struct list_head *list,
332 struct list_head *head)
334 struct list_head *first = list->next;
335 struct list_head *last = list->prev;
336 struct list_head *at = head->next;
338 first->prev = head;
339 head->next = first;
341 last->next = at;
342 at->prev = last;
346 * list_splice - join two lists
347 * @list: the new list to add.
348 * @head: the place to add it in the first list.
350 static inline void list_splice(struct list_head *list, struct list_head *head)
352 if (!list_empty(list))
353 __list_splice(list, head);
357 * list_splice_init - join two lists and reinitialise the emptied list.
358 * @list: the new list to add.
359 * @head: the place to add it in the first list.
361 * The list at @list is reinitialised
363 static inline void list_splice_init(struct list_head *list,
364 struct list_head *head)
366 if (!list_empty(list)) {
367 __list_splice(list, head);
368 INIT_LIST_HEAD(list);
373 * list_splice_init_rcu - splice an RCU-protected list into an existing list.
374 * @list: the RCU-protected list to splice
375 * @head: the place in the list to splice the first list into
376 * @sync: function to sync: synchronize_rcu(), synchronize_sched(), ...
378 * @head can be RCU-read traversed concurrently with this function.
380 * Note that this function blocks.
382 * Important note: the caller must take whatever action is necessary to
383 * prevent any other updates to @head. In principle, it is possible
384 * to modify the list as soon as sync() begins execution.
385 * If this sort of thing becomes necessary, an alternative version
386 * based on call_rcu() could be created. But only if -really-
387 * needed -- there is no shortage of RCU API members.
389 static inline void list_splice_init_rcu(struct list_head *list,
390 struct list_head *head,
391 void (*sync)(void))
393 struct list_head *first = list->next;
394 struct list_head *last = list->prev;
395 struct list_head *at = head->next;
397 if (list_empty(head))
398 return;
400 /* "first" and "last" tracking list, so initialize it. */
402 INIT_LIST_HEAD(list);
405 * At this point, the list body still points to the source list.
406 * Wait for any readers to finish using the list before splicing
407 * the list body into the new list. Any new readers will see
408 * an empty list.
411 sync();
414 * Readers are finished with the source list, so perform splice.
415 * The order is important if the new list is global and accessible
416 * to concurrent RCU readers. Note that RCU readers are not
417 * permitted to traverse the prev pointers without excluding
418 * this function.
421 last->next = at;
422 smp_wmb();
423 head->next = first;
424 first->prev = head;
425 at->prev = last;
429 * list_entry - get the struct for this entry
430 * @ptr: the &struct list_head pointer.
431 * @type: the type of the struct this is embedded in.
432 * @member: the name of the list_struct within the struct.
434 #define list_entry(ptr, type, member) \
435 container_of(ptr, type, member)
438 * list_first_entry - get the first element from a list
439 * @ptr: the list head to take the element from.
440 * @type: the type of the struct this is embedded in.
441 * @member: the name of the list_struct within the struct.
443 * Note, that list is expected to be not empty.
445 #define list_first_entry(ptr, type, member) \
446 list_entry((ptr)->next, type, member)
449 * list_for_each - iterate over a list
450 * @pos: the &struct list_head to use as a loop cursor.
451 * @head: the head for your list.
453 #define list_for_each(pos, head) \
454 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
455 pos = pos->next)
458 * __list_for_each - iterate over a list
459 * @pos: the &struct list_head to use as a loop cursor.
460 * @head: the head for your list.
462 * This variant differs from list_for_each() in that it's the
463 * simplest possible list iteration code, no prefetching is done.
464 * Use this for code that knows the list to be very short (empty
465 * or 1 entry) most of the time.
467 #define __list_for_each(pos, head) \
468 for (pos = (head)->next; pos != (head); pos = pos->next)
471 * list_for_each_prev - iterate over a list backwards
472 * @pos: the &struct list_head to use as a loop cursor.
473 * @head: the head for your list.
475 #define list_for_each_prev(pos, head) \
476 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
477 pos = pos->prev)
480 * list_for_each_safe - iterate over a list safe against removal of list entry
481 * @pos: the &struct list_head to use as a loop cursor.
482 * @n: another &struct list_head to use as temporary storage
483 * @head: the head for your list.
485 #define list_for_each_safe(pos, n, head) \
486 for (pos = (head)->next, n = pos->next; pos != (head); \
487 pos = n, n = pos->next)
490 * list_for_each_entry - iterate over list of given type
491 * @pos: the type * to use as a loop cursor.
492 * @head: the head for your list.
493 * @member: the name of the list_struct within the struct.
495 #define list_for_each_entry(pos, head, member) \
496 for (pos = list_entry((head)->next, typeof(*pos), member); \
497 prefetch(pos->member.next), &pos->member != (head); \
498 pos = list_entry(pos->member.next, typeof(*pos), member))
501 * list_for_each_entry_reverse - iterate backwards over list of given type.
502 * @pos: the type * to use as a loop cursor.
503 * @head: the head for your list.
504 * @member: the name of the list_struct within the struct.
506 #define list_for_each_entry_reverse(pos, head, member) \
507 for (pos = list_entry((head)->prev, typeof(*pos), member); \
508 prefetch(pos->member.prev), &pos->member != (head); \
509 pos = list_entry(pos->member.prev, typeof(*pos), member))
512 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
513 * @pos: the type * to use as a start point
514 * @head: the head of the list
515 * @member: the name of the list_struct within the struct.
517 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
519 #define list_prepare_entry(pos, head, member) \
520 ((pos) ? : list_entry(head, typeof(*pos), member))
523 * list_for_each_entry_continue - continue iteration over list of given type
524 * @pos: the type * to use as a loop cursor.
525 * @head: the head for your list.
526 * @member: the name of the list_struct within the struct.
528 * Continue to iterate over list of given type, continuing after
529 * the current position.
531 #define list_for_each_entry_continue(pos, head, member) \
532 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
533 prefetch(pos->member.next), &pos->member != (head); \
534 pos = list_entry(pos->member.next, typeof(*pos), member))
537 * list_for_each_entry_from - iterate over list of given type from the current point
538 * @pos: the type * to use as a loop cursor.
539 * @head: the head for your list.
540 * @member: the name of the list_struct within the struct.
542 * Iterate over list of given type, continuing from current position.
544 #define list_for_each_entry_from(pos, head, member) \
545 for (; prefetch(pos->member.next), &pos->member != (head); \
546 pos = list_entry(pos->member.next, typeof(*pos), member))
549 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
550 * @pos: the type * to use as a loop cursor.
551 * @n: another type * to use as temporary storage
552 * @head: the head for your list.
553 * @member: the name of the list_struct within the struct.
555 #define list_for_each_entry_safe(pos, n, head, member) \
556 for (pos = list_entry((head)->next, typeof(*pos), member), \
557 n = list_entry(pos->member.next, typeof(*pos), member); \
558 &pos->member != (head); \
559 pos = n, n = list_entry(n->member.next, typeof(*n), member))
562 * list_for_each_entry_safe_continue
563 * @pos: the type * to use as a loop cursor.
564 * @n: another type * to use as temporary storage
565 * @head: the head for your list.
566 * @member: the name of the list_struct within the struct.
568 * Iterate over list of given type, continuing after current point,
569 * safe against removal of list entry.
571 #define list_for_each_entry_safe_continue(pos, n, head, member) \
572 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
573 n = list_entry(pos->member.next, typeof(*pos), member); \
574 &pos->member != (head); \
575 pos = n, n = list_entry(n->member.next, typeof(*n), member))
578 * list_for_each_entry_safe_from
579 * @pos: the type * to use as a loop cursor.
580 * @n: another type * to use as temporary storage
581 * @head: the head for your list.
582 * @member: the name of the list_struct within the struct.
584 * Iterate over list of given type from current point, safe against
585 * removal of list entry.
587 #define list_for_each_entry_safe_from(pos, n, head, member) \
588 for (n = list_entry(pos->member.next, typeof(*pos), member); \
589 &pos->member != (head); \
590 pos = n, n = list_entry(n->member.next, typeof(*n), member))
593 * list_for_each_entry_safe_reverse
594 * @pos: the type * to use as a loop cursor.
595 * @n: another type * to use as temporary storage
596 * @head: the head for your list.
597 * @member: the name of the list_struct within the struct.
599 * Iterate backwards over list of given type, safe against removal
600 * of list entry.
602 #define list_for_each_entry_safe_reverse(pos, n, head, member) \
603 for (pos = list_entry((head)->prev, typeof(*pos), member), \
604 n = list_entry(pos->member.prev, typeof(*pos), member); \
605 &pos->member != (head); \
606 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
609 * list_for_each_rcu - iterate over an rcu-protected list
610 * @pos: the &struct list_head to use as a loop cursor.
611 * @head: the head for your list.
613 * This list-traversal primitive may safely run concurrently with
614 * the _rcu list-mutation primitives such as list_add_rcu()
615 * as long as the traversal is guarded by rcu_read_lock().
617 #define list_for_each_rcu(pos, head) \
618 for (pos = (head)->next; \
619 prefetch(rcu_dereference(pos)->next), pos != (head); \
620 pos = pos->next)
622 #define __list_for_each_rcu(pos, head) \
623 for (pos = (head)->next; \
624 rcu_dereference(pos) != (head); \
625 pos = pos->next)
628 * list_for_each_safe_rcu
629 * @pos: the &struct list_head to use as a loop cursor.
630 * @n: another &struct list_head to use as temporary storage
631 * @head: the head for your list.
633 * Iterate over an rcu-protected list, safe against removal of list entry.
635 * This list-traversal primitive may safely run concurrently with
636 * the _rcu list-mutation primitives such as list_add_rcu()
637 * as long as the traversal is guarded by rcu_read_lock().
639 #define list_for_each_safe_rcu(pos, n, head) \
640 for (pos = (head)->next; \
641 n = rcu_dereference(pos)->next, pos != (head); \
642 pos = n)
645 * list_for_each_entry_rcu - iterate over rcu list of given type
646 * @pos: the type * to use as a loop cursor.
647 * @head: the head for your list.
648 * @member: the name of the list_struct within the struct.
650 * This list-traversal primitive may safely run concurrently with
651 * the _rcu list-mutation primitives such as list_add_rcu()
652 * as long as the traversal is guarded by rcu_read_lock().
654 #define list_for_each_entry_rcu(pos, head, member) \
655 for (pos = list_entry((head)->next, typeof(*pos), member); \
656 prefetch(rcu_dereference(pos)->member.next), \
657 &pos->member != (head); \
658 pos = list_entry(pos->member.next, typeof(*pos), member))
662 * list_for_each_continue_rcu
663 * @pos: the &struct list_head to use as a loop cursor.
664 * @head: the head for your list.
666 * Iterate over an rcu-protected list, continuing after current point.
668 * This list-traversal primitive may safely run concurrently with
669 * the _rcu list-mutation primitives such as list_add_rcu()
670 * as long as the traversal is guarded by rcu_read_lock().
672 #define list_for_each_continue_rcu(pos, head) \
673 for ((pos) = (pos)->next; \
674 prefetch(rcu_dereference((pos))->next), (pos) != (head); \
675 (pos) = (pos)->next)
678 * Double linked lists with a single pointer list head.
679 * Mostly useful for hash tables where the two pointer list head is
680 * too wasteful.
681 * You lose the ability to access the tail in O(1).
684 struct hlist_head {
685 struct hlist_node *first;
688 struct hlist_node {
689 struct hlist_node *next, **pprev;
692 #define HLIST_HEAD_INIT { .first = NULL }
693 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
694 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
695 static inline void INIT_HLIST_NODE(struct hlist_node *h)
697 h->next = NULL;
698 h->pprev = NULL;
701 static inline int hlist_unhashed(const struct hlist_node *h)
703 return !h->pprev;
706 static inline int hlist_empty(const struct hlist_head *h)
708 return !h->first;
711 static inline void __hlist_del(struct hlist_node *n)
713 struct hlist_node *next = n->next;
714 struct hlist_node **pprev = n->pprev;
715 *pprev = next;
716 if (next)
717 next->pprev = pprev;
720 static inline void hlist_del(struct hlist_node *n)
722 __hlist_del(n);
723 n->next = LIST_POISON1;
724 n->pprev = LIST_POISON2;
728 * hlist_del_rcu - deletes entry from hash list without re-initialization
729 * @n: the element to delete from the hash list.
731 * Note: list_unhashed() on entry does not return true after this,
732 * the entry is in an undefined state. It is useful for RCU based
733 * lockfree traversal.
735 * In particular, it means that we can not poison the forward
736 * pointers that may still be used for walking the hash list.
738 * The caller must take whatever precautions are necessary
739 * (such as holding appropriate locks) to avoid racing
740 * with another list-mutation primitive, such as hlist_add_head_rcu()
741 * or hlist_del_rcu(), running on this same list.
742 * However, it is perfectly legal to run concurrently with
743 * the _rcu list-traversal primitives, such as
744 * hlist_for_each_entry().
746 static inline void hlist_del_rcu(struct hlist_node *n)
748 __hlist_del(n);
749 n->pprev = LIST_POISON2;
752 static inline void hlist_del_init(struct hlist_node *n)
754 if (!hlist_unhashed(n)) {
755 __hlist_del(n);
756 INIT_HLIST_NODE(n);
761 * hlist_del_init_rcu - deletes entry from hash list with re-initialization
762 * @n: the element to delete from the hash list.
764 * Note: list_unhashed() on the node return true after this. It is
765 * useful for RCU based read lockfree traversal if the writer side
766 * must know if the list entry is still hashed or already unhashed.
768 * In particular, it means that we can not poison the forward pointers
769 * that may still be used for walking the hash list and we can only
770 * zero the pprev pointer so list_unhashed() will return true after
771 * this.
773 * The caller must take whatever precautions are necessary (such as
774 * holding appropriate locks) to avoid racing with another
775 * list-mutation primitive, such as hlist_add_head_rcu() or
776 * hlist_del_rcu(), running on this same list. However, it is
777 * perfectly legal to run concurrently with the _rcu list-traversal
778 * primitives, such as hlist_for_each_entry_rcu().
780 static inline void hlist_del_init_rcu(struct hlist_node *n)
782 if (!hlist_unhashed(n)) {
783 __hlist_del(n);
784 n->pprev = NULL;
789 * hlist_replace_rcu - replace old entry by new one
790 * @old : the element to be replaced
791 * @new : the new element to insert
793 * The @old entry will be replaced with the @new entry atomically.
795 static inline void hlist_replace_rcu(struct hlist_node *old,
796 struct hlist_node *new)
798 struct hlist_node *next = old->next;
800 new->next = next;
801 new->pprev = old->pprev;
802 smp_wmb();
803 if (next)
804 new->next->pprev = &new->next;
805 *new->pprev = new;
806 old->pprev = LIST_POISON2;
809 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
811 struct hlist_node *first = h->first;
812 n->next = first;
813 if (first)
814 first->pprev = &n->next;
815 h->first = n;
816 n->pprev = &h->first;
821 * hlist_add_head_rcu
822 * @n: the element to add to the hash list.
823 * @h: the list to add to.
825 * Description:
826 * Adds the specified element to the specified hlist,
827 * while permitting racing traversals.
829 * The caller must take whatever precautions are necessary
830 * (such as holding appropriate locks) to avoid racing
831 * with another list-mutation primitive, such as hlist_add_head_rcu()
832 * or hlist_del_rcu(), running on this same list.
833 * However, it is perfectly legal to run concurrently with
834 * the _rcu list-traversal primitives, such as
835 * hlist_for_each_entry_rcu(), used to prevent memory-consistency
836 * problems on Alpha CPUs. Regardless of the type of CPU, the
837 * list-traversal primitive must be guarded by rcu_read_lock().
839 static inline void hlist_add_head_rcu(struct hlist_node *n,
840 struct hlist_head *h)
842 struct hlist_node *first = h->first;
843 n->next = first;
844 n->pprev = &h->first;
845 smp_wmb();
846 if (first)
847 first->pprev = &n->next;
848 h->first = n;
851 /* next must be != NULL */
852 static inline void hlist_add_before(struct hlist_node *n,
853 struct hlist_node *next)
855 n->pprev = next->pprev;
856 n->next = next;
857 next->pprev = &n->next;
858 *(n->pprev) = n;
861 static inline void hlist_add_after(struct hlist_node *n,
862 struct hlist_node *next)
864 next->next = n->next;
865 n->next = next;
866 next->pprev = &n->next;
868 if(next->next)
869 next->next->pprev = &next->next;
873 * hlist_add_before_rcu
874 * @n: the new element to add to the hash list.
875 * @next: the existing element to add the new element before.
877 * Description:
878 * Adds the specified element to the specified hlist
879 * before the specified node while permitting racing traversals.
881 * The caller must take whatever precautions are necessary
882 * (such as holding appropriate locks) to avoid racing
883 * with another list-mutation primitive, such as hlist_add_head_rcu()
884 * or hlist_del_rcu(), running on this same list.
885 * However, it is perfectly legal to run concurrently with
886 * the _rcu list-traversal primitives, such as
887 * hlist_for_each_entry_rcu(), used to prevent memory-consistency
888 * problems on Alpha CPUs.
890 static inline void hlist_add_before_rcu(struct hlist_node *n,
891 struct hlist_node *next)
893 n->pprev = next->pprev;
894 n->next = next;
895 smp_wmb();
896 next->pprev = &n->next;
897 *(n->pprev) = n;
901 * hlist_add_after_rcu
902 * @prev: the existing element to add the new element after.
903 * @n: the new element to add to the hash list.
905 * Description:
906 * Adds the specified element to the specified hlist
907 * after the specified node while permitting racing traversals.
909 * The caller must take whatever precautions are necessary
910 * (such as holding appropriate locks) to avoid racing
911 * with another list-mutation primitive, such as hlist_add_head_rcu()
912 * or hlist_del_rcu(), running on this same list.
913 * However, it is perfectly legal to run concurrently with
914 * the _rcu list-traversal primitives, such as
915 * hlist_for_each_entry_rcu(), used to prevent memory-consistency
916 * problems on Alpha CPUs.
918 static inline void hlist_add_after_rcu(struct hlist_node *prev,
919 struct hlist_node *n)
921 n->next = prev->next;
922 n->pprev = &prev->next;
923 smp_wmb();
924 prev->next = n;
925 if (n->next)
926 n->next->pprev = &n->next;
929 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
931 #define hlist_for_each(pos, head) \
932 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
933 pos = pos->next)
935 #define hlist_for_each_safe(pos, n, head) \
936 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
937 pos = n)
940 * hlist_for_each_entry - iterate over list of given type
941 * @tpos: the type * to use as a loop cursor.
942 * @pos: the &struct hlist_node to use as a loop cursor.
943 * @head: the head for your list.
944 * @member: the name of the hlist_node within the struct.
946 #define hlist_for_each_entry(tpos, pos, head, member) \
947 for (pos = (head)->first; \
948 pos && ({ prefetch(pos->next); 1;}) && \
949 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
950 pos = pos->next)
953 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
954 * @tpos: the type * to use as a loop cursor.
955 * @pos: the &struct hlist_node to use as a loop cursor.
956 * @member: the name of the hlist_node within the struct.
958 #define hlist_for_each_entry_continue(tpos, pos, member) \
959 for (pos = (pos)->next; \
960 pos && ({ prefetch(pos->next); 1;}) && \
961 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
962 pos = pos->next)
965 * hlist_for_each_entry_from - iterate over a hlist continuing from current point
966 * @tpos: the type * to use as a loop cursor.
967 * @pos: the &struct hlist_node to use as a loop cursor.
968 * @member: the name of the hlist_node within the struct.
970 #define hlist_for_each_entry_from(tpos, pos, member) \
971 for (; pos && ({ prefetch(pos->next); 1;}) && \
972 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
973 pos = pos->next)
976 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
977 * @tpos: the type * to use as a loop cursor.
978 * @pos: the &struct hlist_node to use as a loop cursor.
979 * @n: another &struct hlist_node to use as temporary storage
980 * @head: the head for your list.
981 * @member: the name of the hlist_node within the struct.
983 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
984 for (pos = (head)->first; \
985 pos && ({ n = pos->next; 1; }) && \
986 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
987 pos = n)
990 * hlist_for_each_entry_rcu - iterate over rcu list of given type
991 * @tpos: the type * to use as a loop cursor.
992 * @pos: the &struct hlist_node to use as a loop cursor.
993 * @head: the head for your list.
994 * @member: the name of the hlist_node within the struct.
996 * This list-traversal primitive may safely run concurrently with
997 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
998 * as long as the traversal is guarded by rcu_read_lock().
1000 #define hlist_for_each_entry_rcu(tpos, pos, head, member) \
1001 for (pos = (head)->first; \
1002 rcu_dereference(pos) && ({ prefetch(pos->next); 1;}) && \
1003 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
1004 pos = pos->next)
1006 #else
1007 #warning "don't include kernel headers in userspace"
1008 #endif /* __KERNEL__ */
1009 #endif