diff options
author | Elliot Prior <1709938+Quogu@users.noreply.github.com> | 2019-01-25 13:59:30 +0000 |
---|---|---|
committer | Stephen Toub <stoub@microsoft.com> | 2019-01-25 08:59:30 -0500 |
commit | dcdfeea0b85b09e0f066fdfd05f5fef289977ab9 (patch) | |
tree | ae2e3910537fe64e3123a8bb028b6677ed7fffda | |
parent | cdd6e3315ff125aabc34378d5547f68df89c05c2 (diff) | |
download | coreclr-dcdfeea0b85b09e0f066fdfd05f5fef289977ab9.tar.gz coreclr-dcdfeea0b85b09e0f066fdfd05f5fef289977ab9.tar.bz2 coreclr-dcdfeea0b85b09e0f066fdfd05f5fef289977ab9.zip |
Cancel CTS immediately if delay is zero. (#18098)
The intended behaviour on specifying a delay of zero is that the CancellationTokenSource is cancelled immediately - before this change, it would depend on the scheduling of the timer callback which could lead to seeing a non-cancelled token. Now, it marks itself as cancelled immediately, without invoking any callbacks (as this method is called only from within the constructor of the object, before any callbacks could have been registered.
-rw-r--r-- | src/System.Private.CoreLib/shared/System/Threading/CancellationTokenSource.cs | 24 |
1 files changed, 17 insertions, 7 deletions
diff --git a/src/System.Private.CoreLib/shared/System/Threading/CancellationTokenSource.cs b/src/System.Private.CoreLib/shared/System/Threading/CancellationTokenSource.cs index 937bbaf456..8c4dd01c3d 100644 --- a/src/System.Private.CoreLib/shared/System/Threading/CancellationTokenSource.cs +++ b/src/System.Private.CoreLib/shared/System/Threading/CancellationTokenSource.cs @@ -202,16 +202,26 @@ namespace System.Threading InitializeWithTimer(millisecondsDelay); } - /// <summary>Common initialization logic when constructing a CTS with a delay parameter</summary> + /// <summary> + /// Common initialization logic when constructing a CTS with a delay parameter. + /// A zero delay will result in immediate cancellation. + /// </summary> private void InitializeWithTimer(int millisecondsDelay) { - _state = NotCanceledState; - _timer = new TimerQueueTimer(s_timerCallback, this, (uint)millisecondsDelay, Timeout.UnsignedInfinite, flowExecutionContext: false); + if (millisecondsDelay == 0) + { + _state = NotifyingCompleteState; + } + else + { + _state = NotCanceledState; + _timer = new TimerQueueTimer(s_timerCallback, this, (uint)millisecondsDelay, Timeout.UnsignedInfinite, flowExecutionContext: false); - // The timer roots this CTS instance while it's scheduled. That is by design, so - // that code like: - // CancellationToken ct = new CancellationTokenSource(timeout).Token; - // will successfully cancel the token after the timeout. + // The timer roots this CTS instance while it's scheduled. That is by design, so + // that code like: + // CancellationToken ct = new CancellationTokenSource(timeout).Token; + // will successfully cancel the token after the timeout. + } } /// <summary>Communicates a request for cancellation.</summary> |