Moved SafeWaitHandle and cancellationToken to shared#18662
Moved SafeWaitHandle and cancellationToken to shared#18662jkotas merged 9 commits intodotnet:masterfrom Anipik:safeWaitHandle
Conversation
| return Win32Native.CloseHandle(handle); | ||
| #if CORECLR && PLATFORM_UNIX | ||
| WaitSubsystem.DeleteHandle(handle); | ||
| return true |
| protected override bool ReleaseHandle() | ||
| { | ||
| return Win32Native.CloseHandle(handle); | ||
| #if CORECLR && PLATFORM_UNIX |
There was a problem hiding this comment.
Can we please avoid adding PLATFORM_UNIX and use !PLATFORM_WINDOWS instead? I'd like to keep things consistent across the codebase, and to me it's confusing when multiple constants are used for the same purpose.
| { | ||
| return Win32Native.CloseHandle(handle); | ||
| #if CORECLR && PLATFORM_UNIX | ||
| WaitSubsystem.DeleteHandle(handle); |
There was a problem hiding this comment.
Why is coreclr being changed to use WaitSubsystem.DeleteHandle here?
| /// <exception cref="T:System.ObjectDisposedException">The associated <see | ||
| /// cref="T:System.Threading.CancellationTokenSource">CancellationTokenSource</see> has been disposed.</exception> | ||
| #if CORECLR | ||
| public |
There was a problem hiding this comment.
Why is this being made public on coreclr?
|
|
||
| private bool _disposed; | ||
|
|
||
| // we track the running callback to assist ctr.Dispose() to wait for the target callback to complete. |
There was a problem hiding this comment.
CancellationTokenSource in coreclr is newer than the one in corert, and these fields were all reordered in coreclr... I'd prefer if we use the coreclr ordering / commenting / etc.
|
|
||
| // Lazily-initialize the handle. | ||
| var mre = new ManualResetEvent(false); | ||
| ManualResetEvent mre = new ManualResetEvent(false); |
| /// The <see cref="ExecutionContext"/> that was captured when each callback was registered | ||
| /// will be reestablished when the callback is invoked. | ||
| /// </para> | ||
| /// </para> |
There was a problem hiding this comment.
The indenting here was better before.
| { | ||
| Debug.Assert(IsCancellationRequested, "ExecuteCallbackHandlers should only be called after setting IsCancellationRequested->true"); | ||
|
|
||
| Debug.Assert(ThreadIDExecutingCallbacks != -1, "ThreadIDExecutingCallbacks should have been set."); |
There was a problem hiding this comment.
Are you sure this assert is valid? It looks wrong to me.
| Debug.Assert(setSuccessfully, "Should have been able to complete task"); | ||
| } | ||
|
|
||
| void IThreadPoolWorkItem.MarkAborted(ThreadAbortException tae) { /* nop */ } |
There was a problem hiding this comment.
This is still part of the interface and can't be removed. Does this compile?
There was a problem hiding this comment.
Yes it compiles perfectly fine. should I add it again ?
There was a problem hiding this comment.
should I add it again ?
Yes, it should be added back. I expect it's picking up the base type's implementation of the interface, and that's why it's still compiling successfully without this, but in doing so removing this line is potentially changing the behavior here (even though thread aborts are deprecated and in practice we shouldn't be hitting them outside of a debugging environment).
| } | ||
|
|
||
| override protected bool ReleaseHandle() | ||
| protected override bool ReleaseHandle() |
There was a problem hiding this comment.
This would be nice to split into .Windows.cs and .Unix.cs files.
| public Task WaitAsync() | ||
| { | ||
| return WaitAsync(Timeout.Infinite, default); | ||
| return WaitAsync(Timeout.Infinite, default(CancellationToken)); |
| // threading and decrements it back after that. It is used as flag for the release call to know if there are | ||
| // waiting threads in the monitor or not. | ||
| private int m_waitCount; | ||
| private volatile int m_waitCount; |
There was a problem hiding this comment.
Why does this need to be volatile?
There was a problem hiding this comment.
It will be accessed by the multiple threads as it keeps the record of the waiting threads present in the monitor
There was a problem hiding this comment.
It is always accessed under a lock as far as I can tell. Did you happen to trace down when was this volatile added or removed?
There was a problem hiding this comment.
yeah you are right. I missed the lock part. It was removed here #13766 due to the reason you mentioned
| // spin, contention, etc. The usual number of spin iterations that would otherwise be used here is increased to | ||
| // lessen that extra expense of doing a proper wait. | ||
| var spinner = new SpinWait(); | ||
| #if CORECLR |
There was a problem hiding this comment.
Is this ifdef really required? Looks like a performance fix that did not get ported to CoreRT.
There was a problem hiding this comment.
yeah you are right. 0799da2
is checking the m_currentCount == 0 in if condition rather than in while condition has some effect on performance ?
There was a problem hiding this comment.
m_currentCount == 0in if condition rather than in while condition has some effect on performance
That alone - no. However, there are other differences.
Just keep the CoreCLR impl here. It is the right one.
|
@jkotas is it ready to merge ? |
|
It looks fine to me, but it would be nice to wait till morning in case Stephen has more comments. |
| @@ -578,7 +565,7 @@ public Task<bool> WaitAsync(int millisecondsTimeout) | |||
| /// </exception> | |||
| /// <exception cref="T:System.ArgumentOutOfRangeException"> | |||
| /// <paramref name="timeout"/> is a negative number other than -1 milliseconds, which represents | |||
| /// an infinite time-out -or- timeout is greater than <see cref="System.int.MaxValue"/>. | |||
There was a problem hiding this comment.
Nit: The original version of this comment looked right. The new version does not make sense to me.
|
@stephentoub can I go ahead and merge it ? |
…18662) * Moving SafeWaitHandle to shared * Moved CancellationToken to shared * _ remove from variable names * Default change to Default(Token) and minor changes * Fixing coreclr unix build * moving semaphoreSlim to shared * splitting safeWaitHandle to .windows and .unix Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
…18662) * Moving SafeWaitHandle to shared * Moved CancellationToken to shared * _ remove from variable names * Default change to Default(Token) and minor changes * Fixing coreclr unix build * moving semaphoreSlim to shared * splitting safeWaitHandle to .windows and .unix Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
…18662) * Moving SafeWaitHandle to shared * Moved CancellationToken to shared * _ remove from variable names * Default change to Default(Token) and minor changes * Fixing coreclr unix build * moving semaphoreSlim to shared * splitting safeWaitHandle to .windows and .unix Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
…18662) * Moving SafeWaitHandle to shared * Moved CancellationToken to shared * _ remove from variable names * Default change to Default(Token) and minor changes * Fixing coreclr unix build * moving semaphoreSlim to shared * splitting safeWaitHandle to .windows and .unix Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
|
Sorry, just saw the question to me... LGTM. |
|
np :) |
…18662) * Moving SafeWaitHandle to shared * Moved CancellationToken to shared * _ remove from variable names * Default change to Default(Token) and minor changes * Fixing coreclr unix build * moving semaphoreSlim to shared * splitting safeWaitHandle to .windows and .unix Commit migrated from dotnet/coreclr@11c9d85
Corert PR :- dotnet/corert#6020