LocklessFIFO.hpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. Copyright (c) 2009-2010 Christopher A. Taylor. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are met:
  5. * Redistributions of source code must retain the above copyright notice,
  6. this list of conditions and the following disclaimer.
  7. * Redistributions in binary form must reproduce the above copyright notice,
  8. this list of conditions and the following disclaimer in the documentation
  9. and/or other materials provided with the distribution.
  10. * Neither the name of LibCat nor the names of its contributors may be used
  11. to endorse or promote products derived from this software without
  12. specific prior written permission.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  14. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  17. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  23. POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. /*
  26. Algorithm from "An Optimistic Approach to Lock-Free FIFO Queues"
  27. Edya Ladan-Mozes and Nir Shavit (2004)
  28. */
  29. #ifndef CAT_LOCKLESS_FIFO_HPP
  30. #define CAT_LOCKLESS_FIFO_HPP
  31. #include <cat/threads/RegionAllocator.hpp>
  32. #include <cat/threads/Atomic.hpp>
  33. #if defined(CAT_OS_WINDOWS)
  34. # include <cat/port/WindowsInclude.hpp>
  35. #else
  36. # error "Not portable to your OS!"
  37. #endif
  38. namespace cat {
  39. namespace FIFO {
  40. template<class T> class Ptr;
  41. template<class T> class Node;
  42. template<class T> class Queue;
  43. //// Ptr
  44. // Union for an ABA-proof pointer
  45. template<class T>
  46. class Ptr
  47. {
  48. public:
  49. union
  50. {
  51. struct
  52. {
  53. Node<T> *ptr;
  54. #if defined(CAT_WORD_64)
  55. u64 tag;
  56. #else
  57. u32 tag;
  58. #endif
  59. };
  60. #if defined(CAT_WORD_64)
  61. volatile u64 N[2];
  62. #else
  63. volatile u64 N;
  64. #endif
  65. } CAT_PACKED;
  66. Ptr(); // zero everything
  67. bool operator==(const Ptr<T> &rhs);
  68. bool operator!=(const Ptr<T> &rhs);
  69. };
  70. //// Derive from Data<T> for data passed through the queue
  71. template<class T>
  72. class Node
  73. {
  74. friend class Queue<T>;
  75. T *value;
  76. Ptr<T> next, prev;
  77. };
  78. //// Queue
  79. // Performs lazy deallocation of data objects on behalf of the caller,
  80. // freeing all remaining objects when the Queue goes out of scope.
  81. template<class T>
  82. class Queue
  83. {
  84. // Pointer to head and tail
  85. Ptr<T> Head, Tail;
  86. // Event to wait on if dequeuing
  87. #if defined(CAT_OS_WINDOWS)
  88. HANDLE hEvent;
  89. #endif
  90. public:
  91. Queue();
  92. ~Queue();
  93. public:
  94. void Enqueue(T *data);
  95. T *Dequeue();
  96. void FixList(Ptr<T> tail, Ptr<T> head);
  97. // Enqueue a new event to wake up a thread stuck here
  98. T *DequeueWait();
  99. };
  100. //// Compare-and-Swap x2 (CAS2) atomic operation
  101. template<class T>
  102. inline bool CAS2(Ptr<T> &destination, const Ptr<T> &expected, const Ptr<T> &replacement)
  103. {
  104. return Atomic::CAS2(&destination, &expected, &replacement);
  105. }
  106. //// Ptr: Templated member implementation
  107. template<class T>
  108. Ptr<T>::Ptr()
  109. {
  110. #if defined(CAT_WORD_64)
  111. N[0] = 0;
  112. N[1] = 0;
  113. #else
  114. N = 0;
  115. #endif
  116. }
  117. template<class T>
  118. bool Ptr<T>::operator==(const Ptr<T> &n)
  119. {
  120. #if defined(CAT_WORD_64)
  121. return N[0] == n.N[0] && N[1] == n.N[1];
  122. #else
  123. return N == n.N;
  124. #endif
  125. }
  126. template<class T>
  127. bool Ptr<T>::operator!=(const Ptr<T> &n)
  128. {
  129. #if defined(CAT_WORD_64)
  130. return N[0] != n.N[0] || N[1] != n.N[1];
  131. #else
  132. return N != n.N;
  133. #endif
  134. }
  135. //// Queue: Templated member implementation
  136. template<class T>
  137. Queue<T>::Queue()
  138. {
  139. #if defined(CAT_OS_WINDOWS)
  140. hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  141. #endif
  142. Node<T> *node = new (RegionAllocator::ii) Node<T>;
  143. node->value = 0;
  144. Head.ptr = Tail.ptr = node;
  145. }
  146. template<class T>
  147. Queue<T>::~Queue()
  148. {
  149. // Destroy objects that are still queued
  150. for (Node<T> *next, *ptr = Head.ptr; ptr; ptr = next)
  151. {
  152. next = ptr->next.ptr;
  153. if (ptr->value)
  154. RegionAllocator::ii->Delete(ptr->value);
  155. RegionAllocator::ii->Delete(ptr);
  156. }
  157. #if defined(CAT_OS_WINDOWS)
  158. CloseHandle(hEvent);
  159. #endif
  160. }
  161. template<class T>
  162. void Queue<T>::Enqueue(T *val)
  163. {
  164. Ptr<T> tail;
  165. Node<T> *nd = new (RegionAllocator::ii) Node<T>;
  166. nd->value = val;
  167. for (;;)
  168. {
  169. tail = Tail;
  170. nd->next.ptr = tail.ptr;
  171. nd->next.tag = tail.tag + 1;
  172. Ptr<T> new_tail;
  173. new_tail.ptr = nd;
  174. new_tail.tag = tail.tag + 1;
  175. if (CAS2(Tail, tail, new_tail))
  176. {
  177. tail.ptr->prev.ptr = nd;
  178. tail.ptr->prev.tag = tail.tag;
  179. break;
  180. }
  181. }
  182. #if defined(CAT_OS_WINDOWS)
  183. SetEvent(hEvent);
  184. #endif
  185. }
  186. template<class T>
  187. T *Queue<T>::DequeueWait()
  188. {
  189. for (;;)
  190. {
  191. // Attempt to dequeue a message
  192. // If we won the race to service the message, then return it
  193. T *retval = Dequeue();
  194. if (retval) return retval;
  195. #if defined(CAT_OS_WINDOWS)
  196. // If the sychronization wait fails (handle closed), abort with 0
  197. if (WaitForSingleObject(hEvent, INFINITE) != WAIT_OBJECT_0)
  198. return 0;
  199. #endif
  200. }
  201. }
  202. template<class T>
  203. T *Queue<T>::Dequeue()
  204. {
  205. Ptr<T> tail, head, firstNodePrev;
  206. Node<T> *nd_dummy;
  207. T *val;
  208. for (;;)
  209. {
  210. head = Head;
  211. tail = Tail;
  212. firstNodePrev = head.ptr->prev;
  213. val = head.ptr->value;
  214. if (head == Head)
  215. {
  216. if (val != 0)
  217. {
  218. if (tail != head)
  219. {
  220. if (firstNodePrev.tag != head.tag)
  221. {
  222. FixList(tail, head);
  223. continue;
  224. }
  225. }
  226. else
  227. {
  228. nd_dummy = new (RegionAllocator::ii) Node<T>;
  229. nd_dummy->value = 0;
  230. nd_dummy->next.ptr = tail.ptr;
  231. nd_dummy->next.tag = tail.tag + 1;
  232. Ptr<T> new_tail;
  233. new_tail.ptr = nd_dummy;
  234. new_tail.tag = tail.tag + 1;
  235. if (CAS2(Tail, tail, new_tail))
  236. {
  237. head.ptr->prev.ptr = nd_dummy;
  238. head.ptr->prev.tag = tail.tag;
  239. }
  240. else
  241. {
  242. RegionAllocator::ii->Delete(nd_dummy);
  243. }
  244. continue;
  245. }
  246. Ptr<T> new_head;
  247. new_head.ptr = firstNodePrev.ptr;
  248. new_head.tag = head.tag + 1;
  249. if (CAS2(Head, head, new_head))
  250. {
  251. RegionAllocator::ii->Delete(head.ptr);
  252. return val;
  253. }
  254. }
  255. else
  256. {
  257. if (tail.ptr == head.ptr)
  258. return 0;
  259. if (firstNodePrev.tag != head.tag)
  260. {
  261. FixList(tail, head);
  262. continue;
  263. }
  264. Ptr<T> new_head;
  265. new_head.ptr = firstNodePrev.ptr;
  266. new_head.tag = head.tag + 1;
  267. CAS2(Head, head, new_head);
  268. }
  269. }
  270. }
  271. }
  272. template<class T>
  273. void Queue<T>::FixList(Ptr<T> tail, Ptr<T> head)
  274. {
  275. Ptr<T> curNode, curNodeNext, nextNodePrev;
  276. curNode = tail;
  277. while (head == Head && curNode != head)
  278. {
  279. curNodeNext = curNode.ptr->next;
  280. if (curNodeNext.tag != curNode.tag)
  281. return;
  282. nextNodePrev = curNodeNext.ptr->prev;
  283. if (nextNodePrev.ptr != curNode.ptr || nextNodePrev.tag != curNode.tag - 1)
  284. {
  285. curNodeNext.ptr->prev.ptr = curNode.ptr;
  286. curNodeNext.ptr->prev.tag = curNode.tag - 1;
  287. }
  288. curNode.ptr = curNodeNext.ptr;
  289. curNode.tag = curNode.tag - 1;
  290. }
  291. }
  292. } // namespace FIFO
  293. } // namespace cat
  294. #endif // CAT_LOCKLESS_FIFO_HPP
粤ICP备19079148号