Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / Core / System / Linq / Parallel / Channels / SynchronousChannel.cs / 1305376 / SynchronousChannel.cs
// ==++== // // Copyright (c) Microsoft Corporation. All rights reserved. // // ==--== // =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ // // SynchronousChannel.cs // //[....] // // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- using System.Collections.Generic; using System.Diagnostics.Contracts; namespace System.Linq.Parallel { ////// The simplest channel is one that has no synchronization. This is used for stop- /// and-go productions where we are guaranteed the consumer is not running /// concurrently. It just wraps a FIFO queue internally. /// /// Assumptions: /// Producers and consumers never try to enqueue/dequeue concurrently. /// ///internal sealed class SynchronousChannel { // We currently use the BCL FIFO queue internally, although any would do. private Queue m_queue; #if DEBUG // In debug builds, we keep track of when the producer is done (for asserts). private bool m_done; #endif //------------------------------------------------------------------------------------ // Instantiates a new queue. // internal SynchronousChannel() { } //----------------------------------------------------------------------------------- // Initializes the queue for this channel. // internal void Init() { m_queue = new Queue (); } //----------------------------------------------------------------------------------- // Enqueue a new item. // // Arguments: // item - the item to place into the queue // timeoutMilliseconds - synchronous channels never wait, so this is unused // // Assumptions: // The producer has not signaled that it's done yet. // // Return Value: // Synchronous channels always return true for this function. It can't timeout. // internal void Enqueue(T item) { Contract.Assert(m_queue != null); #if DEBUG Contract.Assert(!m_done, "trying to enqueue into the queue after production is done"); #endif m_queue.Enqueue(item); } //----------------------------------------------------------------------------------- // Dequeue the next item in the queue. // // Return Value: // The item removed from the queue. // // Assumptions: // The producer must be done producing. This queue is meant for synchronous // production/consumption, therefore it's unsafe for the consumer to try and // dequeue an item while a producer might be enqueueing one. // internal T Dequeue() { Contract.Assert(m_queue != null); #if DEBUG Contract.Assert(m_done, "trying to dequeue before production is done -- this is not safe"); #endif return m_queue.Dequeue(); } //------------------------------------------------------------------------------------ // Signals that a producer will no longer be enqueueing items. // internal void SetDone() { #if DEBUG // We only track this in DEBUG builds to aid in debugging. This ensures we // can assert dequeue-before-done and enqueue-after-done invariants above. m_done = true; #endif } //----------------------------------------------------------------------------------- // Copies the internal contents of this channel to an array. // internal void CopyTo(T[] array, int arrayIndex) { Contract.Assert(array != null); #if DEBUG Contract.Assert(m_done, "Can only copy from the channel after it's done being added to"); #endif m_queue.CopyTo(array, arrayIndex); } //------------------------------------------------------------------------------------ // Retrieves the current count of items in the queue. // internal int Count { get { Contract.Assert(m_queue != null); return m_queue.Count; } } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // ==++== // // Copyright (c) Microsoft Corporation. All rights reserved. // // ==--== // =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ // // SynchronousChannel.cs // // [....] // // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- using System.Collections.Generic; using System.Diagnostics.Contracts; namespace System.Linq.Parallel { ////// The simplest channel is one that has no synchronization. This is used for stop- /// and-go productions where we are guaranteed the consumer is not running /// concurrently. It just wraps a FIFO queue internally. /// /// Assumptions: /// Producers and consumers never try to enqueue/dequeue concurrently. /// ///internal sealed class SynchronousChannel { // We currently use the BCL FIFO queue internally, although any would do. private Queue m_queue; #if DEBUG // In debug builds, we keep track of when the producer is done (for asserts). private bool m_done; #endif //------------------------------------------------------------------------------------ // Instantiates a new queue. // internal SynchronousChannel() { } //----------------------------------------------------------------------------------- // Initializes the queue for this channel. // internal void Init() { m_queue = new Queue (); } //----------------------------------------------------------------------------------- // Enqueue a new item. // // Arguments: // item - the item to place into the queue // timeoutMilliseconds - synchronous channels never wait, so this is unused // // Assumptions: // The producer has not signaled that it's done yet. // // Return Value: // Synchronous channels always return true for this function. It can't timeout. // internal void Enqueue(T item) { Contract.Assert(m_queue != null); #if DEBUG Contract.Assert(!m_done, "trying to enqueue into the queue after production is done"); #endif m_queue.Enqueue(item); } //----------------------------------------------------------------------------------- // Dequeue the next item in the queue. // // Return Value: // The item removed from the queue. // // Assumptions: // The producer must be done producing. This queue is meant for synchronous // production/consumption, therefore it's unsafe for the consumer to try and // dequeue an item while a producer might be enqueueing one. // internal T Dequeue() { Contract.Assert(m_queue != null); #if DEBUG Contract.Assert(m_done, "trying to dequeue before production is done -- this is not safe"); #endif return m_queue.Dequeue(); } //------------------------------------------------------------------------------------ // Signals that a producer will no longer be enqueueing items. // internal void SetDone() { #if DEBUG // We only track this in DEBUG builds to aid in debugging. This ensures we // can assert dequeue-before-done and enqueue-after-done invariants above. m_done = true; #endif } //----------------------------------------------------------------------------------- // Copies the internal contents of this channel to an array. // internal void CopyTo(T[] array, int arrayIndex) { Contract.Assert(array != null); #if DEBUG Contract.Assert(m_done, "Can only copy from the channel after it's done being added to"); #endif m_queue.CopyTo(array, arrayIndex); } //------------------------------------------------------------------------------------ // Retrieves the current count of items in the queue. // internal int Count { get { Contract.Assert(m_queue != null); return m_queue.Count; } } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- TextEditorMouse.cs
- StrokeIntersection.cs
- LineGeometry.cs
- HtmlWindowCollection.cs
- RelatedImageListAttribute.cs
- TemplateLookupAction.cs
- ListCollectionView.cs
- Stroke.cs
- DataRecord.cs
- OleDbSchemaGuid.cs
- GorillaCodec.cs
- PropertyIDSet.cs
- ReaderWriterLockWrapper.cs
- HttpsHostedTransportConfiguration.cs
- DependentList.cs
- ReturnEventArgs.cs
- TextBoxBase.cs
- LayoutInformation.cs
- ScriptMethodAttribute.cs
- EraserBehavior.cs
- BitmapSizeOptions.cs
- HostedBindingBehavior.cs
- VBIdentifierName.cs
- OrderedEnumerableRowCollection.cs
- QuaternionKeyFrameCollection.cs
- SystemInformation.cs
- TriggerBase.cs
- WindowsImpersonationContext.cs
- AsymmetricSignatureFormatter.cs
- NetworkInformationException.cs
- DataContractSerializerOperationBehavior.cs
- HttpProfileBase.cs
- WindowsListViewItemStartMenu.cs
- OleDbWrapper.cs
- TextElementEnumerator.cs
- BasicHttpSecurityElement.cs
- TargetInvocationException.cs
- MDIControlStrip.cs
- SqlTransaction.cs
- DataGridViewElement.cs
- HtmlTableCell.cs
- ProtectedProviderSettings.cs
- PersistenceProvider.cs
- ProtectedConfiguration.cs
- EncryptedKey.cs
- PageRanges.cs
- ElementsClipboardData.cs
- StylusOverProperty.cs
- ServiceOperationInvoker.cs
- XmlSchemaComplexContentExtension.cs
- SignedInfo.cs
- BehaviorEditorPart.cs
- HtmlControlPersistable.cs
- PrimitiveType.cs
- DbConnectionPoolCounters.cs
- webeventbuffer.cs
- ListBox.cs
- NetworkInterface.cs
- _NestedMultipleAsyncResult.cs
- OrderedDictionaryStateHelper.cs
- DataServiceHost.cs
- ServiceParser.cs
- MultipartIdentifier.cs
- MenuItem.cs
- ThreadStaticAttribute.cs
- OperandQuery.cs
- DataGridViewSortCompareEventArgs.cs
- HttpServerVarsCollection.cs
- ConfigXmlAttribute.cs
- ReferentialConstraint.cs
- KeyTime.cs
- SqlMethodAttribute.cs
- IriParsingElement.cs
- ElasticEase.cs
- HierarchicalDataBoundControl.cs
- DataGridViewTextBoxEditingControl.cs
- WebPartDisplayModeCollection.cs
- OracleInternalConnection.cs
- AnnotationResourceChangedEventArgs.cs
- MonthCalendar.cs
- SqlNodeAnnotations.cs
- SharedHttpTransportManager.cs
- NaturalLanguageHyphenator.cs
- Int64Storage.cs
- UnsafeNativeMethods.cs
- ObjectHandle.cs
- OracleParameter.cs
- EditBehavior.cs
- UrlPath.cs
- MenuItem.cs
- OleDbReferenceCollection.cs
- TableDetailsRow.cs
- ResolvedKeyFrameEntry.cs
- ScriptingJsonSerializationSection.cs
- RequestTimeoutManager.cs
- AtlasWeb.Designer.cs
- DrawingGroup.cs
- AutomationPatternInfo.cs
- AuditLogLocation.cs
- LinqDataSourceContextData.cs