1
0

LockFreeStack.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #pragma once
  2. #include <windows.h>
  3. /* lock free stack object
  4. multiple threads can push and pop without locking
  5. note that order is not guaranteed. that is, if Thread 1 calls Insert before Thread 2, Thread 2's item might still make it in before.
  6. since it uses malloc/free for the linked list nodes, it might not be considered truely lock-free.
  7. use LockFreeStack2 if you can assure that you have a 'next' pointer in your class/struct
  8. */
  9. template <class value_t>
  10. class LockFreeStackNode
  11. {
  12. public:
  13. LockFreeStackNode(value_t _data)
  14. {
  15. data = _data;
  16. next = 0;
  17. }
  18. value_t data;
  19. LockFreeStackNode *next;
  20. };
  21. template <class value_t>
  22. class LockFreeStack
  23. {
  24. public:
  25. LockFreeStack()
  26. {
  27. head = 0;
  28. }
  29. void push(value_t data)
  30. {
  31. LockFreeStackNode<value_t> *new_head = new LockFreeStackNode<value_t>(data);
  32. LockFreeStackNode<value_t> *old_head = 0;
  33. do
  34. {
  35. old_head = (LockFreeStackNode<value_t> *)head;
  36. new_head->next = old_head;
  37. } while (InterlockedCompareExchangePointer((volatile PVOID *)&head, new_head, old_head) != old_head);
  38. }
  39. value_t pop()
  40. {
  41. LockFreeStackNode<value_t> *new_head = 0, *old_head = 0;
  42. do
  43. {
  44. old_head = (LockFreeStackNode<value_t> *)head;
  45. if (old_head)
  46. {
  47. new_head = old_head->next;
  48. }
  49. else
  50. {
  51. new_head = 0;
  52. }
  53. } while (InterlockedCompareExchangePointer((volatile PVOID *)&head, new_head, old_head) != old_head);
  54. value_t ret = old_head?old_head->data:0;
  55. delete old_head;
  56. return ret;
  57. }
  58. bool pop_ref(value_t *val)
  59. {
  60. LockFreeStackNode<value_t> *new_head = 0, *old_head = 0;
  61. do
  62. {
  63. old_head = (LockFreeStackNode<value_t> *)head;
  64. if (old_head)
  65. {
  66. new_head = old_head->next;
  67. }
  68. else
  69. {
  70. new_head = 0;
  71. }
  72. } while (InterlockedCompareExchangePointer((volatile PVOID *)&head, new_head, old_head) != old_head);
  73. if (old_head)
  74. {
  75. *val = old_head->data;
  76. delete old_head;
  77. return true;
  78. }
  79. return false;
  80. }
  81. volatile LockFreeStackNode<value_t> *head;
  82. };
  83. template <class value_t>
  84. class LockFreeStack2
  85. {
  86. public:
  87. LockFreeStack()
  88. {
  89. head = 0;
  90. }
  91. void push(value_t *data)
  92. {
  93. value_t *old_head = 0;
  94. do
  95. {
  96. old_head = head;
  97. data->next = old_head;
  98. } while (InterlockedCompareExchangePointer((volatile PVOID *)&head, data, old_head) != old_head);
  99. }
  100. value_t *pop()
  101. {
  102. value_t *new_head = 0, *old_head = 0;
  103. do
  104. {
  105. old_head = head;
  106. if (old_head)
  107. {
  108. new_head = old_head->next;
  109. }
  110. else
  111. {
  112. new_head = 0;
  113. }
  114. } while (InterlockedCompareExchangePointer((volatile PVOID *)&head, new_head, old_head) != old_head);
  115. return old_head;
  116. }
  117. bool pop_ref(value_t **val)
  118. {
  119. value_t *new_head = 0, *old_head = 0;
  120. do
  121. {
  122. old_head = head;
  123. if (old_head)
  124. {
  125. new_head = old_head->next;
  126. }
  127. else
  128. {
  129. new_head = 0;
  130. }
  131. } while (InterlockedCompareExchangePointer((volatile PVOID *)&head, new_head, old_head) != old_head);
  132. if (old_head)
  133. {
  134. *val = old_head;
  135. return true;
  136. }
  137. return false;
  138. }
  139. volatile value_t *head;
  140. };