Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / Core / System / Linq / Parallel / Scheduling / CancellationState.cs / 1305376 / CancellationState.cs
// ==++== // // Copyright (c) Microsoft Corporation. All rights reserved. // // ==--== // =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ // // CancellationState.cs // //[....] // // A bag of cancellation-related items that are passed around as a group. // // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- using System; using System.Collections.Generic; using System.Threading; namespace System.Linq.Parallel { internal class CancellationState { // a cancellation signal that can be set internally to prompt early query termination. internal CancellationTokenSource InternalCancellationTokenSource; // the external cancellationToken that the user sets to ask for the query to terminate early. // this has to be tracked explicitly so that an OCE(externalToken) can be thrown as the query // execution unravels. internal CancellationToken ExternalCancellationToken; // A combined token Source for internal/external cancellation, defining the total cancellation state. internal CancellationTokenSource MergedCancellationTokenSource; // A combined token for internal/external cancellation, defining the total cancellation state. internal CancellationToken MergedCancellationToken { get { if( MergedCancellationTokenSource != null) return MergedCancellationTokenSource.Token; else return new CancellationToken(false); } } // A shared boolean flag to track whether a query-opening-enumerator dispose has occured. internal SharedTopLevelDisposedFlag; internal CancellationState(CancellationToken externalCancellationToken) { ExternalCancellationToken = externalCancellationToken; TopLevelDisposedFlag = new Shared (false); //it would always be initialised to false, so no harm doing it here and avoid #if around constructors. } /// /// Poll frequency (number of loops per cancellation check) for situations where per-1-loop testing is too high an overhead. /// internal const int POLL_INTERVAL = 63; //must be of the form (2^n)-1. // The two main situations requiring POLL_INTERVAL are: // 1. inner loops of sorting/merging operations // 2. tight loops that perform very little work per MoveNext call. // Testing has shown both situations have similar requirements and can share the same constant for polling interval. // // Because the poll checks are per-N loops, if there are delays in user code, they may affect cancellation timeliness. // Guidance is that all user-delegates should perform cancellation checks at least every 1ms. // // Inner loop code should poll once per n loop, typically via: // if ((i++ & CancellationState.POLL_INTERVAL) == 0) // CancellationState.ThrowIfCanceled(m_cancellationToken); // (Note, this only behaves as expected if FREQ is of the form (2^n)-1 ////// Throws an OCE if the merged token has been canceled. /// /// A token to check for cancelation. internal static void ThrowIfCanceled(CancellationToken token) { if (token.IsCancellationRequested) throw new OperationCanceledException(token); } // Test if external cancellation was requested and occured, and if so throw a standardize OCE with standardized message internal static void ThrowWithStandardMessageIfCanceled(CancellationToken externalCancellationToken) { if (externalCancellationToken.IsCancellationRequested) { string oceMessage = SR.GetString(SR.PLINQ_ExternalCancellationRequested); throw new OperationCanceledException(oceMessage, externalCancellationToken); } } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // ==++== // // Copyright (c) Microsoft Corporation. All rights reserved. // // ==--== // =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ // // CancellationState.cs // //[....] // // A bag of cancellation-related items that are passed around as a group. // // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- using System; using System.Collections.Generic; using System.Threading; namespace System.Linq.Parallel { internal class CancellationState { // a cancellation signal that can be set internally to prompt early query termination. internal CancellationTokenSource InternalCancellationTokenSource; // the external cancellationToken that the user sets to ask for the query to terminate early. // this has to be tracked explicitly so that an OCE(externalToken) can be thrown as the query // execution unravels. internal CancellationToken ExternalCancellationToken; // A combined token Source for internal/external cancellation, defining the total cancellation state. internal CancellationTokenSource MergedCancellationTokenSource; // A combined token for internal/external cancellation, defining the total cancellation state. internal CancellationToken MergedCancellationToken { get { if( MergedCancellationTokenSource != null) return MergedCancellationTokenSource.Token; else return new CancellationToken(false); } } // A shared boolean flag to track whether a query-opening-enumerator dispose has occured. internal SharedTopLevelDisposedFlag; internal CancellationState(CancellationToken externalCancellationToken) { ExternalCancellationToken = externalCancellationToken; TopLevelDisposedFlag = new Shared (false); //it would always be initialised to false, so no harm doing it here and avoid #if around constructors. } /// /// Poll frequency (number of loops per cancellation check) for situations where per-1-loop testing is too high an overhead. /// internal const int POLL_INTERVAL = 63; //must be of the form (2^n)-1. // The two main situations requiring POLL_INTERVAL are: // 1. inner loops of sorting/merging operations // 2. tight loops that perform very little work per MoveNext call. // Testing has shown both situations have similar requirements and can share the same constant for polling interval. // // Because the poll checks are per-N loops, if there are delays in user code, they may affect cancellation timeliness. // Guidance is that all user-delegates should perform cancellation checks at least every 1ms. // // Inner loop code should poll once per n loop, typically via: // if ((i++ & CancellationState.POLL_INTERVAL) == 0) // CancellationState.ThrowIfCanceled(m_cancellationToken); // (Note, this only behaves as expected if FREQ is of the form (2^n)-1 ////// Throws an OCE if the merged token has been canceled. /// /// A token to check for cancelation. internal static void ThrowIfCanceled(CancellationToken token) { if (token.IsCancellationRequested) throw new OperationCanceledException(token); } // Test if external cancellation was requested and occured, and if so throw a standardize OCE with standardized message internal static void ThrowWithStandardMessageIfCanceled(CancellationToken externalCancellationToken) { if (externalCancellationToken.IsCancellationRequested) { string oceMessage = SR.GetString(SR.PLINQ_ExternalCancellationRequested); throw new OperationCanceledException(oceMessage, externalCancellationToken); } } } } // 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
- TrustExchangeException.cs
- LightweightCodeGenerator.cs
- TextFormatter.cs
- WindowAutomationPeer.cs
- CaseInsensitiveOrdinalStringComparer.cs
- UIElementPropertyUndoUnit.cs
- DesignerAutoFormat.cs
- MouseEvent.cs
- ApplicationSecurityInfo.cs
- DataPagerFieldCommandEventArgs.cs
- ExtendedPropertyDescriptor.cs
- PropertyGridView.cs
- DesignerAutoFormat.cs
- WmlObjectListAdapter.cs
- XmlSchemaChoice.cs
- AxImporter.cs
- shaperfactory.cs
- BaseAddressElement.cs
- DataGridViewRowPostPaintEventArgs.cs
- ConsoleCancelEventArgs.cs
- ErrorEventArgs.cs
- EntityConnection.cs
- Timeline.cs
- DependencyPropertyDescriptor.cs
- DataGridViewTopRowAccessibleObject.cs
- FunctionParameter.cs
- WebPartDeleteVerb.cs
- WebPartDisplayModeCollection.cs
- SplitterEvent.cs
- InvalidAsynchronousStateException.cs
- ExpandSegment.cs
- DescendantQuery.cs
- DetailsViewRowCollection.cs
- OpCodes.cs
- ScrollEvent.cs
- DbConnectionStringCommon.cs
- NamespaceMapping.cs
- EpmContentDeSerializer.cs
- XmlAnyElementAttributes.cs
- LocalizableResourceBuilder.cs
- ResponseStream.cs
- ResourceExpressionBuilder.cs
- GeometryGroup.cs
- DbConnectionStringCommon.cs
- InternalConfigHost.cs
- ExpressionBinding.cs
- WebControlParameterProxy.cs
- SmtpCommands.cs
- ISessionStateStore.cs
- OdbcErrorCollection.cs
- Dictionary.cs
- ExpressionBuilderContext.cs
- AttributeUsageAttribute.cs
- TaskHelper.cs
- FormatSelectingMessageInspector.cs
- XamlDesignerSerializationManager.cs
- DataServicePagingProviderWrapper.cs
- CodePrimitiveExpression.cs
- DateTimeOffset.cs
- IssuanceLicense.cs
- CreateUserErrorEventArgs.cs
- LocalizableAttribute.cs
- SemaphoreSlim.cs
- SchemaMapping.cs
- EdmFunction.cs
- DataGridViewCellParsingEventArgs.cs
- HandleCollector.cs
- ConstructorBuilder.cs
- ContextMenu.cs
- RTTypeWrapper.cs
- InitializerFacet.cs
- HttpCachePolicy.cs
- SqlNodeAnnotations.cs
- RadioButtonStandardAdapter.cs
- WinInetCache.cs
- TouchesCapturedWithinProperty.cs
- DataGridViewMethods.cs
- ListSortDescriptionCollection.cs
- XmlSchemaSimpleContentRestriction.cs
- MetadataItemSerializer.cs
- PropertyIDSet.cs
- PngBitmapEncoder.cs
- HtmlElementErrorEventArgs.cs
- CommonRemoteMemoryBlock.cs
- SqlDataSourceStatusEventArgs.cs
- AppDomainAttributes.cs
- RichTextBoxDesigner.cs
- Cell.cs
- TailCallAnalyzer.cs
- OleAutBinder.cs
- VersionPair.cs
- MarkupExtensionParser.cs
- DispatcherTimer.cs
- SQLInt64.cs
- XmlSchemaImporter.cs
- IChannel.cs
- SeekStoryboard.cs
- BinHexDecoder.cs
- CodeTypeParameterCollection.cs
- TextOnlyOutput.cs