// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
//
// [....]
////////////////////////////////////////////////////////////////////////////////
#pragma warning disable 0420 // turn off 'a reference to a volatile field will not be treated as volatile' during CAS.
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.Diagnostics.Contracts;
using System.Runtime;
using System.Runtime.CompilerServices;
using System.Security;
namespace System.Threading
{
///
/// Propagates notification that operations should be canceled.
///
///
///
/// A may be created directly in an unchangeable canceled or non-canceled state
/// using the CancellationToken's constructors. However, to have a CancellationToken that can change
/// from a non-canceled to a canceled state,
/// CancellationTokenSource must be used.
/// CancellationTokenSource exposes the associated CancellationToken that may be canceled by the source through its
/// Token property.
///
///
/// Once canceled, a token may not transition to a non-canceled state, and a token whose
/// is false will never change to one that can be canceled.
///
///
/// All members of this struct are thread-safe and may be used concurrently from multiple threads.
///
///
[ComVisible(false)]
[HostProtection(Synchronization = true, ExternalThreading = true)]
[DebuggerDisplay("IsCancellationRequested = {IsCancellationRequested}")]
public struct CancellationToken
{
// The backing TokenSource.
// if null, it implicitly represents the same thing as new CancellationToken(false).
// When required, it will be instantiated to reflect this.
private CancellationTokenSource m_source;
//!! warning. If more fields are added, the assumptions in CreateLinkedToken may no longer be valid
/* Properties */
///
/// Returns an empty CancellationToken value.
///
///
/// The value returned by this property will be non-cancelable by default.
///
public static CancellationToken None
{
#if !FEATURE_CORECLR
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
#endif
get { return default(CancellationToken); }
}
///
/// Gets whether cancellation has been requested for this token.
///
/// Whether cancellation has been requested for this token.
///
///
/// This property indicates whether cancellation has been requested for this token,
/// either through the token initially being construted in a canceled state, or through
/// calling Cancel
/// on the token's associated .
///
///
/// If this property is true, it only guarantees that cancellation has been requested.
/// It does not guarantee that every registered handler
/// has finished executing, nor that cancellation requests have finished propagating
/// to all registered handlers. Additional synchronization may be required,
/// particularly in situations where related objects are being canceled concurrently.
///
///
public bool IsCancellationRequested
{
#if !FEATURE_CORECLR
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
#endif
get
{
return m_source != null && m_source.IsCancellationRequested;
}
}
///
/// Gets whether this token is capable of being in the canceled state.
///
///
/// If CanBeCanceled returns false, it is guaranteed that the token will never transition
/// into a canceled state, meaning that will never
/// return true.
///
public bool CanBeCanceled
{
#if !FEATURE_CORECLR
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
#endif
get
{
return m_source != null && m_source.CanBeCanceled;
}
}
///
/// Gets a that is signaled when the token is canceled.
///
/// Accessing this property causes a WaitHandle
/// to be instantiated. It is preferable to only use this property when necessary, and to then
/// dispose the associated instance at the earliest opportunity (disposing
/// the source will dispose of this allocated handle). The handle should not be closed or disposed directly.
///
/// The associated CancellationTokenSource has been disposed.
public WaitHandle WaitHandle
{
get
{
if (m_source == null)
{
InitializeDefaultSource();
}
return m_source.WaitHandle;
}
}
// public CancellationToken()
// this constructor is implicit for structs
// -> this should behaves exactly as for new CancellationToken(false)
///
/// Internal constructor only a CancellationTokenSource should create a CancellationToken
///
#if !FEATURE_CORECLR
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
#endif
internal CancellationToken(CancellationTokenSource source)
{
m_source = source;
}
///
/// Initializes the CancellationToken.
///
///
/// The canceled state for the token.
///
///
/// Tokens created with this constructor will remain in the canceled state specified
/// by the parameter. If is false,
/// both and will be false.
/// If is true,
/// both and will be true.
///
public CancellationToken(bool canceled) :
this()
{
if(canceled)
m_source = CancellationTokenSource.InternalGetStaticSource(canceled);
}
/* Methods */
private readonly static Action