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 /*============================================================
6 **
7 **
8 **
9 ** Purpose: Capture synchronization semantics for asynchronous callbacks
10 **
11 **
12 ===========================================================*/
13 
14 using Internal.Runtime.Augments;
15 
16 namespace System.Threading
17 {
18     [Flags]
19     internal enum SynchronizationContextProperties
20     {
21         None = 0,
22         RequireWaitNotification = 0x1
23     };
24 
25     public partial class SynchronizationContext
26     {
27         private SynchronizationContextProperties _props = SynchronizationContextProperties.None;
28 
SynchronizationContext()29         public SynchronizationContext()
30         {
31         }
32 
33 #if PLATFORM_WINDOWS
34         // protected so that only the derived sync context class can enable these flags
SetWaitNotificationRequired()35         protected void SetWaitNotificationRequired()
36         {
37             _props |= SynchronizationContextProperties.RequireWaitNotification;
38         }
39 #endif
40 
IsWaitNotificationRequired()41         public bool IsWaitNotificationRequired()
42         {
43             return ((_props & SynchronizationContextProperties.RequireWaitNotification) != 0);
44         }
45 
Send(SendOrPostCallback d, Object state)46         public virtual void Send(SendOrPostCallback d, Object state)
47         {
48             d(state);
49         }
50 
Post(SendOrPostCallback d, Object state)51         public virtual void Post(SendOrPostCallback d, Object state)
52         {
53             ThreadPool.QueueUserWorkItem(new WaitCallback(d), state);
54         }
55 
56         /// <summary>
57         ///     Optional override for subclasses, for responding to notification that operation is starting.
58         /// </summary>
OperationStarted()59         public virtual void OperationStarted()
60         {
61         }
62 
63         /// <summary>
64         ///     Optional override for subclasses, for responding to notification that operation has completed.
65         /// </summary>
OperationCompleted()66         public virtual void OperationCompleted()
67         {
68         }
69 
70 #if PLATFORM_WINDOWS
71         // Method called when the CLR does a wait operation
72         [CLSCompliant(false)]
Wait(IntPtr[] waitHandles, bool waitAll, int millisecondsTimeout)73         public virtual int Wait(IntPtr[] waitHandles, bool waitAll, int millisecondsTimeout)
74         {
75             return WaitHelper(waitHandles, waitAll, millisecondsTimeout);
76         }
77 
78         // Method that can be called by Wait overrides
79         [CLSCompliant(false)]
WaitHelper(IntPtr[] waitHandles, bool waitAll, int millisecondsTimeout)80         protected static int WaitHelper(IntPtr[] waitHandles, bool waitAll, int millisecondsTimeout)
81         {
82             if (waitHandles == null)
83             {
84                 throw new ArgumentNullException(nameof(waitHandles));
85             }
86 
87             return WaitHandle.WaitForMultipleObjectsIgnoringSyncContext(waitHandles, waitHandles.Length, waitAll, millisecondsTimeout);
88         }
89 #endif
90 
91         // Set the SynchronizationContext on the current thread
SetSynchronizationContext(SynchronizationContext syncContext)92         public static void SetSynchronizationContext(SynchronizationContext syncContext)
93         {
94             RuntimeThread.CurrentThread.SynchronizationContext = syncContext;
95         }
96 
97         internal static SynchronizationContext CurrentExplicit
98         {
99             get
100             {
101                 return RuntimeThread.CurrentThread.SynchronizationContext;
102             }
103         }
104 
105         // helper to Clone this SynchronizationContext,
CreateCopy()106         public virtual SynchronizationContext CreateCopy()
107         {
108             // the CLR dummy has an empty clone function - no member data
109             return new SynchronizationContext();
110         }
111     }
112 }
113