ConcurrentQueue.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #if NET_4_0 || BOOTSTRAP_NET_4_0
  2. // ConcurrentQueue.cs
  3. //
  4. // Copyright (c) 2008 Jérémie "Garuma" Laval
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. //
  24. //
  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. Interlocked.Increment (ref count);
  55. Node node = new Node ();
  56. node.Value = item;
  57. Node oldTail = null;
  58. Node oldNext = null;
  59. bool update = false;
  60. while (!update) {
  61. oldTail = tail;
  62. oldNext = oldTail.Next;
  63. // Did tail was already updated ?
  64. if (tail == oldTail) {
  65. if (oldNext == null) {
  66. // The place is for us
  67. update = Interlocked.CompareExchange (ref tail.Next, node, null) == null;
  68. } else {
  69. // another Thread already used the place so give him a hand by putting tail where it should be
  70. Interlocked.CompareExchange (ref tail, oldNext, oldTail);
  71. }
  72. }
  73. }
  74. // 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
  75. Interlocked.CompareExchange (ref tail, node, oldTail);
  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. IEnumerator<T> IEnumerable<T>.GetEnumerator ()
  129. {
  130. return InternalGetEnumerator ();
  131. }
  132. public IEnumerator<T> GetEnumerator ()
  133. {
  134. return InternalGetEnumerator ();
  135. }
  136. IEnumerator<T> InternalGetEnumerator ()
  137. {
  138. Node my_head = head;
  139. while ((my_head = my_head.Next) != null) {
  140. yield return my_head.Value;
  141. }
  142. }
  143. void ICollection.CopyTo (Array array, int index)
  144. {
  145. T[] dest = array as T[];
  146. if (dest == null)
  147. return;
  148. CopyTo (dest, index);
  149. }
  150. public void CopyTo (T[] dest, int index)
  151. {
  152. IEnumerator<T> e = InternalGetEnumerator ();
  153. int i = index;
  154. while (e.MoveNext ()) {
  155. dest [i++] = e.Current;
  156. }
  157. }
  158. public T[] ToArray ()
  159. {
  160. T[] dest = new T [count];
  161. CopyTo (dest, 0);
  162. return dest;
  163. }
  164. bool ICollection.IsSynchronized {
  165. get { return true; }
  166. }
  167. bool IProducerConsumerCollection<T>.TryTake (out T item)
  168. {
  169. return TryDequeue (out item);
  170. }
  171. object syncRoot = new object();
  172. object ICollection.SyncRoot {
  173. get { return syncRoot; }
  174. }
  175. public int Count {
  176. get {
  177. return count;
  178. }
  179. }
  180. public bool IsEmpty {
  181. get {
  182. return count == 0;
  183. }
  184. }
  185. }
  186. }
  187. #endif