From 4e5e9a4627b0156b9af997b60f6c95750428d6eb Mon Sep 17 00:00:00 2001 From: Alex Barney Date: Mon, 11 Apr 2022 13:54:44 -0700 Subject: [PATCH] Add memory fence functions --- src/LibHac/Os/Impl/MemoryFence.cs | 128 ++++++++++++++++++++++++++++++ src/LibHac/Os/MemoryFenceApi.cs | 19 +++++ 2 files changed, 147 insertions(+) create mode 100644 src/LibHac/Os/Impl/MemoryFence.cs create mode 100644 src/LibHac/Os/MemoryFenceApi.cs diff --git a/src/LibHac/Os/Impl/MemoryFence.cs b/src/LibHac/Os/Impl/MemoryFence.cs new file mode 100644 index 00000000..725d188d --- /dev/null +++ b/src/LibHac/Os/Impl/MemoryFence.cs @@ -0,0 +1,128 @@ +using System.Runtime.CompilerServices; +using System.Runtime.Intrinsics.X86; + +namespace LibHac.Os.Impl; + +public static class MemoryFence +{ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void FenceMemoryStoreStore() + { + if (Sse.IsSupported) + { + Sse.StoreFence(); + } + else + { + // This only needs to be a store barrier on aarch64 + System.Threading.Thread.MemoryBarrier(); + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void FenceMemoryStoreLoad() + { + if (Sse2.IsSupported) + { + Sse2.MemoryFence(); + } + else + { + System.Threading.Thread.MemoryBarrier(); + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void FenceMemoryStoreAny() + { + if (Sse2.IsSupported) + { + Sse2.MemoryFence(); + } + else + { + System.Threading.Thread.MemoryBarrier(); + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void FenceMemoryLoadStore() + { + if (Sse2.IsSupported) + { + Sse2.MemoryFence(); + } + else + { + // This only needs to be a load barrier on aarch64 + System.Threading.Thread.MemoryBarrier(); + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void FenceMemoryLoadLoad() + { + if (Sse2.IsSupported) + { + Sse2.LoadFence(); + } + else + { + // This only needs to be a load barrier on aarch64 + System.Threading.Thread.MemoryBarrier(); + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void FenceMemoryLoadAny() + { + if (Sse2.IsSupported) + { + Sse2.MemoryFence(); + } + else + { + // This only needs to be a load barrier on aarch64 + System.Threading.Thread.MemoryBarrier(); + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void FenceMemoryAnyStore() + { + if (Sse2.IsSupported) + { + Sse2.MemoryFence(); + } + else + { + System.Threading.Thread.MemoryBarrier(); + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void FenceMemoryAnyLoad() + { + if (Sse2.IsSupported) + { + Sse2.MemoryFence(); + } + else + { + System.Threading.Thread.MemoryBarrier(); + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void FenceMemoryAnyAny() + { + if (Sse2.IsSupported) + { + Sse2.MemoryFence(); + } + else + { + System.Threading.Thread.MemoryBarrier(); + } + } +} \ No newline at end of file diff --git a/src/LibHac/Os/MemoryFenceApi.cs b/src/LibHac/Os/MemoryFenceApi.cs new file mode 100644 index 00000000..35949014 --- /dev/null +++ b/src/LibHac/Os/MemoryFenceApi.cs @@ -0,0 +1,19 @@ +using System.Runtime.CompilerServices; +using LibHac.Os.Impl; + +namespace LibHac.Os; + +public static class MemoryFenceApi +{ + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void FenceMemoryStoreStore() => MemoryFence.FenceMemoryStoreStore(); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void FenceMemoryStoreLoad() => MemoryFence.FenceMemoryStoreLoad(); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void FenceMemoryStoreAny() => MemoryFence.FenceMemoryStoreAny(); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void FenceMemoryLoadStore() => MemoryFence.FenceMemoryLoadStore(); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void FenceMemoryLoadLoad() => MemoryFence.FenceMemoryLoadLoad(); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void FenceMemoryLoadAny() => MemoryFence.FenceMemoryLoadAny(); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void FenceMemoryAnyStore() => MemoryFence.FenceMemoryAnyStore(); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void FenceMemoryAnyLoad() => MemoryFence.FenceMemoryAnyLoad(); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void FenceMemoryAnyAny() => MemoryFence.FenceMemoryAnyAny(); +} \ No newline at end of file