BlockingCollection.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. //
  2. // BlockingCollection.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. #if NET_4_0 || BOOTSTRAP_NET_4_0
  26. using System;
  27. using System.Threading;
  28. using System.Collections;
  29. using System.Collections.Generic;
  30. using System.Diagnostics;
  31. using System.Runtime.InteropServices;
  32. namespace System.Collections.Concurrent
  33. {
  34. [ComVisible (false)]
  35. [DebuggerDisplay ("Count={Count}")]
  36. [DebuggerTypeProxy (typeof (CollectionDebuggerView<>))]
  37. public class BlockingCollection<T> : IEnumerable<T>, ICollection, IEnumerable, IDisposable
  38. {
  39. readonly IProducerConsumerCollection<T> underlyingColl;
  40. readonly int upperBound;
  41. AtomicBoolean isComplete;
  42. long completeId;
  43. long addId = long.MinValue;
  44. long removeId = long.MinValue;
  45. #region ctors
  46. public BlockingCollection ()
  47. : this (new ConcurrentQueue<T> (), -1)
  48. {
  49. }
  50. public BlockingCollection (int upperBound)
  51. : this (new ConcurrentQueue<T> (), upperBound)
  52. {
  53. }
  54. public BlockingCollection (IProducerConsumerCollection<T> underlyingColl)
  55. : this (underlyingColl, -1)
  56. {
  57. }
  58. public BlockingCollection (IProducerConsumerCollection<T> underlyingColl, int upperBound)
  59. {
  60. this.underlyingColl = underlyingColl;
  61. this.upperBound = upperBound;
  62. this.isComplete = new AtomicBoolean ();
  63. }
  64. #endregion
  65. #region Add & Remove (+ Try)
  66. public void Add (T item)
  67. {
  68. Add (item, CancellationToken.None);
  69. }
  70. public void Add (T item, CancellationToken token)
  71. {
  72. SpinWait sw = new SpinWait ();
  73. while (true) {
  74. token.ThrowIfCancellationRequested ();
  75. long cachedAddId = addId;
  76. long cachedRemoveId = removeId;
  77. if (upperBound != -1) {
  78. if (cachedAddId - cachedRemoveId > upperBound) {
  79. sw.SpinOnce ();
  80. continue;
  81. }
  82. }
  83. // Check our transaction id against completed stored one
  84. if (isComplete.Value && cachedAddId >= completeId)
  85. throw new InvalidOperationException ("The BlockingCollection<T> has"
  86. + " been marked as complete with regards to additions.");
  87. if (Interlocked.CompareExchange (ref addId, cachedAddId + 1, cachedAddId) == cachedAddId)
  88. break;
  89. }
  90. if (!underlyingColl.TryAdd (item))
  91. throw new InvalidOperationException ("The underlying collection didn't accept the item.");
  92. }
  93. public T Take ()
  94. {
  95. return Take (CancellationToken.None);
  96. }
  97. public T Take (CancellationToken token)
  98. {
  99. SpinWait sw = new SpinWait ();
  100. while (true) {
  101. token.ThrowIfCancellationRequested ();
  102. long cachedRemoveId = removeId;
  103. long cachedAddId = addId;
  104. // Empty case
  105. if (cachedRemoveId == cachedAddId) {
  106. if (IsCompleted)
  107. throw new OperationCanceledException ("The BlockingCollection<T> has"
  108. + " been marked as complete with regards to additions.");
  109. sw.SpinOnce ();
  110. continue;
  111. }
  112. if (Interlocked.CompareExchange (ref removeId, cachedRemoveId + 1, cachedRemoveId) == cachedRemoveId)
  113. break;
  114. }
  115. T item;
  116. while (!underlyingColl.TryTake (out item));
  117. return item;
  118. }
  119. public bool TryAdd (T item)
  120. {
  121. return TryAdd (item, null, CancellationToken.None);
  122. }
  123. bool TryAdd (T item, Func<bool> contFunc, CancellationToken token)
  124. {
  125. do {
  126. token.ThrowIfCancellationRequested ();
  127. long cachedAddId = addId;
  128. long cachedRemoveId = removeId;
  129. if (upperBound != -1) {
  130. if (cachedAddId - cachedRemoveId > upperBound) {
  131. continue;
  132. }
  133. }
  134. // Check our transaction id against completed stored one
  135. if (isComplete.Value && cachedAddId >= completeId)
  136. throw new InvalidOperationException ("The BlockingCollection<T> has"
  137. + " been marked as complete with regards to additions.");
  138. if (Interlocked.CompareExchange (ref addId, cachedAddId + 1, cachedAddId) != cachedAddId)
  139. continue;
  140. if (!underlyingColl.TryAdd (item))
  141. throw new InvalidOperationException ("The underlying collection didn't accept the item.");
  142. return true;
  143. } while (contFunc != null && contFunc ());
  144. return false;
  145. }
  146. public bool TryAdd (T item, TimeSpan ts)
  147. {
  148. return TryAdd (item, (int)ts.TotalMilliseconds);
  149. }
  150. public bool TryAdd (T item, int millisecondsTimeout)
  151. {
  152. Stopwatch sw = Stopwatch.StartNew ();
  153. return TryAdd (item, () => sw.ElapsedMilliseconds < millisecondsTimeout, CancellationToken.None);
  154. }
  155. public bool TryAdd (T item, int millisecondsTimeout, CancellationToken token)
  156. {
  157. Stopwatch sw = Stopwatch.StartNew ();
  158. return TryAdd (item, () => sw.ElapsedMilliseconds < millisecondsTimeout, token);
  159. }
  160. public bool TryTake (out T item)
  161. {
  162. return TryTake (out item, null, CancellationToken.None);
  163. }
  164. bool TryTake (out T item, Func<bool> contFunc, CancellationToken token)
  165. {
  166. item = default (T);
  167. do {
  168. token.ThrowIfCancellationRequested ();
  169. long cachedRemoveId = removeId;
  170. long cachedAddId = addId;
  171. // Empty case
  172. if (cachedRemoveId == cachedAddId) {
  173. if (IsCompleted)
  174. return false;
  175. continue;
  176. }
  177. if (Interlocked.CompareExchange (ref removeId, cachedRemoveId + 1, cachedRemoveId) != cachedRemoveId)
  178. continue;
  179. return underlyingColl.TryTake (out item);
  180. } while (contFunc != null && contFunc ());
  181. return false;
  182. }
  183. public bool TryTake (out T item, TimeSpan ts)
  184. {
  185. return TryTake (out item, (int)ts.TotalMilliseconds);
  186. }
  187. public bool TryTake (out T item, int millisecondsTimeout)
  188. {
  189. item = default (T);
  190. Stopwatch sw = Stopwatch.StartNew ();
  191. return TryTake (out item, () => sw.ElapsedMilliseconds < millisecondsTimeout, CancellationToken.None);
  192. }
  193. public bool TryTake (out T item, int millisecondsTimeout, CancellationToken token)
  194. {
  195. item = default (T);
  196. Stopwatch sw = Stopwatch.StartNew ();
  197. return TryTake (out item, () => sw.ElapsedMilliseconds < millisecondsTimeout, token);
  198. }
  199. #endregion
  200. #region static methods
  201. static void CheckArray (BlockingCollection<T>[] collections)
  202. {
  203. if (collections == null)
  204. throw new ArgumentNullException ("collections");
  205. if (collections.Length == 0 || IsThereANullElement (collections))
  206. throw new ArgumentException ("The collections argument is a 0-length array or contains a null element.", "collections");
  207. }
  208. static bool IsThereANullElement (BlockingCollection<T>[] collections)
  209. {
  210. foreach (BlockingCollection<T> e in collections)
  211. if (e == null)
  212. return true;
  213. return false;
  214. }
  215. public static int AddToAny (BlockingCollection<T>[] collections, T item)
  216. {
  217. CheckArray (collections);
  218. int index = 0;
  219. foreach (var coll in collections) {
  220. try {
  221. coll.Add (item);
  222. return index;
  223. } catch {}
  224. index++;
  225. }
  226. return -1;
  227. }
  228. public static int AddToAny (BlockingCollection<T>[] collections, T item, CancellationToken token)
  229. {
  230. CheckArray (collections);
  231. int index = 0;
  232. foreach (var coll in collections) {
  233. try {
  234. coll.Add (item, token);
  235. return index;
  236. } catch {}
  237. index++;
  238. }
  239. return -1;
  240. }
  241. public static int TryAddToAny (BlockingCollection<T>[] collections, T item)
  242. {
  243. CheckArray (collections);
  244. int index = 0;
  245. foreach (var coll in collections) {
  246. if (coll.TryAdd (item))
  247. return index;
  248. index++;
  249. }
  250. return -1;
  251. }
  252. public static int TryAddToAny (BlockingCollection<T>[] collections, T item, TimeSpan ts)
  253. {
  254. CheckArray (collections);
  255. int index = 0;
  256. foreach (var coll in collections) {
  257. if (coll.TryAdd (item, ts))
  258. return index;
  259. index++;
  260. }
  261. return -1;
  262. }
  263. public static int TryAddToAny (BlockingCollection<T>[] collections, T item, int millisecondsTimeout)
  264. {
  265. CheckArray (collections);
  266. int index = 0;
  267. foreach (var coll in collections) {
  268. if (coll.TryAdd (item, millisecondsTimeout))
  269. return index;
  270. index++;
  271. }
  272. return -1;
  273. }
  274. public static int TryAddToAny (BlockingCollection<T>[] collections, T item, int millisecondsTimeout,
  275. CancellationToken token)
  276. {
  277. CheckArray (collections);
  278. int index = 0;
  279. foreach (var coll in collections) {
  280. if (coll.TryAdd (item, millisecondsTimeout, token))
  281. return index;
  282. index++;
  283. }
  284. return -1;
  285. }
  286. public static int TakeFromAny (BlockingCollection<T>[] collections, out T item)
  287. {
  288. item = default (T);
  289. CheckArray (collections);
  290. int index = 0;
  291. foreach (var coll in collections) {
  292. try {
  293. item = coll.Take ();
  294. return index;
  295. } catch {}
  296. index++;
  297. }
  298. return -1;
  299. }
  300. public static int TakeFromAny (BlockingCollection<T>[] collections, out T item, CancellationToken token)
  301. {
  302. item = default (T);
  303. CheckArray (collections);
  304. int index = 0;
  305. foreach (var coll in collections) {
  306. try {
  307. item = coll.Take (token);
  308. return index;
  309. } catch {}
  310. index++;
  311. }
  312. return -1;
  313. }
  314. public static int TryTakeFromAny (BlockingCollection<T>[] collections, out T item)
  315. {
  316. item = default (T);
  317. CheckArray (collections);
  318. int index = 0;
  319. foreach (var coll in collections) {
  320. if (coll.TryTake (out item))
  321. return index;
  322. index++;
  323. }
  324. return -1;
  325. }
  326. public static int TryTakeFromAny (BlockingCollection<T>[] collections, out T item, TimeSpan ts)
  327. {
  328. item = default (T);
  329. CheckArray (collections);
  330. int index = 0;
  331. foreach (var coll in collections) {
  332. if (coll.TryTake (out item, ts))
  333. return index;
  334. index++;
  335. }
  336. return -1;
  337. }
  338. public static int TryTakeFromAny (BlockingCollection<T>[] collections, out T item, int millisecondsTimeout)
  339. {
  340. item = default (T);
  341. CheckArray (collections);
  342. int index = 0;
  343. foreach (var coll in collections) {
  344. if (coll.TryTake (out item, millisecondsTimeout))
  345. return index;
  346. index++;
  347. }
  348. return -1;
  349. }
  350. public static int TryTakeFromAny (BlockingCollection<T>[] collections, out T item, int millisecondsTimeout,
  351. CancellationToken token)
  352. {
  353. item = default (T);
  354. CheckArray (collections);
  355. int index = 0;
  356. foreach (var coll in collections) {
  357. if (coll.TryTake (out item, millisecondsTimeout, token))
  358. return index;
  359. index++;
  360. }
  361. return -1;
  362. }
  363. #endregion
  364. public void CompleteAdding ()
  365. {
  366. // No further add beside that point
  367. completeId = addId;
  368. isComplete.Value = true;
  369. }
  370. void ICollection.CopyTo (Array array, int index)
  371. {
  372. underlyingColl.CopyTo (array, index);
  373. }
  374. public void CopyTo (T[] array, int index)
  375. {
  376. underlyingColl.CopyTo (array, index);
  377. }
  378. public IEnumerable<T> GetConsumingEnumerable ()
  379. {
  380. return GetConsumingEnumerable (Take);
  381. }
  382. public IEnumerable<T> GetConsumingEnumerable (CancellationToken token)
  383. {
  384. return GetConsumingEnumerable (() => Take (token));
  385. }
  386. IEnumerable<T> GetConsumingEnumerable (Func<T> getFunc)
  387. {
  388. while (true) {
  389. T item = default (T);
  390. try {
  391. item = getFunc ();
  392. } catch {
  393. break;
  394. }
  395. yield return item;
  396. }
  397. }
  398. IEnumerator IEnumerable.GetEnumerator ()
  399. {
  400. return ((IEnumerable)underlyingColl).GetEnumerator ();
  401. }
  402. IEnumerator<T> IEnumerable<T>.GetEnumerator ()
  403. {
  404. return ((IEnumerable<T>)underlyingColl).GetEnumerator ();
  405. }
  406. public void Dispose ()
  407. {
  408. }
  409. protected virtual void Dispose (bool managedRes)
  410. {
  411. }
  412. public T[] ToArray ()
  413. {
  414. return underlyingColl.ToArray ();
  415. }
  416. public int BoundedCapacity {
  417. get {
  418. return upperBound;
  419. }
  420. }
  421. public int Count {
  422. get {
  423. return underlyingColl.Count;
  424. }
  425. }
  426. public bool IsAddingCompleted {
  427. get {
  428. return isComplete.Value;
  429. }
  430. }
  431. public bool IsCompleted {
  432. get {
  433. return isComplete.Value && addId == removeId;
  434. }
  435. }
  436. object ICollection.SyncRoot {
  437. get {
  438. return underlyingColl.SyncRoot;
  439. }
  440. }
  441. bool ICollection.IsSynchronized {
  442. get {
  443. return underlyingColl.IsSynchronized;
  444. }
  445. }
  446. }
  447. }
  448. #endif