This repository was archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Move SynchronizationContext to shared partition #22389
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/System.Private.CoreLib/shared/System/Threading/SynchronizationContext.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using Thread = Internal.Runtime.Augments.RuntimeThread; | ||
|
||
namespace System.Threading | ||
{ | ||
public partial class SynchronizationContext | ||
{ | ||
private bool _requireWaitNotification; | ||
|
||
public SynchronizationContext() | ||
{ | ||
} | ||
|
||
#if !FEATURE_APPX && !ENABLE_WINRT | ||
public static SynchronizationContext Current => Thread.CurrentThread.SynchronizationContext; | ||
#endif | ||
|
||
protected void SetWaitNotificationRequired() => _requireWaitNotification = true; | ||
|
||
public bool IsWaitNotificationRequired() => _requireWaitNotification; | ||
|
||
public virtual void Send(SendOrPostCallback d, object state) => d(state); | ||
|
||
public virtual void Post(SendOrPostCallback d, object state) => ThreadPool.QueueUserWorkItem(s => s.d(s.state), (d, state), preferLocal: false); | ||
|
||
/// <summary> | ||
/// Optional override for subclasses, for responding to notification that operation is starting. | ||
/// </summary> | ||
public virtual void OperationStarted() | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Optional override for subclasses, for responding to notification that operation has completed. | ||
/// </summary> | ||
public virtual void OperationCompleted() | ||
{ | ||
} | ||
|
||
[CLSCompliant(false)] | ||
public virtual int Wait(IntPtr[] waitHandles, bool waitAll, int millisecondsTimeout) | ||
{ | ||
return WaitHelper(waitHandles, waitAll, millisecondsTimeout); | ||
} | ||
|
||
[CLSCompliant(false)] | ||
protected static int WaitHelper(IntPtr[] waitHandles, bool waitAll, int millisecondsTimeout) | ||
{ | ||
if (waitHandles == null) | ||
{ | ||
throw new ArgumentNullException(nameof(waitHandles)); | ||
} | ||
|
||
return WaitHandle.WaitMultipleIgnoringSyncContext(waitHandles, waitAll, millisecondsTimeout); | ||
} | ||
|
||
public static void SetSynchronizationContext(SynchronizationContext syncContext) => Thread.CurrentThread.SynchronizationContext = syncContext; | ||
|
||
public virtual SynchronizationContext CreateCopy() => new SynchronizationContext(); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/System.Private.CoreLib/src/System/Threading/SynchronizationContext.CoreCLR.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
namespace System.Threading | ||
{ | ||
public partial class SynchronizationContext | ||
{ | ||
private static int InvokeWaitMethodHelper(SynchronizationContext syncContext, IntPtr[] waitHandles, bool waitAll, int millisecondsTimeout) | ||
{ | ||
return syncContext.Wait(waitHandles, waitAll, millisecondsTimeout); | ||
} | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
src/System.Private.CoreLib/src/System/Threading/SynchronizationContext.Uap.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Runtime.InteropServices; | ||
using System.Runtime.CompilerServices; | ||
using System.Reflection; | ||
using System.Diagnostics; | ||
|
||
namespace System.Threading | ||
{ | ||
public partial class SynchronizationContext | ||
{ | ||
public static SynchronizationContext Current | ||
{ | ||
get | ||
{ | ||
SynchronizationContext context = Thread.CurrentThread.SynchronizationContext; | ||
|
||
if (context == null && ApplicationModel.IsUap) | ||
context = GetWinRTContext(); | ||
|
||
return context; | ||
} | ||
} | ||
|
||
private static SynchronizationContext GetWinRTContext() | ||
{ | ||
Debug.Assert(Environment.IsWinRTSupported); | ||
Debug.Assert(ApplicationModel.IsUap); | ||
|
||
// | ||
// We call into the VM to get the dispatcher. This is because: | ||
// | ||
// a) We cannot call the WinRT APIs directly from mscorlib, because we don't have the fancy projections here. | ||
// b) We cannot call into System.Runtime.WindowsRuntime here, because we don't want to load that assembly | ||
// into processes that don't need it (for performance reasons). | ||
// | ||
// So, we check the VM to see if the current thread has a dispatcher; if it does, we pass that along to | ||
// System.Runtime.WindowsRuntime to get a corresponding SynchronizationContext. | ||
// | ||
object dispatcher = GetWinRTDispatcherForCurrentThread(); | ||
if (dispatcher != null) | ||
return GetWinRTSynchronizationContext(dispatcher); | ||
|
||
return null; | ||
} | ||
|
||
private static Func<object, SynchronizationContext> s_createSynchronizationContextDelegate; | ||
|
||
private static SynchronizationContext GetWinRTSynchronizationContext(object dispatcher) | ||
{ | ||
// | ||
// Since we can't directly reference System.Runtime.WindowsRuntime from mscorlib, we have to get the factory via reflection. | ||
// It would be better if we could just implement WinRTSynchronizationContextFactory in mscorlib, but we can't, because | ||
// we can do very little with WinRT stuff in mscorlib. | ||
// | ||
Func<object, SynchronizationContext> createSynchronizationContextDelegate = s_createSynchronizationContextDelegate; | ||
if (createSynchronizationContextDelegate == null) | ||
{ | ||
Type factoryType = Type.GetType("System.Threading.WinRTSynchronizationContextFactory, System.Runtime.WindowsRuntime", throwOnError: true); | ||
|
||
// Create an instance delegate for the Create static method | ||
MethodInfo createMethodInfo = factoryType.GetMethod("Create", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public); | ||
createSynchronizationContextDelegate = (Func<object, SynchronizationContext>)Delegate.CreateDelegate(typeof(Func<object, SynchronizationContext>), createMethodInfo, /* throwOnBindFailure */ true); | ||
|
||
s_createSynchronizationContextDelegate = createSynchronizationContextDelegate; | ||
} | ||
|
||
return s_createSynchronizationContextDelegate(dispatcher); | ||
} | ||
|
||
[DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)] | ||
[return: MarshalAs(UnmanagedType.Interface)] | ||
private static extern object GetWinRTDispatcherForCurrentThread(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
#if CORERT
should not be necessary.Thread
is already in the same namespace and it will be resolved to the correct class. Theusing
will just become no-op on CoreCLR.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is for CoreRT which still does not have Thread (AFAIK)
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I meant to keep the
using
and drop the#if
around it. Sorry, I wasn't clear about it :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. I have removed it although there is a mixture of both used across the files
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It has to be there if the main class is not in the
System.Threading
namespace (eg. AsyncMethodBuilders). I agree it's confusing and I will be happy to address that once the strategy is decided upon (#22032).