llist.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #ifndef LLIST_H
  2. #define LLIST_H
  3. /*
  4. * Lock-less NULL terminated single linked list
  5. *
  6. * If there are multiple producers and multiple consumers, llist_add
  7. * can be used in producers and llist_del_all can be used in
  8. * consumers. They can work simultaneously without lock. But
  9. * llist_del_first can not be used here. Because llist_del_first
  10. * depends on list->first->next does not changed if list->first is not
  11. * changed during its operation, but llist_del_first, llist_add,
  12. * llist_add (or llist_del_all, llist_add, llist_add) sequence in
  13. * another consumer may violate that.
  14. *
  15. * If there are multiple producers and one consumer, llist_add can be
  16. * used in producers and llist_del_all or llist_del_first can be used
  17. * in the consumer.
  18. *
  19. * This can be summarized as follow:
  20. *
  21. * | add | del_first | del_all
  22. * add | - | - | -
  23. * del_first | | L | L
  24. * del_all | | | -
  25. *
  26. * Where "-" stands for no lock is needed, while "L" stands for lock
  27. * is needed.
  28. *
  29. * The list entries deleted via llist_del_all can be traversed with
  30. * traversing function such as llist_for_each etc. But the list
  31. * entries can not be traversed safely before deleted from the list.
  32. * The order of deleted entries is from the newest to the oldest added
  33. * one. If you want to traverse from the oldest to the newest, you
  34. * must reverse the order by yourself before traversing.
  35. *
  36. * The basic atomic operation of this list is cmpxchg on long. On
  37. * architectures that don't have NMI-safe cmpxchg implementation, the
  38. * list can NOT be used in NMI handlers. So code that uses the list in
  39. * an NMI handler should depend on CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG.
  40. *
  41. * Copyright 2010,2011 Intel Corp.
  42. * Author: Huang Ying <ying.huang@intel.com>
  43. *
  44. * This program is free software; you can redistribute it and/or
  45. * modify it under the terms of the GNU General Public License version
  46. * 2 as published by the Free Software Foundation;
  47. *
  48. * This program is distributed in the hope that it will be useful,
  49. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  50. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  51. * GNU General Public License for more details.
  52. *
  53. * You should have received a copy of the GNU General Public License
  54. * along with this program; if not, write to the Free Software
  55. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  56. */
  57. #include <linux/atomic.h>
  58. #include <linux/kernel.h>
  59. struct llist_head {
  60. struct llist_node *first;
  61. };
  62. struct llist_node {
  63. struct llist_node *next;
  64. };
  65. #define LLIST_HEAD_INIT(name) { NULL }
  66. #define LLIST_HEAD(name) struct llist_head name = LLIST_HEAD_INIT(name)
  67. /**
  68. * init_llist_head - initialize lock-less list head
  69. * @head: the head for your lock-less list
  70. */
  71. static inline void init_llist_head(struct llist_head *list)
  72. {
  73. list->first = NULL;
  74. }
  75. /**
  76. * llist_entry - get the struct of this entry
  77. * @ptr: the &struct llist_node pointer.
  78. * @type: the type of the struct this is embedded in.
  79. * @member: the name of the llist_node within the struct.
  80. */
  81. #define llist_entry(ptr, type, member) \
  82. container_of(ptr, type, member)
  83. /**
  84. * member_address_is_nonnull - check whether the member address is not NULL
  85. * @ptr: the object pointer (struct type * that contains the llist_node)
  86. * @member: the name of the llist_node within the struct.
  87. *
  88. * This macro is conceptually the same as
  89. * &ptr->member != NULL
  90. * but it works around the fact that compilers can decide that taking a member
  91. * address is never a NULL pointer.
  92. *
  93. * Real objects that start at a high address and have a member at NULL are
  94. * unlikely to exist, but such pointers may be returned e.g. by the
  95. * container_of() macro.
  96. */
  97. #define member_address_is_nonnull(ptr, member) \
  98. ((uintptr_t)(ptr) + offsetof(typeof(*(ptr)), member) != 0)
  99. /**
  100. * llist_for_each - iterate over some deleted entries of a lock-less list
  101. * @pos: the &struct llist_node to use as a loop cursor
  102. * @node: the first entry of deleted list entries
  103. *
  104. * In general, some entries of the lock-less list can be traversed
  105. * safely only after being deleted from list, so start with an entry
  106. * instead of list head.
  107. *
  108. * If being used on entries deleted from lock-less list directly, the
  109. * traverse order is from the newest to the oldest added entry. If
  110. * you want to traverse from the oldest to the newest, you must
  111. * reverse the order by yourself before traversing.
  112. */
  113. #define llist_for_each(pos, node) \
  114. for ((pos) = (node); pos; (pos) = (pos)->next)
  115. /**
  116. * llist_for_each_entry - iterate over some deleted entries of lock-less list of given type
  117. * @pos: the type * to use as a loop cursor.
  118. * @node: the fist entry of deleted list entries.
  119. * @member: the name of the llist_node with the struct.
  120. *
  121. * In general, some entries of the lock-less list can be traversed
  122. * safely only after being removed from list, so start with an entry
  123. * instead of list head.
  124. *
  125. * If being used on entries deleted from lock-less list directly, the
  126. * traverse order is from the newest to the oldest added entry. If
  127. * you want to traverse from the oldest to the newest, you must
  128. * reverse the order by yourself before traversing.
  129. */
  130. #define llist_for_each_entry(pos, node, member) \
  131. for ((pos) = llist_entry((node), typeof(*(pos)), member); \
  132. member_address_is_nonnull(pos, member); \
  133. (pos) = llist_entry((pos)->member.next, typeof(*(pos)), member))
  134. /**
  135. * llist_for_each_entry_safe - iterate over some deleted entries of lock-less list of given type
  136. * safe against removal of list entry
  137. * @pos: the type * to use as a loop cursor.
  138. * @n: another type * to use as temporary storage
  139. * @node: the first entry of deleted list entries.
  140. * @member: the name of the llist_node with the struct.
  141. *
  142. * In general, some entries of the lock-less list can be traversed
  143. * safely only after being removed from list, so start with an entry
  144. * instead of list head.
  145. *
  146. * If being used on entries deleted from lock-less list directly, the
  147. * traverse order is from the newest to the oldest added entry. If
  148. * you want to traverse from the oldest to the newest, you must
  149. * reverse the order by yourself before traversing.
  150. */
  151. #define llist_for_each_entry_safe(pos, n, node, member) \
  152. for (pos = llist_entry((node), typeof(*pos), member); \
  153. member_address_is_nonnull(pos, member) && \
  154. (n = llist_entry(pos->member.next, typeof(*n), member), true); \
  155. pos = n)
  156. /**
  157. * llist_empty - tests whether a lock-less list is empty
  158. * @head: the list to test
  159. *
  160. * Not guaranteed to be accurate or up to date. Just a quick way to
  161. * test whether the list is empty without deleting something from the
  162. * list.
  163. */
  164. static inline bool llist_empty(const struct llist_head *head)
  165. {
  166. return ACCESS_ONCE(head->first) == NULL;
  167. }
  168. static inline struct llist_node *llist_next(struct llist_node *node)
  169. {
  170. return node->next;
  171. }
  172. extern bool llist_add_batch(struct llist_node *new_first,
  173. struct llist_node *new_last,
  174. struct llist_head *head);
  175. /**
  176. * llist_add - add a new entry
  177. * @new: new entry to be added
  178. * @head: the head for your lock-less list
  179. *
  180. * Returns true if the list was empty prior to adding this entry.
  181. */
  182. static inline bool llist_add(struct llist_node *new, struct llist_head *head)
  183. {
  184. return llist_add_batch(new, new, head);
  185. }
  186. /**
  187. * llist_del_all - delete all entries from lock-less list
  188. * @head: the head of lock-less list to delete all entries
  189. *
  190. * If list is empty, return NULL, otherwise, delete all entries and
  191. * return the pointer to the first entry. The order of entries
  192. * deleted is from the newest to the oldest added one.
  193. */
  194. static inline struct llist_node *llist_del_all(struct llist_head *head)
  195. {
  196. return xchg(&head->first, NULL);
  197. }
  198. extern struct llist_node *llist_del_first(struct llist_head *head);
  199. struct llist_node *llist_reverse_order(struct llist_node *head);
  200. #endif /* LLIST_H */