xlist.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #ifndef _LINUX_XLIST_H
  2. #define _LINUX_XLIST_H
  3. #include <linux/stddef.h>
  4. #include <linux/poison.h>
  5. #include <linux/prefetch.h>
  6. #include <asm/system.h>
  7. struct xlist_head {
  8. struct xlist_head *next;
  9. };
  10. /*
  11. * XLIST_PTR_TAIL can be used to prevent double insertion. See
  12. * xlist_protect()
  13. */
  14. #define XLIST_PTR_TAIL ((struct xlist_head *)0x1)
  15. static inline void xlist_add(struct xlist_head *new, struct xlist_head *tail, struct xlist_head *head)
  16. {
  17. struct xlist_head *cur;
  18. struct xlist_head *check;
  19. while (1) {
  20. cur = head->next;
  21. tail->next = cur;
  22. check = cmpxchg(&head->next, cur, new);
  23. if (check == cur)
  24. break;
  25. }
  26. }
  27. /*
  28. * To avoid duplicate insertion by two CPUs of the same xlist item
  29. * you can call xlist_protect. It will stuff XLIST_PTR_TAIL
  30. * into the entry->next pointer with xchg, and only return 1
  31. * if there was a NULL there before.
  32. *
  33. * if xlist_protect returns zero, someone else is busy working
  34. * on this entry. Getting a NULL into the entry in a race
  35. * free manner is the caller's job.
  36. */
  37. static inline int xlist_protect(struct xlist_head *entry)
  38. {
  39. struct xlist_head *val;
  40. val = xchg(&entry->next, XLIST_PTR_TAIL);
  41. if (val == NULL)
  42. return 1;
  43. return 0;
  44. }
  45. static inline struct xlist_head *xlist_del_head(struct xlist_head *head)
  46. {
  47. struct xlist_head *cur;
  48. struct xlist_head *check;
  49. struct xlist_head *next;
  50. while (1) {
  51. cur = head->next;
  52. if (!cur)
  53. goto out;
  54. if (cur == XLIST_PTR_TAIL) {
  55. cur = NULL;
  56. goto out;
  57. }
  58. next = cur->next;
  59. check = cmpxchg(&head->next, cur, next);
  60. if (check == cur)
  61. goto out;
  62. }
  63. out:
  64. return cur;
  65. }
  66. static inline struct xlist_head *xlist_del_head_fast(struct xlist_head *head)
  67. {
  68. struct xlist_head *cur;
  69. cur = head->next;
  70. if (!cur || cur == XLIST_PTR_TAIL)
  71. return NULL;
  72. head->next = cur->next;
  73. return cur;
  74. }
  75. static inline void xlist_splice(struct xlist_head *list,
  76. struct xlist_head *head)
  77. {
  78. struct xlist_head *cur;
  79. WARN_ON(head->next);
  80. cur = xchg(&list->next, NULL);
  81. head->next = cur;
  82. }
  83. static inline void INIT_XLIST_HEAD(struct xlist_head *list)
  84. {
  85. list->next = NULL;
  86. }
  87. static inline int xlist_empty(struct xlist_head *head)
  88. {
  89. return head->next == NULL || head->next == XLIST_PTR_TAIL;
  90. }
  91. #endif