Skip to content

Commit d0bef9d

Browse files
committed
Removed expensive CAS in the best case
1 parent 4f11448 commit d0bef9d

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

src/DotNext.Threading/Threading/LinkedCancellationTokenSource.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,14 @@ private object RawToken
9292
// achieve deterministic behavior for CancellationOrigin and IsRootCause properties:
9393
// if cancellation is requested for this source by timeout, switch cancellationOrigin to not-null
9494
// value in getter to prevent concurrent overwrite by the linked token cancellation callback.
95-
object? tokenCopy;
95+
var tokenCopy = cancellationOrigin.Item1;
9696
if (CanInlineToken)
9797
{
98-
tokenCopy = IsCancellationRequested
99-
? Interlocked.CompareExchange(ref cancellationOrigin.Item1, this, comparand: null)
100-
: Volatile.Read(in cancellationOrigin.Item1);
101-
102-
tokenCopy ??= this;
98+
tokenCopy ??= IsCancellationRequested
99+
? Interlocked.CompareExchange(ref cancellationOrigin.Item1, this, comparand: null) ?? this
100+
: this;
103101
}
104-
else if ((tokenCopy = Volatile.Read(in cancellationOrigin.Item1)) is null)
102+
else if (tokenCopy is null)
105103
{
106104
object boxedToken = Token;
107105
tokenCopy = Interlocked.CompareExchange(ref cancellationOrigin.Item1, boxedToken, comparand: null) ?? boxedToken;

0 commit comments

Comments
 (0)