Add memory fence functions

This commit is contained in:
Alex Barney 2022-04-11 13:54:44 -07:00
parent 3a05e779f9
commit 4e5e9a4627
2 changed files with 147 additions and 0 deletions

View file

@ -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();
}
}
}

View file

@ -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();
}