1 // Copyright (c) Microsoft. All rights reserved.
2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
4 // =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
6 // IProducerConsumerCollection.cs
9 // A common interface for all concurrent collections.
11 // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
13 using System.Collections;
14 using System.Collections.Generic;
15 using System.Diagnostics;
17 namespace System.Threading.Tasks.Dataflow.Internal.Collections
20 /// Defines methods to manipulate thread-safe collections intended for producer/consumer usage.
22 /// <typeparam name="T">Specifies the type of elements in the collection.</typeparam>
24 /// All implementations of this interface must enable all members of this interface
25 /// to be used concurrently from multiple threads.
27 internal interface IProducerConsumerCollection<T> : IEnumerable<T>, ICollection
30 /// Copies the elements of the <see cref="IProducerConsumerCollection{T}"/> to
32 /// <see cref="T:System.Array"/>, starting at a specified index.
34 /// <param name="array">The one-dimensional <see cref="T:System.Array"/> that is the destination of
35 /// the elements copied from the <see cref="IProducerConsumerCollection{T}"/>.
36 /// The array must have zero-based indexing.</param>
37 /// <param name="index">The zero-based index in <paramref name="array"/> at which copying
39 /// <exception cref="ArgumentNullException"><paramref name="array"/> is a null reference (Nothing in
40 /// Visual Basic).</exception>
41 /// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> is less than
43 /// <exception cref="ArgumentException"><paramref name="index"/> is equal to or greater than the
44 /// length of the <paramref name="array"/>
45 /// -or- The number of elements in the source <see cref="ConcurrentQueue{T}"/> is greater than the
46 /// available space from <paramref name="index"/> to the end of the destination <paramref
49 void CopyTo(T[] array, int index);
52 /// Attempts to add an object to the <see
53 /// cref="IProducerConsumerCollection{T}"/>.
55 /// <param name="item">The object to add to the <see
56 /// cref="IProducerConsumerCollection{T}"/>.</param>
57 /// <returns>true if the object was added successfully; otherwise, false.</returns>
58 /// <exception cref="T:System.ArgumentException">The <paramref name="item"/> was invalid for this collection.</exception>
62 /// Attempts to remove and return an object from the <see cref="IProducerConsumerCollection{T}"/>.
64 /// <param name="item">
65 /// When this method returns, if the object was removed and returned successfully, <paramref
66 /// name="item"/> contains the removed object. If no object was available to be removed, the value is
69 /// <returns>true if an object was removed and returned successfully; otherwise, false.</returns>
70 bool TryTake(out T item);
73 /// Copies the elements contained in the <see cref="IProducerConsumerCollection{T}"/> to a new array.
75 /// <returns>A new array containing the elements copied from the <see cref="IProducerConsumerCollection{T}"/>.</returns>
81 /// A debugger view of the IProducerConsumerCollection that makes it simple to browse the
82 /// collection's contents at a point in time.
84 /// <typeparam name="T">The type of elements stored within.</typeparam>
85 internal sealed class SystemCollectionsConcurrent_ProducerConsumerCollectionDebugView<T>
87 private IProducerConsumerCollection<T> _collection; // The collection being viewed.
90 /// Constructs a new debugger view object for the provided collection object.
92 /// <param name="collection">A collection to browse in the debugger.</param>
93 public SystemCollectionsConcurrent_ProducerConsumerCollectionDebugView(IProducerConsumerCollection<T> collection)
95 if (collection == null)
97 throw new ArgumentNullException("collection");
100 _collection = collection;
104 /// Returns a snapshot of the underlying collection's elements.
106 [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
109 get { return _collection.ToArray(); }