Reduce overhead of SemaphoreSlim.WaitAsync (dotnet/coreclr#24687)
authorStephen Toub <stoub@microsoft.com>
Tue, 21 May 2019 22:00:53 +0000 (18:00 -0400)
committerGitHub <noreply@github.com>
Tue, 21 May 2019 22:00:53 +0000 (18:00 -0400)
commit9f9e5f0dec4111f1b6d420acda43c322edfa9b5d
treef3df01b0145ece9af9c572dcac8b8c98e5d1b68d
parentd4fc0098013c7286558683d5ca3c06e8abfbdb17
Reduce overhead of SemaphoreSlim.WaitAsync (dotnet/coreclr#24687)

This affects the case where SemaphoreSlim.WaitAsync is invoked, the operation completes asynchronously because there's no count available in the semaphore, and where a timeout and/or a cancellation token is passed to WaitAsync.  In that case, we need to wait for the underlying wait task to complete, for cancellation to be requested, or for the timeout to elapse.

There's currently one code path that handles this.  This change improves that slightly by avoiding a defensive array copy.  However, we can do much better when we get a cancelable token but no timeout, in which case we can avoid operations like Task.Delay, can register with the original cancellation token rather than a new one (which means we avoid forcing a new CancellationTokenSource's lazily-allocated partitions into existence), etc.

Commit migrated from https://github.com/dotnet/coreclr/commit/26533de63976cb34eb1224c903128e398ca887d9
src/libraries/System.Private.CoreLib/src/System/Threading/SemaphoreSlim.cs