Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -883,7 +883,7 @@ private sealed class Linked1CancellationTokenSource : CancellationTokenSource

internal Linked1CancellationTokenSource(CancellationToken token1)
{
_reg1 = token1.UnsafeRegister(LinkedNCancellationTokenSource.s_linkedTokenCancelDelegate, this);
_reg1 = token1.UnsafeRegister(LinkedNCancellationTokenSource.s_linkedTokenCancelDelegate, new WeakReference(this));
}

protected override void Dispose(bool disposing)
Expand All @@ -905,8 +905,8 @@ private sealed class Linked2CancellationTokenSource : CancellationTokenSource

internal Linked2CancellationTokenSource(CancellationToken token1, CancellationToken token2)
{
_reg1 = token1.UnsafeRegister(LinkedNCancellationTokenSource.s_linkedTokenCancelDelegate, this);
_reg2 = token2.UnsafeRegister(LinkedNCancellationTokenSource.s_linkedTokenCancelDelegate, this);
_reg1 = token1.UnsafeRegister(LinkedNCancellationTokenSource.s_linkedTokenCancelDelegate, new WeakReference(this));
_reg2 = token2.UnsafeRegister(LinkedNCancellationTokenSource.s_linkedTokenCancelDelegate, new WeakReference(this));
}

protected override void Dispose(bool disposing)
Expand All @@ -924,8 +924,11 @@ protected override void Dispose(bool disposing)

private sealed class LinkedNCancellationTokenSource : CancellationTokenSource
{
internal static readonly Action<object?> s_linkedTokenCancelDelegate = static s =>
internal static readonly Action<object?> s_linkedTokenCancelDelegate = static wr =>
{
Debug.Assert(wr is WeakReference, $"Expected {typeof(WeakReference)}, got {wr}");
object? s = ((WeakReference)wr).Target;
if (s is null) return;
Debug.Assert(s is CancellationTokenSource, $"Expected {typeof(CancellationTokenSource)}, got {s}");
((CancellationTokenSource)s).NotifyCancellation(throwOnFirstException: false); // skip ThrowIfDisposed() check in Cancel()
};
Expand All @@ -939,7 +942,7 @@ internal LinkedNCancellationTokenSource(CancellationToken[] tokens)
{
if (tokens[i].CanBeCanceled)
{
_linkingRegistrations[i] = tokens[i].UnsafeRegister(s_linkedTokenCancelDelegate, this);
_linkingRegistrations[i] = tokens[i].UnsafeRegister(s_linkedTokenCancelDelegate, new WeakReference(this));
}
// Empty slots in the array will be default(CancellationTokenRegistration), which are nops to Dispose.
// Based on usage patterns, such occurrences should also be rare, such that it's not worth resizing
Expand Down