Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can a default(CancellationToken) have a corresponding CancellationTokenSource

When I create a default CancellationToken I can see in the debugger that the CancellationToken has a CancellationTokenSource associated with it which is stored in the private m_source field:

Not null

I am wondering how can that be as for structs the default keyword "will return each member of the struct initialized to zero or null depending on whether they are value or reference types" and CancellationTokenSource is a reference type.

CancellationToken does have 2 constructors that set this field however they are irrelevant as default(CancellationToken) doesn't call constructors and new CancellationToken() (which has the exact same behavior) doesn't call a constructor becuase structs can't have parameterless constructors (yet).

like image 532
i3arnon Avatar asked Mar 30 '15 19:03

i3arnon


People also ask

What is default for CancellationToken?

You can also use the C# default(CancellationToken) statement to create an empty cancellation token. Two empty cancellation tokens are always equal.

Can a CancellationTokenSource be reused?

CancellationTokenSource is quite a heavyweight object and its not normally cancelled; however it can't be pooled or reused because its registrations cannot be cleared.

What is a CancellationTokenSource?

A CancellationToken enables cooperative cancellation between threads, thread pool work items, or Task objects. You create a cancellation token by instantiating a CancellationTokenSource object, which manages cancellation tokens retrieved from its CancellationTokenSource.


1 Answers

default(CancellationToken) does create a CancellationToken where m_source is null. You can see that by getting the value of that private field using reflection:

Console.WriteLine(typeof (CancellationToken).
    GetField("m_source", BindingFlags.NonPublic | BindingFlags.Instance).
    GetValue(default(CancellationToken)) ?? "null");

Output:

null

You can also see that by pining only the relevant field in the debugger:

null

So, what happens?

The debugger, in order to display the contents of the CancellationToken, accesses its properties one by one. When the inner CancellationTokenSource is null the WaitHandle property creates and sets a default CancellationTokenSource before delegating to its WaitHandle property:

public WaitHandle WaitHandle
{
    get
    {
        if (m_source == null)
        {
             m_source = CancellationTokenSource.InternalGetStaticSource(false);
        }

        return m_source.WaitHandle;
    }
}

In conclusion, default(CancellationToken) and new CancellationToken create an empty struct where m_source is null but by looking at the struct in the debugger you are filling that field with a default CancellationTokenSource instance that can't be cancelled.

like image 93
i3arnon Avatar answered Oct 23 '22 20:10

i3arnon