3 // Copyright (c) Microsoft Corporation. All rights reserved.
6 // <OWNER>Microsoft</OWNER>
7 /*============================================================
9 ** Class: SynchronizationContext
12 ** Purpose: Capture synchronization semantics for asynchronous callbacks
15 ===========================================================*/
17 namespace System.Threading
20 using Microsoft.Win32.SafeHandles;
22 using System.Security.Permissions;
23 using System.Runtime.InteropServices;
24 using System.Runtime.CompilerServices;
25 #if FEATURE_CORRUPTING_EXCEPTIONS
26 using System.Runtime.ExceptionServices;
27 #endif // FEATURE_CORRUPTING_EXCEPTIONS
29 using System.Runtime.Versioning;
30 using System.Runtime.ConstrainedExecution;
31 using System.Reflection;
32 using System.Security;
33 using System.Diagnostics.Contracts;
34 using System.Diagnostics.CodeAnalysis;
37 #if FEATURE_SYNCHRONIZATIONCONTEXT_WAIT
39 enum SynchronizationContextProperties
42 RequireWaitNotification = 0x1
46 #if FEATURE_COMINTEROP && FEATURE_APPX
48 // This is implemented in System.Runtime.WindowsRuntime, allowing us to ask that assembly for a WinRT-specific SyncCtx.
49 // I'd like this to be an interface, or at least an abstract class - but neither seems to play nice with FriendAccessAllowed.
53 internal class WinRTSynchronizationContextFactoryBase
56 public virtual SynchronizationContext Create(object coreDispatcher) {return null;}
58 #endif //FEATURE_COMINTEROP
61 [SecurityPermissionAttribute(SecurityAction.InheritanceDemand, Flags =SecurityPermissionFlag.ControlPolicy|SecurityPermissionFlag.ControlEvidence)]
63 public class SynchronizationContext
65 #if FEATURE_SYNCHRONIZATIONCONTEXT_WAIT
66 SynchronizationContextProperties _props = SynchronizationContextProperties.None;
69 public SynchronizationContext()
73 #if FEATURE_SYNCHRONIZATIONCONTEXT_WAIT
75 // helper delegate to statically bind to Wait method
76 private delegate int WaitDelegate(IntPtr[] waitHandles, bool waitAll, int millisecondsTimeout);
78 static Type s_cachedPreparedType1;
79 static Type s_cachedPreparedType2;
80 static Type s_cachedPreparedType3;
81 static Type s_cachedPreparedType4;
82 static Type s_cachedPreparedType5;
84 // protected so that only the derived sync context class can enable these flags
85 [System.Security.SecuritySafeCritical] // auto-generated
86 [SuppressMessage("Microsoft.Concurrency", "CA8001", Justification = "We never dereference s_cachedPreparedType*, so ordering is unimportant")]
87 protected void SetWaitNotificationRequired()
90 // Prepare the method so that it can be called in a reliable fashion when a wait is needed.
91 // This will obviously only make the Wait reliable if the Wait method is itself reliable. The only thing
92 // preparing the method here does is to ensure there is no failure point before the method execution begins.
94 // Preparing the method in this way is quite expensive, but only needs to be done once per type, per AppDomain.
95 // So we keep track of a few types we've already prepared in this AD. It is uncommon to have more than
96 // a few SynchronizationContext implementations, so we only cache the first five we encounter; this lets
97 // our cache be much faster than a more general cache might be. This is important, because this
98 // is a *very* hot code path for many WPF and Microsoft apps.
100 Type type = this.GetType();
101 if (s_cachedPreparedType1 != type &&
102 s_cachedPreparedType2 != type &&
103 s_cachedPreparedType3 != type &&
104 s_cachedPreparedType4 != type &&
105 s_cachedPreparedType5 != type)
107 RuntimeHelpers.PrepareDelegate(new WaitDelegate(this.Wait));
109 if (s_cachedPreparedType1 == null) s_cachedPreparedType1 = type;
110 else if (s_cachedPreparedType2 == null) s_cachedPreparedType2 = type;
111 else if (s_cachedPreparedType3 == null) s_cachedPreparedType3 = type;
112 else if (s_cachedPreparedType4 == null) s_cachedPreparedType4 = type;
113 else if (s_cachedPreparedType5 == null) s_cachedPreparedType5 = type;
116 _props |= SynchronizationContextProperties.RequireWaitNotification;
119 public bool IsWaitNotificationRequired()
121 return ((_props & SynchronizationContextProperties.RequireWaitNotification) != 0);
126 public virtual void Send(SendOrPostCallback d, Object state)
131 public virtual void Post(SendOrPostCallback d, Object state)
133 ThreadPool.QueueUserWorkItem(new WaitCallback(d), state);
138 /// Optional override for subclasses, for responding to notification that operation is starting.
140 public virtual void OperationStarted()
145 /// Optional override for subclasses, for responding to notification that operation has completed.
147 public virtual void OperationCompleted()
151 #if FEATURE_SYNCHRONIZATIONCONTEXT_WAIT
152 // Method called when the CLR does a wait operation
153 [System.Security.SecurityCritical] // auto-generated_required
154 [CLSCompliant(false)]
156 public virtual int Wait(IntPtr[] waitHandles, bool waitAll, int millisecondsTimeout)
158 if (waitHandles == null)
160 throw new ArgumentNullException("waitHandles");
162 Contract.EndContractBlock();
163 return WaitHelper(waitHandles, waitAll, millisecondsTimeout);
166 // Static helper to which the above method can delegate to in order to get the default
168 [System.Security.SecurityCritical] // auto-generated_required
169 [CLSCompliant(false)]
171 [ResourceExposure(ResourceScope.None)]
172 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
174 protected static int WaitHelper(IntPtr[] waitHandles, bool waitAll, int millisecondsTimeout)
176 throw new NotImplementedException ();
179 [MethodImplAttribute(MethodImplOptions.InternalCall)]
180 protected static extern int WaitHelper(IntPtr[] waitHandles, bool waitAll, int millisecondsTimeout);
186 private static SynchronizationContext s_threadStaticContext;
189 // NetCF had a bug where SynchronizationContext.SetThreadStaticContext would set the SyncContext for every thread in the process.
190 // This was because they stored the value in a regular static field (NetCF has no support for ThreadStatic fields). This was fixed in
191 // Mango, but some apps built against pre-Mango WP7 do depend on the broken behavior. So for those apps we need an AppDomain-wide static
192 // to hold whatever context was last set on any thread.
194 private static SynchronizationContext s_appDomainStaticContext;
196 [System.Security.SecurityCritical]
197 public static void SetSynchronizationContext(SynchronizationContext syncContext)
199 s_threadStaticContext = syncContext;
202 [System.Security.SecurityCritical]
203 public static void SetThreadStaticContext(SynchronizationContext syncContext)
206 // If this is a pre-Mango Windows Phone app, we need to set the SC for *all* threads to match the old NetCF behavior.
208 if (CompatibilitySwitches.IsAppEarlierThanWindowsPhoneMango)
209 s_appDomainStaticContext = syncContext;
211 s_threadStaticContext = syncContext;
214 public static SynchronizationContext Current
218 SynchronizationContext context = null;
220 if (CompatibilitySwitches.IsAppEarlierThanWindowsPhoneMango)
221 context = s_appDomainStaticContext;
223 context = s_threadStaticContext;
226 if (context == null && Environment.IsWinRTSupported)
227 context = GetWinRTContext();
234 // Get the last SynchronizationContext that was set explicitly (not flowed via ExecutionContext.Capture/Run)
235 internal static SynchronizationContext CurrentNoFlow
237 [FriendAccessAllowed]
240 return Current; // SC never flows
244 #else //FEATURE_CORECLR
246 // set SynchronizationContext on the current thread
247 [System.Security.SecurityCritical] // auto-generated_required
248 public static void SetSynchronizationContext(SynchronizationContext syncContext)
250 ExecutionContext ec = Thread.CurrentThread.GetMutableExecutionContext();
251 ec.SynchronizationContext = syncContext;
252 ec.SynchronizationContextNoFlow = syncContext;
256 [Obsolete("The method is not supported and will be removed")]
257 public static void SetThreadStaticContext(SynchronizationContext syncContext)
259 throw new NotSupportedException ();
263 // Get the current SynchronizationContext on the current thread
264 public static SynchronizationContext Current
268 return Thread.CurrentThread.GetExecutionContextReader().SynchronizationContext ?? GetThreadLocalContext();
272 // Get the last SynchronizationContext that was set explicitly (not flowed via ExecutionContext.Capture/Run)
273 internal static SynchronizationContext CurrentNoFlow
275 [FriendAccessAllowed]
278 return Thread.CurrentThread.GetExecutionContextReader().SynchronizationContextNoFlow ?? GetThreadLocalContext();
282 private static SynchronizationContext GetThreadLocalContext()
284 SynchronizationContext context = null;
287 if (context == null && Environment.IsWinRTSupported)
288 context = GetWinRTContext();
293 context = AndroidPlatform.GetDefaultSyncContext ();
299 #endif //FEATURE_CORECLR
302 [SecuritySafeCritical]
303 private static SynchronizationContext GetWinRTContext()
305 Contract.Assert(Environment.IsWinRTSupported);
307 // Temporary hack to avoid loading a bunch of DLLs in every managed process.
308 // This disables this feature for non-AppX processes that happen to use CoreWindow/CoreDispatcher,
309 // which is not what we want.
310 if (!AppDomain.IsAppXModel())
314 // We call into the VM to get the dispatcher. This is because:
316 // a) We cannot call the WinRT APIs directly from mscorlib, because we don't have the fancy projections here.
317 // b) We cannot call into System.Runtime.WindowsRuntime here, because we don't want to load that assembly
318 // into processes that don't need it (for performance reasons).
320 // So, we check the VM to see if the current thread has a dispatcher; if it does, we pass that along to
321 // System.Runtime.WindowsRuntime to get a corresponding SynchronizationContext.
323 object dispatcher = GetWinRTDispatcherForCurrentThread();
324 if (dispatcher != null)
325 return GetWinRTSynchronizationContextFactory().Create(dispatcher);
331 static WinRTSynchronizationContextFactoryBase s_winRTContextFactory;
334 private static WinRTSynchronizationContextFactoryBase GetWinRTSynchronizationContextFactory()
337 // Since we can't directly reference System.Runtime.WindowsRuntime from mscorlib, we have to get the factory via reflection.
338 // It would be better if we could just implement WinRTSynchronizationContextFactory in mscorlib, but we can't, because
339 // we can do very little with WinRT stuff in mscorlib.
341 WinRTSynchronizationContextFactoryBase factory = s_winRTContextFactory;
344 Type factoryType = Type.GetType("System.Threading.WinRTSynchronizationContextFactory, " + AssemblyRef.SystemRuntimeWindowsRuntime, true);
345 s_winRTContextFactory = factory = (WinRTSynchronizationContextFactoryBase)Activator.CreateInstance(factoryType, true);
350 [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
352 [ResourceExposure(ResourceScope.None)]
353 [SuppressUnmanagedCodeSecurity]
354 [return: MarshalAs(UnmanagedType.Interface)]
355 private static extern object GetWinRTDispatcherForCurrentThread();
356 #endif //FEATURE_APPX
359 // helper to Clone this SynchronizationContext,
360 public virtual SynchronizationContext CreateCopy()
362 // the CLR dummy has an empty clone function - no member data
363 return new SynchronizationContext();
366 #if FEATURE_SYNCHRONIZATIONCONTEXT_WAIT
367 [System.Security.SecurityCritical] // auto-generated
368 private static int InvokeWaitMethodHelper(SynchronizationContext syncContext, IntPtr[] waitHandles, bool waitAll, int millisecondsTimeout)
370 return syncContext.Wait(waitHandles, waitAll, millisecondsTimeout);