Add Interlocked.MemoryBarrierProcessWide (dotnet/coreclr#10476)
authorJan Kotas <jkotas@microsoft.com>
Sat, 25 Mar 2017 07:42:49 +0000 (00:42 -0700)
committerGitHub <noreply@github.com>
Sat, 25 Mar 2017 07:42:49 +0000 (00:42 -0700)
Contributes to dotnet/coreclr#16799

Commit migrated from https://github.com/dotnet/coreclr/commit/76b7d89f6e32ff51ae4809827163864c40a6a76f

src/coreclr/src/mscorlib/src/System/Threading/CancellationTokenSource.cs
src/coreclr/src/mscorlib/src/System/Threading/Interlocked.cs
src/coreclr/src/mscorlib/src/System/Threading/Thread.cs
src/coreclr/src/mscorlib/src/System/Threading/Volatile.cs
src/coreclr/src/vm/comsynchronizable.cpp
src/coreclr/src/vm/comsynchronizable.h
src/coreclr/src/vm/comutilnative.cpp
src/coreclr/src/vm/comutilnative.h
src/coreclr/src/vm/ecalllist.h

index 3f4aaaa..2707292 100644 (file)
@@ -820,7 +820,7 @@ namespace System.Threading
             {
                 m_state = NOTIFYINGCOMPLETE;
                 m_executingCallback = null;
-                Thread.MemoryBarrier(); // for safety, prevent reorderings crossing this point and seeing inconsistent state.
+                Interlocked.MemoryBarrier(); // for safety, prevent reorderings crossing this point and seeing inconsistent state.
             }
 
             if (exceptionList != null)
index 578201e..7e2c2ae 100644 (file)
@@ -9,6 +9,8 @@ using System.Runtime.CompilerServices;
 using System.Runtime.ConstrainedExecution;
 using System.Runtime.Versioning;
 using System.Runtime;
+using System.Runtime.InteropServices;
+using System.Security;
 
 namespace System.Threading
 {
@@ -188,10 +190,16 @@ namespace System.Threading
             return Interlocked.CompareExchange(ref location, 0, 0);
         }
 
+        [MethodImplAttribute(MethodImplOptions.InternalCall)]
+        public static extern void MemoryBarrier();
+
+        [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
+        [SuppressUnmanagedCodeSecurity]
+        private static extern void _MemoryBarrierProcessWide();
 
-        public static void MemoryBarrier()
+        public static void MemoryBarrierProcessWide()
         {
-            Thread.MemoryBarrier();
+            _MemoryBarrierProcessWide();
         }
     }
 }
index 6908b17..70a5d06 100644 (file)
@@ -550,8 +550,6 @@ namespace System.Threading
         [SuppressUnmanagedCodeSecurity]
         private static extern void InformThreadNameChange(ThreadHandle t, String name, int len);
 
-        [MethodImplAttribute(MethodImplOptions.InternalCall)]
-        public static extern void MemoryBarrier();
     } // End of class Thread
 
     // declaring a local var of this enum type and passing it by ref into a function that needs to do a
index 620aa5e..6aac8d6 100644 (file)
@@ -34,7 +34,7 @@ namespace System.Threading
             // The VM will replace this with a more efficient implementation.
             //
             var value = location;
-            Thread.MemoryBarrier();
+            Interlocked.MemoryBarrier();
             return value;
         }
 
@@ -46,7 +46,7 @@ namespace System.Threading
             // The VM will replace this with a more efficient implementation.
             //
             var value = location;
-            Thread.MemoryBarrier();
+            Interlocked.MemoryBarrier();
             return value;
         }
 
@@ -57,7 +57,7 @@ namespace System.Threading
             // The VM will replace this with a more efficient implementation.
             //
             var value = location;
-            Thread.MemoryBarrier();
+            Interlocked.MemoryBarrier();
             return value;
         }
 
@@ -68,7 +68,7 @@ namespace System.Threading
             // The VM will replace this with a more efficient implementation.
             //
             var value = location;
-            Thread.MemoryBarrier();
+            Interlocked.MemoryBarrier();
             return value;
         }
 
@@ -80,7 +80,7 @@ namespace System.Threading
             // The VM will replace this with a more efficient implementation.
             //
             var value = location;
-            Thread.MemoryBarrier();
+            Interlocked.MemoryBarrier();
             return value;
         }
 
@@ -91,7 +91,7 @@ namespace System.Threading
             // The VM will replace this with a more efficient implementation.
             //
             var value = location;
-            Thread.MemoryBarrier();
+            Interlocked.MemoryBarrier();
             return value;
         }
 
@@ -103,7 +103,7 @@ namespace System.Threading
             // The VM will replace this with a more efficient implementation.
             //
             var value = location;
-            Thread.MemoryBarrier();
+            Interlocked.MemoryBarrier();
             return value;
         }
 
@@ -115,7 +115,7 @@ namespace System.Threading
             // The VM will replace this with a more efficient implementation.
             //
             var value = location;
-            Thread.MemoryBarrier();
+            Interlocked.MemoryBarrier();
             return value;
         }
 
@@ -127,7 +127,7 @@ namespace System.Threading
             // The VM will replace this with a more efficient implementation.
             //
             var value = location;
-            Thread.MemoryBarrier();
+            Interlocked.MemoryBarrier();
             return value;
         }
 #else
@@ -166,7 +166,7 @@ namespace System.Threading
             // The VM will replace this with a more efficient implementation.
             //
             var value = location;
-            Thread.MemoryBarrier();
+            Interlocked.MemoryBarrier();
             return value;
         }
 
@@ -178,7 +178,7 @@ namespace System.Threading
             // The VM will replace this with a more efficient implementation.
             //
             var value = location;
-            Thread.MemoryBarrier();
+            Interlocked.MemoryBarrier();
             return value;
         }
 
@@ -189,7 +189,7 @@ namespace System.Threading
             // The VM will replace this with a more efficient implementation.
             //
             var value = location;
-            Thread.MemoryBarrier();
+            Interlocked.MemoryBarrier();
             return value;
         }
 
@@ -212,7 +212,7 @@ namespace System.Threading
             // The VM will replace this with a more efficient implementation.
             //
             var value = location;
-            Thread.MemoryBarrier();
+            Interlocked.MemoryBarrier();
             return value;
         }
 
@@ -225,7 +225,7 @@ namespace System.Threading
             // 
             // The VM will replace this with a more efficient implementation.
             //
-            Thread.MemoryBarrier();
+            Interlocked.MemoryBarrier();
             location = value;
         }
 
@@ -236,7 +236,7 @@ namespace System.Threading
             // 
             // The VM will replace this with a more efficient implementation.
             //
-            Thread.MemoryBarrier();
+            Interlocked.MemoryBarrier();
             location = value;
         }
 
@@ -246,7 +246,7 @@ namespace System.Threading
             // 
             // The VM will replace this with a more efficient implementation.
             //
-            Thread.MemoryBarrier();
+            Interlocked.MemoryBarrier();
             location = value;
         }
 
@@ -256,7 +256,7 @@ namespace System.Threading
             // 
             // The VM will replace this with a more efficient implementation.
             //
-            Thread.MemoryBarrier();
+            Interlocked.MemoryBarrier();
             location = value;
         }
 
@@ -267,7 +267,7 @@ namespace System.Threading
             // 
             // The VM will replace this with a more efficient implementation.
             //
-            Thread.MemoryBarrier();
+            Interlocked.MemoryBarrier();
             location = value;
         }
 
@@ -277,7 +277,7 @@ namespace System.Threading
             // 
             // The VM will replace this with a more efficient implementation.
             //
-            Thread.MemoryBarrier();
+            Interlocked.MemoryBarrier();
             location = value;
         }
 
@@ -288,7 +288,7 @@ namespace System.Threading
             // 
             // The VM will replace this with a more efficient implementation.
             //
-            Thread.MemoryBarrier();
+            Interlocked.MemoryBarrier();
             location = value;
         }
 
@@ -299,7 +299,7 @@ namespace System.Threading
             // 
             // The VM will replace this with a more efficient implementation.
             //
-            Thread.MemoryBarrier();
+            Interlocked.MemoryBarrier();
             location = value;
         }
 
@@ -310,7 +310,7 @@ namespace System.Threading
             // 
             // The VM will replace this with a more efficient implementation.
             //
-            Thread.MemoryBarrier();
+            Interlocked.MemoryBarrier();
             location = value;
         }
 #else
@@ -354,7 +354,7 @@ namespace System.Threading
             // 
             // The VM will replace this with a more efficient implementation.
             //
-            Thread.MemoryBarrier();
+            Interlocked.MemoryBarrier();
             location = value;
         }
 
@@ -365,7 +365,7 @@ namespace System.Threading
             // 
             // The VM will replace this with a more efficient implementation.
             //
-            Thread.MemoryBarrier();
+            Interlocked.MemoryBarrier();
             location = value;
         }
 
@@ -375,7 +375,7 @@ namespace System.Threading
             // 
             // The VM will replace this with a more efficient implementation.
             //
-            Thread.MemoryBarrier();
+            Interlocked.MemoryBarrier();
             location = value;
         }
 
@@ -397,7 +397,7 @@ namespace System.Threading
             // 
             // The VM will replace this with a more efficient implementation.
             //
-            Thread.MemoryBarrier();
+            Interlocked.MemoryBarrier();
             location = value;
         }
     }
index bd24f6d..b8f871d 100644 (file)
@@ -1669,16 +1669,6 @@ BOOL QCALLTYPE ThreadNative::YieldThread()
     return ret;
 }
 
-
-FCIMPL0(void, ThreadNative::FCMemoryBarrier)
-{
-    FCALL_CONTRACT;
-
-    MemoryBarrier();
-    FC_GC_POLL();
-}
-FCIMPLEND
-
 FCIMPL2(void, ThreadNative::SetAbortReason, ThreadBaseObject* pThisUNSAFE, Object* pObject)
 {
     FCALL_CONTRACT;
index e9b4b62..00b055c 100644 (file)
@@ -106,7 +106,6 @@ public:
 #endif //FEATURE_COMINTEROP
     static FCDECL1(FC_BOOL_RET,IsThreadpoolThread,             ThreadBaseObject* thread);
 
-    static FCDECL0(void, FCMemoryBarrier);
     static FCDECL1(void, SetIsThreadStaticsArray, Object* pObject);
 
     static FCDECL2(void,    SetAbortReason, ThreadBaseObject* pThisUNSAFE, Object* pObject);
index 8e8d8a6..10d0225 100644 (file)
@@ -2584,9 +2584,23 @@ FCIMPL2_IV(INT64,COMInterlocked::ExchangeAdd64, INT64 *location, INT64 value)
 }
 FCIMPLEND
 
+FCIMPL0(void, COMInterlocked::FCMemoryBarrier)
+{
+    FCALL_CONTRACT;
+
+    MemoryBarrier();
+    FC_GC_POLL();
+}
+FCIMPLEND
+
 #include <optdefault.h>
 
+void QCALLTYPE COMInterlocked::MemoryBarrierProcessWide()
+{
+    QCALL_CONTRACT;
 
+    FlushProcessWriteBuffers();
+}
 
 FCIMPL6(INT32, ManagedLoggingHelper::GetRegistryLoggingValues, CLR_BOOL* bLoggingEnabled, CLR_BOOL* bLogToConsole, INT32 *iLogLevel, CLR_BOOL* bPerfWarnings, CLR_BOOL* bCorrectnessWarnings, CLR_BOOL* bSafeHandleStackTraces)
 {
index 5f581e2..41df265 100644 (file)
@@ -234,6 +234,9 @@ public:
         static FCDECL2_IV(INT64, ExchangeAdd64, INT64 *location, INT64 value);
         static FCDECL2_VV(void, ExchangeGeneric, FC_TypedByRef location, FC_TypedByRef value);
         static FCDECL3_VVI(void, CompareExchangeGeneric, FC_TypedByRef location, FC_TypedByRef value, LPVOID comparand);
+
+        static FCDECL0(void, FCMemoryBarrier);
+        static void QCALLTYPE MemoryBarrierProcessWide();
 };
 
 class ManagedLoggingHelper {
index 6adc3a1..a34d538 100644 (file)
@@ -765,7 +765,6 @@ FCFuncStart(gThreadFuncs)
 #ifdef FEATURE_COMINTEROP_APARTMENT_SUPPORT
     FCFuncElement("StartupSetApartmentStateInternal", ThreadNative::StartupSetApartmentState)
 #endif // FEATURE_COMINTEROP_APARTMENT_SUPPORT
-    FCIntrinsic("MemoryBarrier", ThreadNative::FCMemoryBarrier, CORINFO_INTRINSIC_MemoryBarrier)
 FCFuncEnd()
 
 FCFuncStart(gThreadPoolFuncs)
@@ -1047,7 +1046,9 @@ FCFuncStart(gInterlockedFuncs)
 
     FCFuncElement("_Exchange", COMInterlocked::ExchangeGeneric)
     FCFuncElement("_CompareExchange", COMInterlocked::CompareExchangeGeneric)
-    
+
+    FCIntrinsic("MemoryBarrier", COMInterlocked::FCMemoryBarrier, CORINFO_INTRINSIC_MemoryBarrier)
+    QCFuncElement("_MemoryBarrierProcessWide", COMInterlocked::MemoryBarrierProcessWide)
 FCFuncEnd()
 
 FCFuncStart(gVarArgFuncs)