Mirror of Svelto.ECS because we're a fan of it
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LockFreeQueue.cs 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System.Threading;
  2. //from unify wiki
  3. namespace Svelto.DataStructures
  4. {
  5. public class SingleLinkEntityView<T>
  6. {
  7. // Note; the Next member cannot be a property since
  8. // it participates in many CAS operations
  9. public SingleLinkEntityView<T> Next;
  10. public T Item;
  11. }
  12. public static class SyncMethods
  13. {
  14. public static bool CAS<T>(ref T location, T comparand, T newValue) where T : class
  15. {
  16. return
  17. (object)comparand ==
  18. (object)Interlocked.CompareExchange<T>(ref location, newValue, comparand);
  19. }
  20. }
  21. public class LockFreeLinkPool<T>
  22. {
  23. private SingleLinkEntityView<T> head;
  24. public LockFreeLinkPool()
  25. {
  26. head = new SingleLinkEntityView<T>();
  27. }
  28. public void Push(SingleLinkEntityView<T> newEntityView)
  29. {
  30. newEntityView.Item = default(T);
  31. do
  32. {
  33. newEntityView.Next = head.Next;
  34. } while (!SyncMethods.CAS<SingleLinkEntityView<T>>(ref head.Next, newEntityView.Next, newEntityView));
  35. return;
  36. }
  37. public bool Pop(out SingleLinkEntityView<T> entityView)
  38. {
  39. do
  40. {
  41. entityView = head.Next;
  42. if (entityView == null)
  43. {
  44. return false;
  45. }
  46. } while (!SyncMethods.CAS<SingleLinkEntityView<T>>(ref head.Next, entityView, entityView.Next));
  47. return true;
  48. }
  49. }
  50. public class LockFreeQueue<T>
  51. {
  52. SingleLinkEntityView<T> head;
  53. SingleLinkEntityView<T> tail;
  54. LockFreeLinkPool<T> trash;
  55. public LockFreeQueue()
  56. {
  57. head = new SingleLinkEntityView<T>();
  58. tail = head;
  59. trash = new LockFreeLinkPool<T>();
  60. }
  61. public void Enqueue(T item)
  62. {
  63. SingleLinkEntityView<T> oldTail = null;
  64. SingleLinkEntityView<T> oldTailNext;
  65. SingleLinkEntityView<T> newEntityView;
  66. if (!trash.Pop(out newEntityView))
  67. {
  68. newEntityView = new SingleLinkEntityView<T>();
  69. }
  70. else
  71. {
  72. newEntityView.Next = null;
  73. }
  74. newEntityView.Item = item;
  75. bool newEntityViewWasAdded = false;
  76. while (!newEntityViewWasAdded)
  77. {
  78. oldTail = tail;
  79. oldTailNext = oldTail.Next;
  80. if (tail == oldTail)
  81. {
  82. if (oldTailNext == null)
  83. newEntityViewWasAdded = SyncMethods.CAS<SingleLinkEntityView<T>>(ref tail.Next, null, newEntityView);
  84. else
  85. SyncMethods.CAS<SingleLinkEntityView<T>>(ref tail, oldTail, oldTailNext);
  86. }
  87. }
  88. SyncMethods.CAS<SingleLinkEntityView<T>>(ref tail, oldTail, newEntityView);
  89. }
  90. public bool Dequeue(out T item)
  91. {
  92. item = default(T);
  93. SingleLinkEntityView<T> oldHead = null;
  94. bool haveAdvancedHead = false;
  95. while (!haveAdvancedHead)
  96. {
  97. oldHead = head;
  98. SingleLinkEntityView<T> oldTail = tail;
  99. SingleLinkEntityView<T> oldHeadNext = oldHead.Next;
  100. if (oldHead == head)
  101. {
  102. if (oldHead == oldTail)
  103. {
  104. if (oldHeadNext == null)
  105. {
  106. return false;
  107. }
  108. SyncMethods.CAS<SingleLinkEntityView<T>>(ref tail, oldTail, oldHeadNext);
  109. }
  110. else
  111. {
  112. item = oldHeadNext.Item;
  113. haveAdvancedHead = SyncMethods.CAS<SingleLinkEntityView<T>>(ref head, oldHead, oldHeadNext);
  114. if (haveAdvancedHead)
  115. {
  116. trash.Push(oldHead);
  117. }
  118. }
  119. }
  120. }
  121. return true;
  122. }
  123. }
  124. }