ConcurrentStack.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. #if NET_4_0
  2. // ConcurrentStack.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 ConcurrentStack<T> : IProducerConsumerCollection<T>, IEnumerable<T>,
  33. ICollection, IEnumerable, ISerializable, IDeserializationCallback
  34. {
  35. class Node
  36. {
  37. public T Value = default (T);
  38. public Node Next = null;
  39. }
  40. Node head = null;
  41. int count;
  42. public ConcurrentStack ()
  43. {
  44. }
  45. public ConcurrentStack (IEnumerable<T> enumerable)
  46. {
  47. foreach (T item in enumerable)
  48. Push (item);
  49. }
  50. [MonoTODO]
  51. protected ConcurrentStack (SerializationInfo info, StreamingContext context)
  52. {
  53. throw new NotImplementedException ();
  54. }
  55. bool IProducerConsumerCollection<T>.TryAdd (T elem)
  56. {
  57. Push (elem);
  58. return true;
  59. }
  60. public void Push (T element)
  61. {
  62. Node temp = new Node ();
  63. temp.Value = element;
  64. do {
  65. temp.Next = head;
  66. } while (Interlocked.CompareExchange (ref head, temp, temp.Next) != temp.Next);
  67. Interlocked.Increment (ref count);
  68. }
  69. public void PushRange (T[] values)
  70. {
  71. PushRange (values, 0, values.Length);
  72. }
  73. public void PushRange (T[] values, int start, int length)
  74. {
  75. Node insert = null;
  76. Node first = null;
  77. for (int i = start; i < length; i++) {
  78. Node temp = new Node ();
  79. temp.Value = values[i];
  80. temp.Next = insert;
  81. insert = temp;
  82. if (first == null)
  83. first = temp;
  84. }
  85. do {
  86. first.Next = head;
  87. } while (Interlocked.CompareExchange (ref head, insert, first.Next) != first.Next);
  88. Interlocked.Add (ref count, length);
  89. }
  90. public bool TryPop (out T value)
  91. {
  92. Node temp;
  93. do {
  94. temp = head;
  95. // The stak is empty
  96. if (temp == null) {
  97. value = default (T);
  98. return false;
  99. }
  100. } while (Interlocked.CompareExchange (ref head, temp.Next, temp) != temp);
  101. Interlocked.Decrement (ref count);
  102. value = temp.Value;
  103. return true;
  104. }
  105. public int TryPopRange (T[] values)
  106. {
  107. return TryPopRange (values, 0, values.Length);
  108. }
  109. public int TryPopRange (T[] values, int startIndex, int count)
  110. {
  111. Node temp;
  112. Node end;
  113. do {
  114. temp = head;
  115. if (temp == null)
  116. return -1;
  117. end = temp;
  118. for (int j = 0; j < count - 1; j++) {
  119. end = end.Next;
  120. if (end == null)
  121. break;
  122. }
  123. } while (Interlocked.CompareExchange (ref head, end, temp) != temp);
  124. int i;
  125. for (i = startIndex; i < count && temp != null; i++) {
  126. values[i] = temp.Value;
  127. temp = temp.Next;
  128. }
  129. return i - 1;
  130. }
  131. public bool TryPeek (out T value)
  132. {
  133. Node myHead = head;
  134. if (myHead == null) {
  135. value = default (T);
  136. return false;
  137. }
  138. value = myHead.Value;
  139. return true;
  140. }
  141. public void Clear ()
  142. {
  143. // This is not satisfactory
  144. count = 0;
  145. head = null;
  146. }
  147. IEnumerator IEnumerable.GetEnumerator ()
  148. {
  149. return (IEnumerator)InternalGetEnumerator ();
  150. }
  151. IEnumerator<T> IEnumerable<T>.GetEnumerator ()
  152. {
  153. return InternalGetEnumerator ();
  154. }
  155. public IEnumerator<T> GetEnumerator ()
  156. {
  157. return InternalGetEnumerator ();
  158. }
  159. IEnumerator<T> InternalGetEnumerator ()
  160. {
  161. Node my_head = head;
  162. if (my_head == null) {
  163. yield break;
  164. } else {
  165. do {
  166. yield return my_head.Value;
  167. } while ((my_head = my_head.Next) != null);
  168. }
  169. }
  170. void ICollection.CopyTo (Array array, int index)
  171. {
  172. T[] dest = array as T[];
  173. if (dest == null)
  174. return;
  175. CopyTo (dest, index);
  176. }
  177. public void CopyTo (T[] dest, int index)
  178. {
  179. IEnumerator<T> e = InternalGetEnumerator ();
  180. int i = index;
  181. while (e.MoveNext ()) {
  182. dest[i++] = e.Current;
  183. }
  184. }
  185. [MonoTODO]
  186. protected virtual void GetObjectData (SerializationInfo info, StreamingContext context)
  187. {
  188. throw new NotImplementedException ();
  189. }
  190. [MonoTODO]
  191. void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
  192. {
  193. GetObjectData (info, context);
  194. }
  195. bool ICollection.IsSynchronized {
  196. get { return true; }
  197. }
  198. [MonoTODO]
  199. protected virtual void OnDeserialization (object sender)
  200. {
  201. throw new NotImplementedException ();
  202. }
  203. void IDeserializationCallback.OnDeserialization (object sender)
  204. {
  205. OnDeserialization (sender);
  206. }
  207. bool IProducerConsumerCollection<T>.TryTake (out T item)
  208. {
  209. return TryPop (out item);
  210. }
  211. object syncRoot = new object ();
  212. object ICollection.SyncRoot {
  213. get { return syncRoot; }
  214. }
  215. public T[] ToArray ()
  216. {
  217. T[] dest = new T [count];
  218. CopyTo (dest, 0);
  219. return dest;
  220. }
  221. public int Count {
  222. get {
  223. return count;
  224. }
  225. }
  226. public bool IsEmpty {
  227. get {
  228. return count == 0;
  229. }
  230. }
  231. }
  232. }
  233. #endif