Skip to content

Commit e7af3bd

Browse files
marek-safardotnet-bot
authored andcommitted
Move SynchronizationContext to shared partition (dotnet/coreclr#22389)
* Move SynchronizationContext to shared partition * Move WaitHelperNative to WaitHandle Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
1 parent e62720d commit e7af3bd

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

src/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,7 @@
788788
<Compile Include="$(MSBuildThisFileDirectory)System\Threading\SendOrPostCallback.cs" />
789789
<Compile Include="$(MSBuildThisFileDirectory)System\Threading\SpinLock.cs" />
790790
<Compile Include="$(MSBuildThisFileDirectory)System\Threading\SpinWait.cs" />
791+
<Compile Include="$(MSBuildThisFileDirectory)System\Threading\SynchronizationContext.cs" />
791792
<Compile Include="$(MSBuildThisFileDirectory)System\Threading\SynchronizationLockException.cs" />
792793
<Compile Include="$(MSBuildThisFileDirectory)System\Threading\ThreadLocal.cs" />
793794
<Compile Include="$(MSBuildThisFileDirectory)System\Threading\Tasks\AsyncCausalityTracerConstants.cs" />
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using Thread = Internal.Runtime.Augments.RuntimeThread;
6+
7+
namespace System.Threading
8+
{
9+
public partial class SynchronizationContext
10+
{
11+
private bool _requireWaitNotification;
12+
13+
public SynchronizationContext()
14+
{
15+
}
16+
17+
#if !FEATURE_APPX && !ENABLE_WINRT
18+
public static SynchronizationContext Current => Thread.CurrentThread.SynchronizationContext;
19+
#endif
20+
21+
protected void SetWaitNotificationRequired() => _requireWaitNotification = true;
22+
23+
public bool IsWaitNotificationRequired() => _requireWaitNotification;
24+
25+
public virtual void Send(SendOrPostCallback d, object state) => d(state);
26+
27+
public virtual void Post(SendOrPostCallback d, object state) => ThreadPool.QueueUserWorkItem(s => s.d(s.state), (d, state), preferLocal: false);
28+
29+
/// <summary>
30+
/// Optional override for subclasses, for responding to notification that operation is starting.
31+
/// </summary>
32+
public virtual void OperationStarted()
33+
{
34+
}
35+
36+
/// <summary>
37+
/// Optional override for subclasses, for responding to notification that operation has completed.
38+
/// </summary>
39+
public virtual void OperationCompleted()
40+
{
41+
}
42+
43+
[CLSCompliant(false)]
44+
public virtual int Wait(IntPtr[] waitHandles, bool waitAll, int millisecondsTimeout)
45+
{
46+
return WaitHelper(waitHandles, waitAll, millisecondsTimeout);
47+
}
48+
49+
[CLSCompliant(false)]
50+
protected static int WaitHelper(IntPtr[] waitHandles, bool waitAll, int millisecondsTimeout)
51+
{
52+
if (waitHandles == null)
53+
{
54+
throw new ArgumentNullException(nameof(waitHandles));
55+
}
56+
57+
return WaitHandle.WaitMultipleIgnoringSyncContext(waitHandles, waitAll, millisecondsTimeout);
58+
}
59+
60+
public static void SetSynchronizationContext(SynchronizationContext syncContext) => Thread.CurrentThread.SynchronizationContext = syncContext;
61+
62+
public virtual SynchronizationContext CreateCopy() => new SynchronizationContext();
63+
}
64+
}

0 commit comments

Comments
 (0)