ConcurrentQueue.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // ConcurrentQueue.cs
  2. //
  3. // Copyright (c) 2008 Jérémie "Garuma" Laval
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. //
  24. #if NET_4_0 || BOOTSTRAP_NET_4_0
  25. using System;
  26. using System.Threading;
  27. using System.Collections;
  28. using System.Collections.Generic;
  29. using System.Runtime.Serialization;
  30. namespace System.Collections.Concurrent
  31. {
  32. public class ConcurrentQueue<T> : IProducerConsumerCollection<T>, IEnumerable<T>, ICollection,
  33. IEnumerable
  34. {
  35. class Node
  36. {
  37. public T Value;
  38. public Node Next;
  39. }
  40. Node head = new Node ();
  41. Node tail;
  42. int count;
  43. public ConcurrentQueue ()
  44. {
  45. tail = head;
  46. }
  47. public ConcurrentQueue (IEnumerable<T> enumerable): this()
  48. {
  49. foreach (T item in enumerable)
  50. Enqueue (item);
  51. }
  52. public void Enqueue (T item)
  53. {
  54. Node node = new Node ();
  55. node.Value = item;
  56. Node oldTail = null;
  57. Node oldNext = null;
  58. bool update = false;
  59. while (!update) {
  60. oldTail = tail;
  61. oldNext = oldTail.Next;
  62. // Did tail was already updated ?
  63. if (tail == oldTail) {
  64. if (oldNext == null) {
  65. // The place is for us
  66. update = Interlocked.CompareExchange (ref tail.Next, node, null) == null;
  67. } else {
  68. // another Thread already used the place so give him a hand by putting tail where it should be
  69. Interlocked.CompareExchange (ref tail, oldNext, oldTail);
  70. }
  71. }
  72. }
  73. // At this point we added correctly our node, now we have to update tail. If it fails then it will be done by another thread
  74. Interlocked.CompareExchange (ref tail, node, oldTail);
  75. Interlocked.Increment (ref count);
  76. }
  77. bool IProducerConsumerCollection<T>.TryAdd (T item)
  78. {
  79. Enqueue (item);
  80. return true;
  81. }
  82. public bool TryDequeue (out T value)
  83. {
  84. value = default (T);
  85. bool advanced = false;
  86. while (!advanced) {
  87. Node oldHead = head;
  88. Node oldTail = tail;
  89. Node oldNext = oldHead.Next;
  90. if (oldHead == head) {
  91. // Empty case ?
  92. if (oldHead == oldTail) {
  93. // This should be false then
  94. if (oldNext != null) {
  95. // If not then the linked list is mal formed, update tail
  96. Interlocked.CompareExchange (ref tail, oldNext, oldTail);
  97. }
  98. value = default (T);
  99. return false;
  100. } else {
  101. value = oldNext.Value;
  102. advanced = Interlocked.CompareExchange (ref head, oldNext, oldHead) == oldHead;
  103. }
  104. }
  105. }
  106. Interlocked.Decrement (ref count);
  107. return true;
  108. }
  109. public bool TryPeek (out T value)
  110. {
  111. if (IsEmpty) {
  112. value = default (T);
  113. return false;
  114. }
  115. Node first = head.Next;
  116. value = first.Value;
  117. return true;
  118. }
  119. internal void Clear ()
  120. {
  121. count = 0;
  122. tail = head = new Node ();
  123. }
  124. IEnumerator IEnumerable.GetEnumerator ()
  125. {
  126. return (IEnumerator)InternalGetEnumerator ();
  127. }
  128. public IEnumerator<T> GetEnumerator ()
  129. {
  130. return InternalGetEnumerator ();
  131. }
  132. IEnumerator<T> InternalGetEnumerator ()
  133. {
  134. Node my_head = head;
  135. while ((my_head = my_head.Next) != null) {
  136. yield return my_head.Value;
  137. }
  138. }
  139. void ICollection.CopyTo (Array array, int index)
  140. {
  141. T[] dest = array as T[];
  142. if (dest == null)
  143. return;
  144. CopyTo (dest, index);
  145. }
  146. public void CopyTo (T[] dest, int index)
  147. {
  148. IEnumerator<T> e = InternalGetEnumerator ();
  149. int i = index;
  150. while (e.MoveNext ()) {
  151. dest [i++] = e.Current;
  152. }
  153. }
  154. public T[] ToArray ()
  155. {
  156. T[] dest = new T [count];
  157. CopyTo (dest, 0);
  158. return dest;
  159. }
  160. bool ICollection.IsSynchronized {
  161. get { return true; }
  162. }
  163. bool IProducerConsumerCollection<T>.TryTake (out T item)
  164. {
  165. return TryDequeue (out item);
  166. }
  167. object syncRoot = new object();
  168. object ICollection.SyncRoot {
  169. get { return syncRoot; }
  170. }
  171. public int Count {
  172. get {
  173. return count;
  174. }
  175. }
  176. public bool IsEmpty {
  177. get {
  178. return count == 0;
  179. }
  180. }
  181. }
  182. }
  183. #endif