| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552 |
- #if NET_4_0
- // BlockingCollection.cs
- //
- // Copyright (c) 2008 Jérémie "Garuma" Laval
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- //
- using System;
- using System.Threading;
- using System.Collections;
- using System.Collections.Generic;
- using System.Diagnostics;
- namespace System.Collections.Concurrent
- {
- public class BlockingCollection<T> : IEnumerable<T>, ICollection, IEnumerable, IDisposable
- {
- readonly IProducerConsumerCollection<T> underlyingColl;
- readonly int upperBound;
-
- readonly SpinWait sw = new SpinWait ();
-
- AtomicBoolean isComplete;
- long completeId;
- long addId = long.MinValue;
- long removeId = long.MinValue;
-
- #region ctors
- public BlockingCollection ()
- : this (new ConcurrentQueue<T> (), -1)
- {
- }
-
- public BlockingCollection (int upperBound)
- : this (new ConcurrentQueue<T> (), upperBound)
- {
- }
-
- public BlockingCollection (IProducerConsumerCollection<T> underlyingColl)
- : this (underlyingColl, -1)
- {
- }
-
- public BlockingCollection (IProducerConsumerCollection<T> underlyingColl, int upperBound)
- {
- this.underlyingColl = underlyingColl;
- this.upperBound = upperBound;
- this.isComplete = new AtomicBoolean ();
- }
- #endregion
-
- #region Add & Remove (+ Try)
- public void Add (T item)
- {
- Add (item, null);
- }
-
- public void Add (T item, CancellationToken token)
- {
- Add (item, () => token.IsCancellationRequested);
- }
-
- void Add (T item, Func<bool> cancellationFunc)
- {
- while (true) {
- long cachedAddId = addId;
- long cachedRemoveId = removeId;
-
- if (upperBound != -1) {
- if (cachedAddId - cachedRemoveId > upperBound) {
- Block ();
- continue;
- }
- }
-
- // Check our transaction id against completed stored one
- if (isComplete.Value && cachedAddId >= completeId)
- throw new InvalidOperationException ("The BlockingCollection<T> has"
- + " been marked as complete with regards to additions.");
-
- if (Interlocked.CompareExchange (ref addId, cachedAddId + 1, cachedAddId) == cachedAddId)
- break;
-
- if (cancellationFunc != null && cancellationFunc ())
- throw new OperationCanceledException ("CancellationToken triggered");
- }
-
-
- if (!underlyingColl.TryAdd (item))
- throw new InvalidOperationException ("The underlying collection didn't accept the item.");
- }
-
- public T Take ()
- {
- return Take (null);
- }
-
- public T Take (CancellationToken token)
- {
- return Take (() => token.IsCancellationRequested);
- }
-
- T Take (Func<bool> cancellationFunc)
- {
- while (true) {
- long cachedRemoveId = removeId;
- long cachedAddId = addId;
-
- // Empty case
- if (cachedRemoveId == cachedAddId) {
- if (isComplete.Value && cachedRemoveId >= completeId)
- throw new OperationCanceledException ("The BlockingCollection<T> has"
- + " been marked as complete with regards to additions.");
-
- Block ();
- continue;
- }
-
- if (Interlocked.CompareExchange (ref removeId, cachedRemoveId + 1, cachedRemoveId) == cachedRemoveId)
- break;
-
- if (cancellationFunc != null && cancellationFunc ())
- throw new OperationCanceledException ("The CancellationToken has had cancellation requested.");
- }
-
- T item;
- while (!underlyingColl.TryTake (out item));
-
- return item;
- }
-
- public bool TryAdd (T item)
- {
- return TryAdd (item, null, null);
- }
-
- bool TryAdd (T item, Func<bool> contFunc, CancellationToken? token)
- {
- do {
- if (token.HasValue && token.Value.IsCancellationRequested)
- throw new OperationCanceledException ("The CancellationToken has had cancellation requested.");
-
- long cachedAddId = addId;
- long cachedRemoveId = removeId;
-
- if (upperBound != -1) {
- if (cachedAddId - cachedRemoveId > upperBound) {
- continue;
- }
- }
-
- // Check our transaction id against completed stored one
- if (isComplete.Value && cachedAddId >= completeId)
- throw new InvalidOperationException ("The BlockingCollection<T> has"
- + " been marked as complete with regards to additions.");
-
- if (Interlocked.CompareExchange (ref addId, cachedAddId + 1, cachedAddId) != cachedAddId)
- continue;
-
- if (!underlyingColl.TryAdd (item))
- throw new InvalidOperationException ("The underlying collection didn't accept the item.");
-
- return true;
- } while (contFunc != null && contFunc ());
-
- return false;
- }
-
- public bool TryAdd (T item, TimeSpan ts)
- {
- return TryAdd (item, (int)ts.TotalMilliseconds);
- }
-
- public bool TryAdd (T item, int millisecondsTimeout)
- {
- Stopwatch sw = Stopwatch.StartNew ();
- return TryAdd (item, () => sw.ElapsedMilliseconds < millisecondsTimeout, null);
- }
-
- public bool TryAdd (T item, int millisecondsTimeout, CancellationToken token)
- {
- Stopwatch sw = Stopwatch.StartNew ();
- return TryAdd (item, () => sw.ElapsedMilliseconds < millisecondsTimeout, token);
- }
-
- public bool TryTake (out T item)
- {
- return TryTake (out item, null, null);
- }
-
- bool TryTake (out T item, Func<bool> contFunc, CancellationToken? token)
- {
- item = default (T);
-
- do {
- if (token.HasValue && token.Value.IsCancellationRequested)
- throw new OperationCanceledException ("The CancellationToken has had cancellation requested.");
-
- long cachedRemoveId = removeId;
- long cachedAddId = addId;
-
- // Empty case
- if (cachedRemoveId == cachedAddId) {
- if (isComplete.Value && cachedRemoveId >= completeId)
- continue;
-
- continue;
- }
-
- if (Interlocked.CompareExchange (ref removeId, cachedRemoveId + 1, cachedRemoveId) != cachedRemoveId)
- continue;
-
- return underlyingColl.TryTake (out item);
- } while (contFunc != null && contFunc ());
-
- return false;
- }
-
- public bool TryTake (out T item, TimeSpan ts)
- {
- return TryTake (out item, (int)ts.TotalMilliseconds);
- }
-
- public bool TryTake (out T item, int millisecondsTimeout)
- {
- item = default (T);
- Stopwatch sw = Stopwatch.StartNew ();
-
- return TryTake (out item, () => sw.ElapsedMilliseconds < millisecondsTimeout, null);
- }
-
- public bool TryTake (out T item, int millisecondsTimeout, CancellationToken token)
- {
- item = default (T);
- Stopwatch sw = Stopwatch.StartNew ();
-
- return TryTake (out item, () => sw.ElapsedMilliseconds < millisecondsTimeout, token);
- }
- #endregion
-
- #region static methods
- static void CheckArray (BlockingCollection<T>[] collections)
- {
- if (collections == null)
- throw new ArgumentNullException ("collections");
- if (collections.Length == 0 || IsThereANullElement (collections))
- throw new ArgumentException ("The collections argument is a 0-length array or contains a null element.", "collections");
- }
-
- static bool IsThereANullElement (BlockingCollection<T>[] collections)
- {
- foreach (BlockingCollection<T> e in collections)
- if (e == null)
- return true;
- return false;
- }
-
- public static int AddToAny (BlockingCollection<T>[] collections, T item)
- {
- CheckArray (collections);
- int index = 0;
- foreach (var coll in collections) {
- try {
- coll.Add (item);
- return index;
- } catch {}
- index++;
- }
- return -1;
- }
-
- public static int AddToAny (BlockingCollection<T>[] collections, T item, CancellationToken token)
- {
- CheckArray (collections);
- int index = 0;
- foreach (var coll in collections) {
- try {
- coll.Add (item, token);
- return index;
- } catch {}
- index++;
- }
- return -1;
- }
-
- public static int TryAddToAny (BlockingCollection<T>[] collections, T item)
- {
- CheckArray (collections);
- int index = 0;
- foreach (var coll in collections) {
- if (coll.TryAdd (item))
- return index;
- index++;
- }
- return -1;
- }
-
- public static int TryAddToAny (BlockingCollection<T>[] collections, T item, TimeSpan ts)
- {
- CheckArray (collections);
- int index = 0;
- foreach (var coll in collections) {
- if (coll.TryAdd (item, ts))
- return index;
- index++;
- }
- return -1;
- }
-
- public static int TryAddToAny (BlockingCollection<T>[] collections, T item, int millisecondsTimeout)
- {
- CheckArray (collections);
- int index = 0;
- foreach (var coll in collections) {
- if (coll.TryAdd (item, millisecondsTimeout))
- return index;
- index++;
- }
- return -1;
- }
-
- public static int TryAddToAny (BlockingCollection<T>[] collections, T item, int millisecondsTimeout,
- CancellationToken token)
- {
- CheckArray (collections);
- int index = 0;
- foreach (var coll in collections) {
- if (coll.TryAdd (item, millisecondsTimeout, token))
- return index;
- index++;
- }
- return -1;
- }
-
- public static int TakeFromAny (BlockingCollection<T>[] collections, out T item)
- {
- item = default (T);
- CheckArray (collections);
- int index = 0;
- foreach (var coll in collections) {
- try {
- item = coll.Take ();
- return index;
- } catch {}
- index++;
- }
- return -1;
- }
-
- public static int TakeFromAny (BlockingCollection<T>[] collections, out T item, CancellationToken token)
- {
- item = default (T);
- CheckArray (collections);
- int index = 0;
- foreach (var coll in collections) {
- try {
- item = coll.Take (token);
- return index;
- } catch {}
- index++;
- }
- return -1;
- }
-
- public static int TryTakeFromAny (BlockingCollection<T>[] collections, out T item)
- {
- item = default (T);
-
- CheckArray (collections);
- int index = 0;
- foreach (var coll in collections) {
- if (coll.TryTake (out item))
- return index;
- index++;
- }
- return -1;
- }
-
- public static int TryTakeFromAny (BlockingCollection<T>[] collections, out T item, TimeSpan ts)
- {
- item = default (T);
-
- CheckArray (collections);
- int index = 0;
- foreach (var coll in collections) {
- if (coll.TryTake (out item, ts))
- return index;
- index++;
- }
- return -1;
- }
-
- public static int TryTakeFromAny (BlockingCollection<T>[] collections, out T item, int millisecondsTimeout)
- {
- item = default (T);
-
- CheckArray (collections);
- int index = 0;
- foreach (var coll in collections) {
- if (coll.TryTake (out item, millisecondsTimeout))
- return index;
- index++;
- }
- return -1;
- }
-
- public static int TryTakeFromAny (BlockingCollection<T>[] collections, out T item, int millisecondsTimeout,
- CancellationToken token)
- {
- item = default (T);
-
- CheckArray (collections);
- int index = 0;
- foreach (var coll in collections) {
- if (coll.TryTake (out item, millisecondsTimeout, token))
- return index;
- index++;
- }
- return -1;
- }
- #endregion
-
- public void CompleteAdding ()
- {
- // No further add beside that point
- completeId = addId;
- isComplete.Value = true;
- }
-
- void ICollection.CopyTo (Array array, int index)
- {
- underlyingColl.CopyTo (array, index);
- }
-
- public void CopyTo (T[] array, int index)
- {
- underlyingColl.CopyTo (array, index);
- }
-
- public IEnumerable<T> GetConsumingEnumerable ()
- {
- return GetConsumingEnumerable (Take);
- }
-
- public IEnumerable<T> GetConsumingEnumerable (CancellationToken token)
- {
- return GetConsumingEnumerable (() => Take (token));
- }
-
- IEnumerable<T> GetConsumingEnumerable (Func<T> getFunc)
- {
- while (true) {
- T item = default (T);
-
- try {
- item = getFunc ();
- } catch {
- break;
- }
-
- yield return item;
- }
- }
-
- IEnumerator IEnumerable.GetEnumerator ()
- {
- return ((IEnumerable)underlyingColl).GetEnumerator ();
- }
-
- IEnumerator<T> IEnumerable<T>.GetEnumerator ()
- {
- return ((IEnumerable<T>)underlyingColl).GetEnumerator ();
- }
-
- public void Dispose ()
- {
-
- }
-
- protected virtual void Dispose (bool managedRes)
- {
-
- }
-
- public T[] ToArray ()
- {
- return underlyingColl.ToArray ();
- }
-
- // Method used to stall the thread for a limited period of time before retrying an operation
- void Block ()
- {
- sw.SpinOnce ();
- }
-
- public int BoundedCapacity {
- get {
- return upperBound;
- }
- }
-
- public int Count {
- get {
- return underlyingColl.Count;
- }
- }
-
- public bool IsAddingCompleted {
- get {
- return isComplete.Value;
- }
- }
-
- public bool IsCompleted {
- get {
- return isComplete.Value && addId == removeId;
- }
- }
-
- object ICollection.SyncRoot {
- get {
- return underlyingColl.SyncRoot;
- }
- }
-
- bool ICollection.IsSynchronized {
- get {
- return underlyingColl.IsSynchronized;
- }
- }
- }
- }
- #endif
|