From 1926888378a19f6c196ab90cb7bb33783eb48e97 Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Sat, 25 Mar 2017 00:42:49 -0700 Subject: [PATCH] Add Interlocked.MemoryBarrierProcessWide (dotnet/coreclr#10476) Contributes to dotnet/coreclr#16799 Commit migrated from https://github.com/dotnet/coreclr/commit/76b7d89f6e32ff51ae4809827163864c40a6a76f --- .../System/Threading/CancellationTokenSource.cs | 2 +- .../mscorlib/src/System/Threading/Interlocked.cs | 12 ++++- .../src/mscorlib/src/System/Threading/Thread.cs | 2 - .../src/mscorlib/src/System/Threading/Volatile.cs | 52 +++++++++++----------- src/coreclr/src/vm/comsynchronizable.cpp | 10 ----- src/coreclr/src/vm/comsynchronizable.h | 1 - src/coreclr/src/vm/comutilnative.cpp | 14 ++++++ src/coreclr/src/vm/comutilnative.h | 3 ++ src/coreclr/src/vm/ecalllist.h | 5 ++- 9 files changed, 57 insertions(+), 44 deletions(-) diff --git a/src/coreclr/src/mscorlib/src/System/Threading/CancellationTokenSource.cs b/src/coreclr/src/mscorlib/src/System/Threading/CancellationTokenSource.cs index 3f4aaaa..2707292 100644 --- a/src/coreclr/src/mscorlib/src/System/Threading/CancellationTokenSource.cs +++ b/src/coreclr/src/mscorlib/src/System/Threading/CancellationTokenSource.cs @@ -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) diff --git a/src/coreclr/src/mscorlib/src/System/Threading/Interlocked.cs b/src/coreclr/src/mscorlib/src/System/Threading/Interlocked.cs index 578201e..7e2c2ae 100644 --- a/src/coreclr/src/mscorlib/src/System/Threading/Interlocked.cs +++ b/src/coreclr/src/mscorlib/src/System/Threading/Interlocked.cs @@ -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(); } } } diff --git a/src/coreclr/src/mscorlib/src/System/Threading/Thread.cs b/src/coreclr/src/mscorlib/src/System/Threading/Thread.cs index 6908b17..70a5d06 100644 --- a/src/coreclr/src/mscorlib/src/System/Threading/Thread.cs +++ b/src/coreclr/src/mscorlib/src/System/Threading/Thread.cs @@ -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 diff --git a/src/coreclr/src/mscorlib/src/System/Threading/Volatile.cs b/src/coreclr/src/mscorlib/src/System/Threading/Volatile.cs index 620aa5e..6aac8d6 100644 --- a/src/coreclr/src/mscorlib/src/System/Threading/Volatile.cs +++ b/src/coreclr/src/mscorlib/src/System/Threading/Volatile.cs @@ -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; } } diff --git a/src/coreclr/src/vm/comsynchronizable.cpp b/src/coreclr/src/vm/comsynchronizable.cpp index bd24f6d..b8f871d 100644 --- a/src/coreclr/src/vm/comsynchronizable.cpp +++ b/src/coreclr/src/vm/comsynchronizable.cpp @@ -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; diff --git a/src/coreclr/src/vm/comsynchronizable.h b/src/coreclr/src/vm/comsynchronizable.h index e9b4b62..00b055c 100644 --- a/src/coreclr/src/vm/comsynchronizable.h +++ b/src/coreclr/src/vm/comsynchronizable.h @@ -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); diff --git a/src/coreclr/src/vm/comutilnative.cpp b/src/coreclr/src/vm/comutilnative.cpp index 8e8d8a6..10d0225 100644 --- a/src/coreclr/src/vm/comutilnative.cpp +++ b/src/coreclr/src/vm/comutilnative.cpp @@ -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 +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) { diff --git a/src/coreclr/src/vm/comutilnative.h b/src/coreclr/src/vm/comutilnative.h index 5f581e2..41df265 100644 --- a/src/coreclr/src/vm/comutilnative.h +++ b/src/coreclr/src/vm/comutilnative.h @@ -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 { diff --git a/src/coreclr/src/vm/ecalllist.h b/src/coreclr/src/vm/ecalllist.h index 6adc3a1..a34d538 100644 --- a/src/coreclr/src/vm/ecalllist.h +++ b/src/coreclr/src/vm/ecalllist.h @@ -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) -- 2.7.4