ConcurrentQueue.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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, ISerializable, IDeserializationCallback
  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. [MonoTODO]
  53. protected ConcurrentQueue (SerializationInfo info, StreamingContext context)
  54. {
  55. throw new NotImplementedException ();
  56. }
  57. public void Enqueue (T item)
  58. {
  59. Interlocked.Increment (ref count);
  60. Node node = new Node ();
  61. node.Value = item;
  62. Node oldTail = null;
  63. Node oldNext = null;
  64. bool update = false;
  65. while (!update) {
  66. oldTail = tail;
  67. oldNext = oldTail.Next;
  68. // Did tail was already updated ?
  69. if (tail == oldTail) {
  70. if (oldNext == null) {
  71. // The place is for us
  72. update = Interlocked.CompareExchange (ref tail.Next, node, null) == null;
  73. } else {
  74. // another Thread already used the place so give him a hand by putting tail where it should be
  75. Interlocked.CompareExchange (ref tail, oldNext, oldTail);
  76. }
  77. }
  78. }
  79. // 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
  80. Interlocked.CompareExchange (ref tail, node, oldTail);
  81. }
  82. bool IProducerConsumerCollection<T>.TryAdd (T item)
  83. {
  84. Enqueue (item);
  85. return true;
  86. }
  87. /// <summary>
  88. /// </summary>
  89. /// <returns></returns>
  90. public bool TryDequeue (out T value)
  91. {
  92. value = default (T);
  93. bool advanced = false;
  94. while (!advanced) {
  95. Node oldHead = head;
  96. Node oldTail = tail;
  97. Node oldNext = oldHead.Next;
  98. if (oldHead == head) {
  99. // Empty case ?
  100. if (oldHead == oldTail) {
  101. // This should be false then
  102. if (oldNext != null) {
  103. // If not then the linked list is mal formed, update tail
  104. Interlocked.CompareExchange (ref tail, oldNext, oldTail);
  105. }
  106. value = default (T);
  107. return false;
  108. } else {
  109. value = oldNext.Value;
  110. advanced = Interlocked.CompareExchange (ref head, oldNext, oldHead) == oldHead;
  111. }
  112. }
  113. }
  114. Interlocked.Decrement (ref count);
  115. return true;
  116. }
  117. /// <summary>
  118. /// </summary>
  119. /// <returns></returns>
  120. public bool TryPeek (out T value)
  121. {
  122. if (IsEmpty) {
  123. value = default (T);
  124. return false;
  125. }
  126. Node first = head.Next;
  127. value = first.Value;
  128. return true;
  129. }
  130. internal void Clear ()
  131. {
  132. count = 0;
  133. tail = head = new Node ();
  134. }
  135. IEnumerator IEnumerable.GetEnumerator ()
  136. {
  137. return (IEnumerator)InternalGetEnumerator ();
  138. }
  139. IEnumerator<T> IEnumerable<T>.GetEnumerator ()
  140. {
  141. return InternalGetEnumerator ();
  142. }
  143. public IEnumerator<T> GetEnumerator ()
  144. {
  145. return InternalGetEnumerator ();
  146. }
  147. IEnumerator<T> InternalGetEnumerator ()
  148. {
  149. Node my_head = head;
  150. while ((my_head = my_head.Next) != null) {
  151. yield return my_head.Value;
  152. }
  153. }
  154. void ICollection.CopyTo (Array array, int index)
  155. {
  156. T[] dest = array as T[];
  157. if (dest == null)
  158. return;
  159. CopyTo (dest, index);
  160. }
  161. public void CopyTo (T[] dest, int index)
  162. {
  163. IEnumerator<T> e = InternalGetEnumerator ();
  164. int i = index;
  165. while (e.MoveNext ()) {
  166. dest [i++] = e.Current;
  167. }
  168. }
  169. public T[] ToArray ()
  170. {
  171. T[] dest = new T [count];
  172. CopyTo (dest, 0);
  173. return dest;
  174. }
  175. [MonoTODO]
  176. protected virtual void GetObjectData (SerializationInfo info, StreamingContext context)
  177. {
  178. throw new NotImplementedException ();
  179. }
  180. [MonoTODO]
  181. void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
  182. {
  183. GetObjectData (info, context);
  184. }
  185. bool ICollection.IsSynchronized {
  186. get { return true; }
  187. }
  188. [MonoTODO]
  189. protected virtual void OnDeserialization (object sender)
  190. {
  191. throw new NotImplementedException ();
  192. }
  193. void IDeserializationCallback.OnDeserialization (object sender)
  194. {
  195. OnDeserialization (sender);
  196. }
  197. bool IProducerConsumerCollection<T>.TryTake (out T item)
  198. {
  199. return TryDequeue (out item);
  200. }
  201. object syncRoot = new object();
  202. object ICollection.SyncRoot {
  203. get { return syncRoot; }
  204. }
  205. public int Count {
  206. get {
  207. return count;
  208. }
  209. }
  210. public bool IsEmpty {
  211. get {
  212. return count == 0;
  213. }
  214. }
  215. }
  216. }
  217. #endif