ConcurrentStack.cs 5.1 KB

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