llist.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 handler. So code uses the list in NMI
  39. * handler should depend on CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG.
  40. */
  41. struct llist_head {
  42. struct llist_node *first;
  43. };
  44. struct llist_node {
  45. struct llist_node *next;
  46. };
  47. #define LLIST_HEAD_INIT(name) { NULL }
  48. #define LLIST_HEAD(name) struct llist_head name = LLIST_HEAD_INIT(name)
  49. /**
  50. * init_llist_head - initialize lock-less list head
  51. * @head: the head for your lock-less list
  52. */
  53. static inline void init_llist_head(struct llist_head *list)
  54. {
  55. list->first = NULL;
  56. }
  57. /**
  58. * llist_entry - get the struct of this entry
  59. * @ptr: the &struct llist_node pointer.
  60. * @type: the type of the struct this is embedded in.
  61. * @member: the name of the llist_node within the struct.
  62. */
  63. #define llist_entry(ptr, type, member) \
  64. container_of(ptr, type, member)
  65. /**
  66. * llist_for_each - iterate over some deleted entries of a lock-less list
  67. * @pos: the &struct llist_node to use as a loop cursor
  68. * @node: the first entry of deleted list entries
  69. *
  70. * In general, some entries of the lock-less list can be traversed
  71. * safely only after being deleted from list, so start with an entry
  72. * instead of list head.
  73. *
  74. * If being used on entries deleted from lock-less list directly, the
  75. * traverse order is from the newest to the oldest added entry. If
  76. * you want to traverse from the oldest to the newest, you must
  77. * reverse the order by yourself before traversing.
  78. */
  79. #define llist_for_each(pos, node) \
  80. for ((pos) = (node); pos; (pos) = (pos)->next)
  81. /**
  82. * llist_for_each_entry - iterate over some deleted entries of lock-less list of given type
  83. * @pos: the type * to use as a loop cursor.
  84. * @node: the fist entry of deleted list entries.
  85. * @member: the name of the llist_node with the struct.
  86. *
  87. * In general, some entries of the lock-less list can be traversed
  88. * safely only after being removed from list, so start with an entry
  89. * instead of list head.
  90. *
  91. * If being used on entries deleted from lock-less list directly, the
  92. * traverse order is from the newest to the oldest added entry. If
  93. * you want to traverse from the oldest to the newest, you must
  94. * reverse the order by yourself before traversing.
  95. */
  96. #define llist_for_each_entry(pos, node, member) \
  97. for ((pos) = llist_entry((node), typeof(*(pos)), member); \
  98. &(pos)->member != NULL; \
  99. (pos) = llist_entry((pos)->member.next, typeof(*(pos)), member))
  100. /**
  101. * llist_empty - tests whether a lock-less list is empty
  102. * @head: the list to test
  103. *
  104. * Not guaranteed to be accurate or up to date. Just a quick way to
  105. * test whether the list is empty without deleting something from the
  106. * list.
  107. */
  108. static inline int llist_empty(const struct llist_head *head)
  109. {
  110. return ACCESS_ONCE(head->first) == NULL;
  111. }
  112. void llist_add(struct llist_node *new, struct llist_head *head);
  113. void llist_add_batch(struct llist_node *new_first, struct llist_node *new_last,
  114. struct llist_head *head);
  115. struct llist_node *llist_del_first(struct llist_head *head);
  116. struct llist_node *llist_del_all(struct llist_head *head);
  117. #endif /* LLIST_H */