From 791cb790d088e8e5faef165be9264572064a3db3 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Sun, 11 Oct 2026 23:47:49 +1100 Subject: [PATCH] Support 1 and 2 byte types in Interlocked.And and Interlocked.Or --- src/Polyfill/InterlockedPolyfill.cs | 96 +++++++++++++++++++ src/Split/net10.0/InterlockedPolyfill.cs | 84 ++++++++++++++++ src/Split/net461/InterlockedPolyfill.cs | 84 ++++++++++++++++ src/Split/net462/InterlockedPolyfill.cs | 84 ++++++++++++++++ src/Split/net47/InterlockedPolyfill.cs | 84 ++++++++++++++++ src/Split/net471/InterlockedPolyfill.cs | 84 ++++++++++++++++ src/Split/net472/InterlockedPolyfill.cs | 84 ++++++++++++++++ src/Split/net48/InterlockedPolyfill.cs | 84 ++++++++++++++++ src/Split/net481/InterlockedPolyfill.cs | 84 ++++++++++++++++ src/Split/net5.0/InterlockedPolyfill.cs | 84 ++++++++++++++++ src/Split/net6.0/InterlockedPolyfill.cs | 84 ++++++++++++++++ src/Split/net7.0/InterlockedPolyfill.cs | 84 ++++++++++++++++ src/Split/net8.0/InterlockedPolyfill.cs | 84 ++++++++++++++++ src/Split/net9.0/InterlockedPolyfill.cs | 84 ++++++++++++++++ .../netcoreapp2.0/InterlockedPolyfill.cs | 84 ++++++++++++++++ .../netcoreapp2.1/InterlockedPolyfill.cs | 84 ++++++++++++++++ .../netcoreapp2.2/InterlockedPolyfill.cs | 84 ++++++++++++++++ .../netcoreapp3.0/InterlockedPolyfill.cs | 84 ++++++++++++++++ .../netcoreapp3.1/InterlockedPolyfill.cs | 84 ++++++++++++++++ .../netstandard2.0/InterlockedPolyfill.cs | 84 ++++++++++++++++ .../netstandard2.1/InterlockedPolyfill.cs | 84 ++++++++++++++++ src/Split/uap10.0/InterlockedPolyfill.cs | 84 ++++++++++++++++ src/Tests/PolyfillTests_Interlocked.cs | 91 ++++++++++++++++++ 23 files changed, 1951 insertions(+) diff --git a/src/Polyfill/InterlockedPolyfill.cs b/src/Polyfill/InterlockedPolyfill.cs index ba54e954..e1d21788 100644 --- a/src/Polyfill/InterlockedPolyfill.cs +++ b/src/Polyfill/InterlockedPolyfill.cs @@ -20,6 +20,22 @@ static partial class Polyfill public static T And(ref T location1, T value) where T : struct { + if (Unsafe.SizeOf() == sizeof(byte)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedAnd(ref loc, val); + return Unsafe.As(ref result); + } + + if (Unsafe.SizeOf() == sizeof(ushort)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedAnd(ref loc, val); + return Unsafe.As(ref result); + } + if (Unsafe.SizeOf() == sizeof(int)) { ref var loc = ref Unsafe.As(ref location1); @@ -46,6 +62,22 @@ public static T And(ref T location1, T value) public static T Or(ref T location1, T value) where T : struct { + if (Unsafe.SizeOf() == sizeof(byte)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedOr(ref loc, val); + return Unsafe.As(ref result); + } + + if (Unsafe.SizeOf() == sizeof(ushort)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedOr(ref loc, val); + return Unsafe.As(ref result); + } + if (Unsafe.SizeOf() == sizeof(int)) { ref var loc = ref Unsafe.As(ref location1); @@ -66,6 +98,70 @@ public static T Or(ref T location1, T value) } } + static byte InterlockedAnd(ref byte location, byte value) + { + var current = location; + while (true) + { + var newValue = (byte) (current & value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + + current = oldValue; + } + } + + static ushort InterlockedAnd(ref ushort location, ushort value) + { + var current = location; + while (true) + { + var newValue = (ushort) (current & value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + + current = oldValue; + } + } + + static byte InterlockedOr(ref byte location, byte value) + { + var current = location; + while (true) + { + var newValue = (byte) (current | value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + + current = oldValue; + } + } + + static ushort InterlockedOr(ref ushort location, ushort value) + { + var current = location; + while (true) + { + var newValue = (ushort) (current | value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + + current = oldValue; + } + } + static int InterlockedAnd(ref int location, int value) { var current = location; diff --git a/src/Split/net10.0/InterlockedPolyfill.cs b/src/Split/net10.0/InterlockedPolyfill.cs index 2dabf01b..8ca0a457 100644 --- a/src/Split/net10.0/InterlockedPolyfill.cs +++ b/src/Split/net10.0/InterlockedPolyfill.cs @@ -17,6 +17,20 @@ static partial class Polyfill public static T And(ref T location1, T value) where T : struct { + if (Unsafe.SizeOf() == sizeof(byte)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedAnd(ref loc, val); + return Unsafe.As(ref result); + } + if (Unsafe.SizeOf() == sizeof(ushort)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedAnd(ref loc, val); + return Unsafe.As(ref result); + } if (Unsafe.SizeOf() == sizeof(int)) { ref var loc = ref Unsafe.As(ref location1); @@ -39,6 +53,20 @@ public static T And(ref T location1, T value) public static T Or(ref T location1, T value) where T : struct { + if (Unsafe.SizeOf() == sizeof(byte)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedOr(ref loc, val); + return Unsafe.As(ref result); + } + if (Unsafe.SizeOf() == sizeof(ushort)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedOr(ref loc, val); + return Unsafe.As(ref result); + } if (Unsafe.SizeOf() == sizeof(int)) { ref var loc = ref Unsafe.As(ref location1); @@ -56,6 +84,62 @@ public static T Or(ref T location1, T value) throw new System.NotSupportedException(); } } + static byte InterlockedAnd(ref byte location, byte value) + { + var current = location; + while (true) + { + var newValue = (byte) (current & value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static ushort InterlockedAnd(ref ushort location, ushort value) + { + var current = location; + while (true) + { + var newValue = (ushort) (current & value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static byte InterlockedOr(ref byte location, byte value) + { + var current = location; + while (true) + { + var newValue = (byte) (current | value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static ushort InterlockedOr(ref ushort location, ushort value) + { + var current = location; + while (true) + { + var newValue = (ushort) (current | value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } static int InterlockedAnd(ref int location, int value) { var current = location; diff --git a/src/Split/net461/InterlockedPolyfill.cs b/src/Split/net461/InterlockedPolyfill.cs index 2dabf01b..8ca0a457 100644 --- a/src/Split/net461/InterlockedPolyfill.cs +++ b/src/Split/net461/InterlockedPolyfill.cs @@ -17,6 +17,20 @@ static partial class Polyfill public static T And(ref T location1, T value) where T : struct { + if (Unsafe.SizeOf() == sizeof(byte)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedAnd(ref loc, val); + return Unsafe.As(ref result); + } + if (Unsafe.SizeOf() == sizeof(ushort)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedAnd(ref loc, val); + return Unsafe.As(ref result); + } if (Unsafe.SizeOf() == sizeof(int)) { ref var loc = ref Unsafe.As(ref location1); @@ -39,6 +53,20 @@ public static T And(ref T location1, T value) public static T Or(ref T location1, T value) where T : struct { + if (Unsafe.SizeOf() == sizeof(byte)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedOr(ref loc, val); + return Unsafe.As(ref result); + } + if (Unsafe.SizeOf() == sizeof(ushort)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedOr(ref loc, val); + return Unsafe.As(ref result); + } if (Unsafe.SizeOf() == sizeof(int)) { ref var loc = ref Unsafe.As(ref location1); @@ -56,6 +84,62 @@ public static T Or(ref T location1, T value) throw new System.NotSupportedException(); } } + static byte InterlockedAnd(ref byte location, byte value) + { + var current = location; + while (true) + { + var newValue = (byte) (current & value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static ushort InterlockedAnd(ref ushort location, ushort value) + { + var current = location; + while (true) + { + var newValue = (ushort) (current & value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static byte InterlockedOr(ref byte location, byte value) + { + var current = location; + while (true) + { + var newValue = (byte) (current | value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static ushort InterlockedOr(ref ushort location, ushort value) + { + var current = location; + while (true) + { + var newValue = (ushort) (current | value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } static int InterlockedAnd(ref int location, int value) { var current = location; diff --git a/src/Split/net462/InterlockedPolyfill.cs b/src/Split/net462/InterlockedPolyfill.cs index 2dabf01b..8ca0a457 100644 --- a/src/Split/net462/InterlockedPolyfill.cs +++ b/src/Split/net462/InterlockedPolyfill.cs @@ -17,6 +17,20 @@ static partial class Polyfill public static T And(ref T location1, T value) where T : struct { + if (Unsafe.SizeOf() == sizeof(byte)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedAnd(ref loc, val); + return Unsafe.As(ref result); + } + if (Unsafe.SizeOf() == sizeof(ushort)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedAnd(ref loc, val); + return Unsafe.As(ref result); + } if (Unsafe.SizeOf() == sizeof(int)) { ref var loc = ref Unsafe.As(ref location1); @@ -39,6 +53,20 @@ public static T And(ref T location1, T value) public static T Or(ref T location1, T value) where T : struct { + if (Unsafe.SizeOf() == sizeof(byte)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedOr(ref loc, val); + return Unsafe.As(ref result); + } + if (Unsafe.SizeOf() == sizeof(ushort)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedOr(ref loc, val); + return Unsafe.As(ref result); + } if (Unsafe.SizeOf() == sizeof(int)) { ref var loc = ref Unsafe.As(ref location1); @@ -56,6 +84,62 @@ public static T Or(ref T location1, T value) throw new System.NotSupportedException(); } } + static byte InterlockedAnd(ref byte location, byte value) + { + var current = location; + while (true) + { + var newValue = (byte) (current & value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static ushort InterlockedAnd(ref ushort location, ushort value) + { + var current = location; + while (true) + { + var newValue = (ushort) (current & value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static byte InterlockedOr(ref byte location, byte value) + { + var current = location; + while (true) + { + var newValue = (byte) (current | value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static ushort InterlockedOr(ref ushort location, ushort value) + { + var current = location; + while (true) + { + var newValue = (ushort) (current | value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } static int InterlockedAnd(ref int location, int value) { var current = location; diff --git a/src/Split/net47/InterlockedPolyfill.cs b/src/Split/net47/InterlockedPolyfill.cs index 2dabf01b..8ca0a457 100644 --- a/src/Split/net47/InterlockedPolyfill.cs +++ b/src/Split/net47/InterlockedPolyfill.cs @@ -17,6 +17,20 @@ static partial class Polyfill public static T And(ref T location1, T value) where T : struct { + if (Unsafe.SizeOf() == sizeof(byte)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedAnd(ref loc, val); + return Unsafe.As(ref result); + } + if (Unsafe.SizeOf() == sizeof(ushort)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedAnd(ref loc, val); + return Unsafe.As(ref result); + } if (Unsafe.SizeOf() == sizeof(int)) { ref var loc = ref Unsafe.As(ref location1); @@ -39,6 +53,20 @@ public static T And(ref T location1, T value) public static T Or(ref T location1, T value) where T : struct { + if (Unsafe.SizeOf() == sizeof(byte)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedOr(ref loc, val); + return Unsafe.As(ref result); + } + if (Unsafe.SizeOf() == sizeof(ushort)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedOr(ref loc, val); + return Unsafe.As(ref result); + } if (Unsafe.SizeOf() == sizeof(int)) { ref var loc = ref Unsafe.As(ref location1); @@ -56,6 +84,62 @@ public static T Or(ref T location1, T value) throw new System.NotSupportedException(); } } + static byte InterlockedAnd(ref byte location, byte value) + { + var current = location; + while (true) + { + var newValue = (byte) (current & value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static ushort InterlockedAnd(ref ushort location, ushort value) + { + var current = location; + while (true) + { + var newValue = (ushort) (current & value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static byte InterlockedOr(ref byte location, byte value) + { + var current = location; + while (true) + { + var newValue = (byte) (current | value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static ushort InterlockedOr(ref ushort location, ushort value) + { + var current = location; + while (true) + { + var newValue = (ushort) (current | value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } static int InterlockedAnd(ref int location, int value) { var current = location; diff --git a/src/Split/net471/InterlockedPolyfill.cs b/src/Split/net471/InterlockedPolyfill.cs index 2dabf01b..8ca0a457 100644 --- a/src/Split/net471/InterlockedPolyfill.cs +++ b/src/Split/net471/InterlockedPolyfill.cs @@ -17,6 +17,20 @@ static partial class Polyfill public static T And(ref T location1, T value) where T : struct { + if (Unsafe.SizeOf() == sizeof(byte)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedAnd(ref loc, val); + return Unsafe.As(ref result); + } + if (Unsafe.SizeOf() == sizeof(ushort)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedAnd(ref loc, val); + return Unsafe.As(ref result); + } if (Unsafe.SizeOf() == sizeof(int)) { ref var loc = ref Unsafe.As(ref location1); @@ -39,6 +53,20 @@ public static T And(ref T location1, T value) public static T Or(ref T location1, T value) where T : struct { + if (Unsafe.SizeOf() == sizeof(byte)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedOr(ref loc, val); + return Unsafe.As(ref result); + } + if (Unsafe.SizeOf() == sizeof(ushort)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedOr(ref loc, val); + return Unsafe.As(ref result); + } if (Unsafe.SizeOf() == sizeof(int)) { ref var loc = ref Unsafe.As(ref location1); @@ -56,6 +84,62 @@ public static T Or(ref T location1, T value) throw new System.NotSupportedException(); } } + static byte InterlockedAnd(ref byte location, byte value) + { + var current = location; + while (true) + { + var newValue = (byte) (current & value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static ushort InterlockedAnd(ref ushort location, ushort value) + { + var current = location; + while (true) + { + var newValue = (ushort) (current & value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static byte InterlockedOr(ref byte location, byte value) + { + var current = location; + while (true) + { + var newValue = (byte) (current | value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static ushort InterlockedOr(ref ushort location, ushort value) + { + var current = location; + while (true) + { + var newValue = (ushort) (current | value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } static int InterlockedAnd(ref int location, int value) { var current = location; diff --git a/src/Split/net472/InterlockedPolyfill.cs b/src/Split/net472/InterlockedPolyfill.cs index 2dabf01b..8ca0a457 100644 --- a/src/Split/net472/InterlockedPolyfill.cs +++ b/src/Split/net472/InterlockedPolyfill.cs @@ -17,6 +17,20 @@ static partial class Polyfill public static T And(ref T location1, T value) where T : struct { + if (Unsafe.SizeOf() == sizeof(byte)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedAnd(ref loc, val); + return Unsafe.As(ref result); + } + if (Unsafe.SizeOf() == sizeof(ushort)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedAnd(ref loc, val); + return Unsafe.As(ref result); + } if (Unsafe.SizeOf() == sizeof(int)) { ref var loc = ref Unsafe.As(ref location1); @@ -39,6 +53,20 @@ public static T And(ref T location1, T value) public static T Or(ref T location1, T value) where T : struct { + if (Unsafe.SizeOf() == sizeof(byte)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedOr(ref loc, val); + return Unsafe.As(ref result); + } + if (Unsafe.SizeOf() == sizeof(ushort)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedOr(ref loc, val); + return Unsafe.As(ref result); + } if (Unsafe.SizeOf() == sizeof(int)) { ref var loc = ref Unsafe.As(ref location1); @@ -56,6 +84,62 @@ public static T Or(ref T location1, T value) throw new System.NotSupportedException(); } } + static byte InterlockedAnd(ref byte location, byte value) + { + var current = location; + while (true) + { + var newValue = (byte) (current & value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static ushort InterlockedAnd(ref ushort location, ushort value) + { + var current = location; + while (true) + { + var newValue = (ushort) (current & value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static byte InterlockedOr(ref byte location, byte value) + { + var current = location; + while (true) + { + var newValue = (byte) (current | value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static ushort InterlockedOr(ref ushort location, ushort value) + { + var current = location; + while (true) + { + var newValue = (ushort) (current | value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } static int InterlockedAnd(ref int location, int value) { var current = location; diff --git a/src/Split/net48/InterlockedPolyfill.cs b/src/Split/net48/InterlockedPolyfill.cs index 2dabf01b..8ca0a457 100644 --- a/src/Split/net48/InterlockedPolyfill.cs +++ b/src/Split/net48/InterlockedPolyfill.cs @@ -17,6 +17,20 @@ static partial class Polyfill public static T And(ref T location1, T value) where T : struct { + if (Unsafe.SizeOf() == sizeof(byte)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedAnd(ref loc, val); + return Unsafe.As(ref result); + } + if (Unsafe.SizeOf() == sizeof(ushort)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedAnd(ref loc, val); + return Unsafe.As(ref result); + } if (Unsafe.SizeOf() == sizeof(int)) { ref var loc = ref Unsafe.As(ref location1); @@ -39,6 +53,20 @@ public static T And(ref T location1, T value) public static T Or(ref T location1, T value) where T : struct { + if (Unsafe.SizeOf() == sizeof(byte)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedOr(ref loc, val); + return Unsafe.As(ref result); + } + if (Unsafe.SizeOf() == sizeof(ushort)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedOr(ref loc, val); + return Unsafe.As(ref result); + } if (Unsafe.SizeOf() == sizeof(int)) { ref var loc = ref Unsafe.As(ref location1); @@ -56,6 +84,62 @@ public static T Or(ref T location1, T value) throw new System.NotSupportedException(); } } + static byte InterlockedAnd(ref byte location, byte value) + { + var current = location; + while (true) + { + var newValue = (byte) (current & value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static ushort InterlockedAnd(ref ushort location, ushort value) + { + var current = location; + while (true) + { + var newValue = (ushort) (current & value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static byte InterlockedOr(ref byte location, byte value) + { + var current = location; + while (true) + { + var newValue = (byte) (current | value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static ushort InterlockedOr(ref ushort location, ushort value) + { + var current = location; + while (true) + { + var newValue = (ushort) (current | value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } static int InterlockedAnd(ref int location, int value) { var current = location; diff --git a/src/Split/net481/InterlockedPolyfill.cs b/src/Split/net481/InterlockedPolyfill.cs index 2dabf01b..8ca0a457 100644 --- a/src/Split/net481/InterlockedPolyfill.cs +++ b/src/Split/net481/InterlockedPolyfill.cs @@ -17,6 +17,20 @@ static partial class Polyfill public static T And(ref T location1, T value) where T : struct { + if (Unsafe.SizeOf() == sizeof(byte)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedAnd(ref loc, val); + return Unsafe.As(ref result); + } + if (Unsafe.SizeOf() == sizeof(ushort)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedAnd(ref loc, val); + return Unsafe.As(ref result); + } if (Unsafe.SizeOf() == sizeof(int)) { ref var loc = ref Unsafe.As(ref location1); @@ -39,6 +53,20 @@ public static T And(ref T location1, T value) public static T Or(ref T location1, T value) where T : struct { + if (Unsafe.SizeOf() == sizeof(byte)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedOr(ref loc, val); + return Unsafe.As(ref result); + } + if (Unsafe.SizeOf() == sizeof(ushort)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedOr(ref loc, val); + return Unsafe.As(ref result); + } if (Unsafe.SizeOf() == sizeof(int)) { ref var loc = ref Unsafe.As(ref location1); @@ -56,6 +84,62 @@ public static T Or(ref T location1, T value) throw new System.NotSupportedException(); } } + static byte InterlockedAnd(ref byte location, byte value) + { + var current = location; + while (true) + { + var newValue = (byte) (current & value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static ushort InterlockedAnd(ref ushort location, ushort value) + { + var current = location; + while (true) + { + var newValue = (ushort) (current & value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static byte InterlockedOr(ref byte location, byte value) + { + var current = location; + while (true) + { + var newValue = (byte) (current | value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static ushort InterlockedOr(ref ushort location, ushort value) + { + var current = location; + while (true) + { + var newValue = (ushort) (current | value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } static int InterlockedAnd(ref int location, int value) { var current = location; diff --git a/src/Split/net5.0/InterlockedPolyfill.cs b/src/Split/net5.0/InterlockedPolyfill.cs index 2dabf01b..8ca0a457 100644 --- a/src/Split/net5.0/InterlockedPolyfill.cs +++ b/src/Split/net5.0/InterlockedPolyfill.cs @@ -17,6 +17,20 @@ static partial class Polyfill public static T And(ref T location1, T value) where T : struct { + if (Unsafe.SizeOf() == sizeof(byte)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedAnd(ref loc, val); + return Unsafe.As(ref result); + } + if (Unsafe.SizeOf() == sizeof(ushort)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedAnd(ref loc, val); + return Unsafe.As(ref result); + } if (Unsafe.SizeOf() == sizeof(int)) { ref var loc = ref Unsafe.As(ref location1); @@ -39,6 +53,20 @@ public static T And(ref T location1, T value) public static T Or(ref T location1, T value) where T : struct { + if (Unsafe.SizeOf() == sizeof(byte)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedOr(ref loc, val); + return Unsafe.As(ref result); + } + if (Unsafe.SizeOf() == sizeof(ushort)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedOr(ref loc, val); + return Unsafe.As(ref result); + } if (Unsafe.SizeOf() == sizeof(int)) { ref var loc = ref Unsafe.As(ref location1); @@ -56,6 +84,62 @@ public static T Or(ref T location1, T value) throw new System.NotSupportedException(); } } + static byte InterlockedAnd(ref byte location, byte value) + { + var current = location; + while (true) + { + var newValue = (byte) (current & value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static ushort InterlockedAnd(ref ushort location, ushort value) + { + var current = location; + while (true) + { + var newValue = (ushort) (current & value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static byte InterlockedOr(ref byte location, byte value) + { + var current = location; + while (true) + { + var newValue = (byte) (current | value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static ushort InterlockedOr(ref ushort location, ushort value) + { + var current = location; + while (true) + { + var newValue = (ushort) (current | value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } static int InterlockedAnd(ref int location, int value) { var current = location; diff --git a/src/Split/net6.0/InterlockedPolyfill.cs b/src/Split/net6.0/InterlockedPolyfill.cs index 2dabf01b..8ca0a457 100644 --- a/src/Split/net6.0/InterlockedPolyfill.cs +++ b/src/Split/net6.0/InterlockedPolyfill.cs @@ -17,6 +17,20 @@ static partial class Polyfill public static T And(ref T location1, T value) where T : struct { + if (Unsafe.SizeOf() == sizeof(byte)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedAnd(ref loc, val); + return Unsafe.As(ref result); + } + if (Unsafe.SizeOf() == sizeof(ushort)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedAnd(ref loc, val); + return Unsafe.As(ref result); + } if (Unsafe.SizeOf() == sizeof(int)) { ref var loc = ref Unsafe.As(ref location1); @@ -39,6 +53,20 @@ public static T And(ref T location1, T value) public static T Or(ref T location1, T value) where T : struct { + if (Unsafe.SizeOf() == sizeof(byte)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedOr(ref loc, val); + return Unsafe.As(ref result); + } + if (Unsafe.SizeOf() == sizeof(ushort)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedOr(ref loc, val); + return Unsafe.As(ref result); + } if (Unsafe.SizeOf() == sizeof(int)) { ref var loc = ref Unsafe.As(ref location1); @@ -56,6 +84,62 @@ public static T Or(ref T location1, T value) throw new System.NotSupportedException(); } } + static byte InterlockedAnd(ref byte location, byte value) + { + var current = location; + while (true) + { + var newValue = (byte) (current & value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static ushort InterlockedAnd(ref ushort location, ushort value) + { + var current = location; + while (true) + { + var newValue = (ushort) (current & value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static byte InterlockedOr(ref byte location, byte value) + { + var current = location; + while (true) + { + var newValue = (byte) (current | value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static ushort InterlockedOr(ref ushort location, ushort value) + { + var current = location; + while (true) + { + var newValue = (ushort) (current | value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } static int InterlockedAnd(ref int location, int value) { var current = location; diff --git a/src/Split/net7.0/InterlockedPolyfill.cs b/src/Split/net7.0/InterlockedPolyfill.cs index 2dabf01b..8ca0a457 100644 --- a/src/Split/net7.0/InterlockedPolyfill.cs +++ b/src/Split/net7.0/InterlockedPolyfill.cs @@ -17,6 +17,20 @@ static partial class Polyfill public static T And(ref T location1, T value) where T : struct { + if (Unsafe.SizeOf() == sizeof(byte)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedAnd(ref loc, val); + return Unsafe.As(ref result); + } + if (Unsafe.SizeOf() == sizeof(ushort)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedAnd(ref loc, val); + return Unsafe.As(ref result); + } if (Unsafe.SizeOf() == sizeof(int)) { ref var loc = ref Unsafe.As(ref location1); @@ -39,6 +53,20 @@ public static T And(ref T location1, T value) public static T Or(ref T location1, T value) where T : struct { + if (Unsafe.SizeOf() == sizeof(byte)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedOr(ref loc, val); + return Unsafe.As(ref result); + } + if (Unsafe.SizeOf() == sizeof(ushort)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedOr(ref loc, val); + return Unsafe.As(ref result); + } if (Unsafe.SizeOf() == sizeof(int)) { ref var loc = ref Unsafe.As(ref location1); @@ -56,6 +84,62 @@ public static T Or(ref T location1, T value) throw new System.NotSupportedException(); } } + static byte InterlockedAnd(ref byte location, byte value) + { + var current = location; + while (true) + { + var newValue = (byte) (current & value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static ushort InterlockedAnd(ref ushort location, ushort value) + { + var current = location; + while (true) + { + var newValue = (ushort) (current & value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static byte InterlockedOr(ref byte location, byte value) + { + var current = location; + while (true) + { + var newValue = (byte) (current | value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static ushort InterlockedOr(ref ushort location, ushort value) + { + var current = location; + while (true) + { + var newValue = (ushort) (current | value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } static int InterlockedAnd(ref int location, int value) { var current = location; diff --git a/src/Split/net8.0/InterlockedPolyfill.cs b/src/Split/net8.0/InterlockedPolyfill.cs index 2dabf01b..8ca0a457 100644 --- a/src/Split/net8.0/InterlockedPolyfill.cs +++ b/src/Split/net8.0/InterlockedPolyfill.cs @@ -17,6 +17,20 @@ static partial class Polyfill public static T And(ref T location1, T value) where T : struct { + if (Unsafe.SizeOf() == sizeof(byte)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedAnd(ref loc, val); + return Unsafe.As(ref result); + } + if (Unsafe.SizeOf() == sizeof(ushort)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedAnd(ref loc, val); + return Unsafe.As(ref result); + } if (Unsafe.SizeOf() == sizeof(int)) { ref var loc = ref Unsafe.As(ref location1); @@ -39,6 +53,20 @@ public static T And(ref T location1, T value) public static T Or(ref T location1, T value) where T : struct { + if (Unsafe.SizeOf() == sizeof(byte)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedOr(ref loc, val); + return Unsafe.As(ref result); + } + if (Unsafe.SizeOf() == sizeof(ushort)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedOr(ref loc, val); + return Unsafe.As(ref result); + } if (Unsafe.SizeOf() == sizeof(int)) { ref var loc = ref Unsafe.As(ref location1); @@ -56,6 +84,62 @@ public static T Or(ref T location1, T value) throw new System.NotSupportedException(); } } + static byte InterlockedAnd(ref byte location, byte value) + { + var current = location; + while (true) + { + var newValue = (byte) (current & value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static ushort InterlockedAnd(ref ushort location, ushort value) + { + var current = location; + while (true) + { + var newValue = (ushort) (current & value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static byte InterlockedOr(ref byte location, byte value) + { + var current = location; + while (true) + { + var newValue = (byte) (current | value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static ushort InterlockedOr(ref ushort location, ushort value) + { + var current = location; + while (true) + { + var newValue = (ushort) (current | value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } static int InterlockedAnd(ref int location, int value) { var current = location; diff --git a/src/Split/net9.0/InterlockedPolyfill.cs b/src/Split/net9.0/InterlockedPolyfill.cs index 2dabf01b..8ca0a457 100644 --- a/src/Split/net9.0/InterlockedPolyfill.cs +++ b/src/Split/net9.0/InterlockedPolyfill.cs @@ -17,6 +17,20 @@ static partial class Polyfill public static T And(ref T location1, T value) where T : struct { + if (Unsafe.SizeOf() == sizeof(byte)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedAnd(ref loc, val); + return Unsafe.As(ref result); + } + if (Unsafe.SizeOf() == sizeof(ushort)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedAnd(ref loc, val); + return Unsafe.As(ref result); + } if (Unsafe.SizeOf() == sizeof(int)) { ref var loc = ref Unsafe.As(ref location1); @@ -39,6 +53,20 @@ public static T And(ref T location1, T value) public static T Or(ref T location1, T value) where T : struct { + if (Unsafe.SizeOf() == sizeof(byte)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedOr(ref loc, val); + return Unsafe.As(ref result); + } + if (Unsafe.SizeOf() == sizeof(ushort)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedOr(ref loc, val); + return Unsafe.As(ref result); + } if (Unsafe.SizeOf() == sizeof(int)) { ref var loc = ref Unsafe.As(ref location1); @@ -56,6 +84,62 @@ public static T Or(ref T location1, T value) throw new System.NotSupportedException(); } } + static byte InterlockedAnd(ref byte location, byte value) + { + var current = location; + while (true) + { + var newValue = (byte) (current & value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static ushort InterlockedAnd(ref ushort location, ushort value) + { + var current = location; + while (true) + { + var newValue = (ushort) (current & value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static byte InterlockedOr(ref byte location, byte value) + { + var current = location; + while (true) + { + var newValue = (byte) (current | value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static ushort InterlockedOr(ref ushort location, ushort value) + { + var current = location; + while (true) + { + var newValue = (ushort) (current | value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } static int InterlockedAnd(ref int location, int value) { var current = location; diff --git a/src/Split/netcoreapp2.0/InterlockedPolyfill.cs b/src/Split/netcoreapp2.0/InterlockedPolyfill.cs index 2dabf01b..8ca0a457 100644 --- a/src/Split/netcoreapp2.0/InterlockedPolyfill.cs +++ b/src/Split/netcoreapp2.0/InterlockedPolyfill.cs @@ -17,6 +17,20 @@ static partial class Polyfill public static T And(ref T location1, T value) where T : struct { + if (Unsafe.SizeOf() == sizeof(byte)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedAnd(ref loc, val); + return Unsafe.As(ref result); + } + if (Unsafe.SizeOf() == sizeof(ushort)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedAnd(ref loc, val); + return Unsafe.As(ref result); + } if (Unsafe.SizeOf() == sizeof(int)) { ref var loc = ref Unsafe.As(ref location1); @@ -39,6 +53,20 @@ public static T And(ref T location1, T value) public static T Or(ref T location1, T value) where T : struct { + if (Unsafe.SizeOf() == sizeof(byte)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedOr(ref loc, val); + return Unsafe.As(ref result); + } + if (Unsafe.SizeOf() == sizeof(ushort)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedOr(ref loc, val); + return Unsafe.As(ref result); + } if (Unsafe.SizeOf() == sizeof(int)) { ref var loc = ref Unsafe.As(ref location1); @@ -56,6 +84,62 @@ public static T Or(ref T location1, T value) throw new System.NotSupportedException(); } } + static byte InterlockedAnd(ref byte location, byte value) + { + var current = location; + while (true) + { + var newValue = (byte) (current & value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static ushort InterlockedAnd(ref ushort location, ushort value) + { + var current = location; + while (true) + { + var newValue = (ushort) (current & value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static byte InterlockedOr(ref byte location, byte value) + { + var current = location; + while (true) + { + var newValue = (byte) (current | value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static ushort InterlockedOr(ref ushort location, ushort value) + { + var current = location; + while (true) + { + var newValue = (ushort) (current | value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } static int InterlockedAnd(ref int location, int value) { var current = location; diff --git a/src/Split/netcoreapp2.1/InterlockedPolyfill.cs b/src/Split/netcoreapp2.1/InterlockedPolyfill.cs index 2dabf01b..8ca0a457 100644 --- a/src/Split/netcoreapp2.1/InterlockedPolyfill.cs +++ b/src/Split/netcoreapp2.1/InterlockedPolyfill.cs @@ -17,6 +17,20 @@ static partial class Polyfill public static T And(ref T location1, T value) where T : struct { + if (Unsafe.SizeOf() == sizeof(byte)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedAnd(ref loc, val); + return Unsafe.As(ref result); + } + if (Unsafe.SizeOf() == sizeof(ushort)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedAnd(ref loc, val); + return Unsafe.As(ref result); + } if (Unsafe.SizeOf() == sizeof(int)) { ref var loc = ref Unsafe.As(ref location1); @@ -39,6 +53,20 @@ public static T And(ref T location1, T value) public static T Or(ref T location1, T value) where T : struct { + if (Unsafe.SizeOf() == sizeof(byte)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedOr(ref loc, val); + return Unsafe.As(ref result); + } + if (Unsafe.SizeOf() == sizeof(ushort)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedOr(ref loc, val); + return Unsafe.As(ref result); + } if (Unsafe.SizeOf() == sizeof(int)) { ref var loc = ref Unsafe.As(ref location1); @@ -56,6 +84,62 @@ public static T Or(ref T location1, T value) throw new System.NotSupportedException(); } } + static byte InterlockedAnd(ref byte location, byte value) + { + var current = location; + while (true) + { + var newValue = (byte) (current & value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static ushort InterlockedAnd(ref ushort location, ushort value) + { + var current = location; + while (true) + { + var newValue = (ushort) (current & value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static byte InterlockedOr(ref byte location, byte value) + { + var current = location; + while (true) + { + var newValue = (byte) (current | value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static ushort InterlockedOr(ref ushort location, ushort value) + { + var current = location; + while (true) + { + var newValue = (ushort) (current | value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } static int InterlockedAnd(ref int location, int value) { var current = location; diff --git a/src/Split/netcoreapp2.2/InterlockedPolyfill.cs b/src/Split/netcoreapp2.2/InterlockedPolyfill.cs index 2dabf01b..8ca0a457 100644 --- a/src/Split/netcoreapp2.2/InterlockedPolyfill.cs +++ b/src/Split/netcoreapp2.2/InterlockedPolyfill.cs @@ -17,6 +17,20 @@ static partial class Polyfill public static T And(ref T location1, T value) where T : struct { + if (Unsafe.SizeOf() == sizeof(byte)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedAnd(ref loc, val); + return Unsafe.As(ref result); + } + if (Unsafe.SizeOf() == sizeof(ushort)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedAnd(ref loc, val); + return Unsafe.As(ref result); + } if (Unsafe.SizeOf() == sizeof(int)) { ref var loc = ref Unsafe.As(ref location1); @@ -39,6 +53,20 @@ public static T And(ref T location1, T value) public static T Or(ref T location1, T value) where T : struct { + if (Unsafe.SizeOf() == sizeof(byte)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedOr(ref loc, val); + return Unsafe.As(ref result); + } + if (Unsafe.SizeOf() == sizeof(ushort)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedOr(ref loc, val); + return Unsafe.As(ref result); + } if (Unsafe.SizeOf() == sizeof(int)) { ref var loc = ref Unsafe.As(ref location1); @@ -56,6 +84,62 @@ public static T Or(ref T location1, T value) throw new System.NotSupportedException(); } } + static byte InterlockedAnd(ref byte location, byte value) + { + var current = location; + while (true) + { + var newValue = (byte) (current & value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static ushort InterlockedAnd(ref ushort location, ushort value) + { + var current = location; + while (true) + { + var newValue = (ushort) (current & value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static byte InterlockedOr(ref byte location, byte value) + { + var current = location; + while (true) + { + var newValue = (byte) (current | value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static ushort InterlockedOr(ref ushort location, ushort value) + { + var current = location; + while (true) + { + var newValue = (ushort) (current | value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } static int InterlockedAnd(ref int location, int value) { var current = location; diff --git a/src/Split/netcoreapp3.0/InterlockedPolyfill.cs b/src/Split/netcoreapp3.0/InterlockedPolyfill.cs index 2dabf01b..8ca0a457 100644 --- a/src/Split/netcoreapp3.0/InterlockedPolyfill.cs +++ b/src/Split/netcoreapp3.0/InterlockedPolyfill.cs @@ -17,6 +17,20 @@ static partial class Polyfill public static T And(ref T location1, T value) where T : struct { + if (Unsafe.SizeOf() == sizeof(byte)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedAnd(ref loc, val); + return Unsafe.As(ref result); + } + if (Unsafe.SizeOf() == sizeof(ushort)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedAnd(ref loc, val); + return Unsafe.As(ref result); + } if (Unsafe.SizeOf() == sizeof(int)) { ref var loc = ref Unsafe.As(ref location1); @@ -39,6 +53,20 @@ public static T And(ref T location1, T value) public static T Or(ref T location1, T value) where T : struct { + if (Unsafe.SizeOf() == sizeof(byte)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedOr(ref loc, val); + return Unsafe.As(ref result); + } + if (Unsafe.SizeOf() == sizeof(ushort)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedOr(ref loc, val); + return Unsafe.As(ref result); + } if (Unsafe.SizeOf() == sizeof(int)) { ref var loc = ref Unsafe.As(ref location1); @@ -56,6 +84,62 @@ public static T Or(ref T location1, T value) throw new System.NotSupportedException(); } } + static byte InterlockedAnd(ref byte location, byte value) + { + var current = location; + while (true) + { + var newValue = (byte) (current & value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static ushort InterlockedAnd(ref ushort location, ushort value) + { + var current = location; + while (true) + { + var newValue = (ushort) (current & value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static byte InterlockedOr(ref byte location, byte value) + { + var current = location; + while (true) + { + var newValue = (byte) (current | value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static ushort InterlockedOr(ref ushort location, ushort value) + { + var current = location; + while (true) + { + var newValue = (ushort) (current | value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } static int InterlockedAnd(ref int location, int value) { var current = location; diff --git a/src/Split/netcoreapp3.1/InterlockedPolyfill.cs b/src/Split/netcoreapp3.1/InterlockedPolyfill.cs index 2dabf01b..8ca0a457 100644 --- a/src/Split/netcoreapp3.1/InterlockedPolyfill.cs +++ b/src/Split/netcoreapp3.1/InterlockedPolyfill.cs @@ -17,6 +17,20 @@ static partial class Polyfill public static T And(ref T location1, T value) where T : struct { + if (Unsafe.SizeOf() == sizeof(byte)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedAnd(ref loc, val); + return Unsafe.As(ref result); + } + if (Unsafe.SizeOf() == sizeof(ushort)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedAnd(ref loc, val); + return Unsafe.As(ref result); + } if (Unsafe.SizeOf() == sizeof(int)) { ref var loc = ref Unsafe.As(ref location1); @@ -39,6 +53,20 @@ public static T And(ref T location1, T value) public static T Or(ref T location1, T value) where T : struct { + if (Unsafe.SizeOf() == sizeof(byte)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedOr(ref loc, val); + return Unsafe.As(ref result); + } + if (Unsafe.SizeOf() == sizeof(ushort)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedOr(ref loc, val); + return Unsafe.As(ref result); + } if (Unsafe.SizeOf() == sizeof(int)) { ref var loc = ref Unsafe.As(ref location1); @@ -56,6 +84,62 @@ public static T Or(ref T location1, T value) throw new System.NotSupportedException(); } } + static byte InterlockedAnd(ref byte location, byte value) + { + var current = location; + while (true) + { + var newValue = (byte) (current & value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static ushort InterlockedAnd(ref ushort location, ushort value) + { + var current = location; + while (true) + { + var newValue = (ushort) (current & value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static byte InterlockedOr(ref byte location, byte value) + { + var current = location; + while (true) + { + var newValue = (byte) (current | value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static ushort InterlockedOr(ref ushort location, ushort value) + { + var current = location; + while (true) + { + var newValue = (ushort) (current | value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } static int InterlockedAnd(ref int location, int value) { var current = location; diff --git a/src/Split/netstandard2.0/InterlockedPolyfill.cs b/src/Split/netstandard2.0/InterlockedPolyfill.cs index 2dabf01b..8ca0a457 100644 --- a/src/Split/netstandard2.0/InterlockedPolyfill.cs +++ b/src/Split/netstandard2.0/InterlockedPolyfill.cs @@ -17,6 +17,20 @@ static partial class Polyfill public static T And(ref T location1, T value) where T : struct { + if (Unsafe.SizeOf() == sizeof(byte)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedAnd(ref loc, val); + return Unsafe.As(ref result); + } + if (Unsafe.SizeOf() == sizeof(ushort)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedAnd(ref loc, val); + return Unsafe.As(ref result); + } if (Unsafe.SizeOf() == sizeof(int)) { ref var loc = ref Unsafe.As(ref location1); @@ -39,6 +53,20 @@ public static T And(ref T location1, T value) public static T Or(ref T location1, T value) where T : struct { + if (Unsafe.SizeOf() == sizeof(byte)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedOr(ref loc, val); + return Unsafe.As(ref result); + } + if (Unsafe.SizeOf() == sizeof(ushort)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedOr(ref loc, val); + return Unsafe.As(ref result); + } if (Unsafe.SizeOf() == sizeof(int)) { ref var loc = ref Unsafe.As(ref location1); @@ -56,6 +84,62 @@ public static T Or(ref T location1, T value) throw new System.NotSupportedException(); } } + static byte InterlockedAnd(ref byte location, byte value) + { + var current = location; + while (true) + { + var newValue = (byte) (current & value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static ushort InterlockedAnd(ref ushort location, ushort value) + { + var current = location; + while (true) + { + var newValue = (ushort) (current & value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static byte InterlockedOr(ref byte location, byte value) + { + var current = location; + while (true) + { + var newValue = (byte) (current | value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static ushort InterlockedOr(ref ushort location, ushort value) + { + var current = location; + while (true) + { + var newValue = (ushort) (current | value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } static int InterlockedAnd(ref int location, int value) { var current = location; diff --git a/src/Split/netstandard2.1/InterlockedPolyfill.cs b/src/Split/netstandard2.1/InterlockedPolyfill.cs index 2dabf01b..8ca0a457 100644 --- a/src/Split/netstandard2.1/InterlockedPolyfill.cs +++ b/src/Split/netstandard2.1/InterlockedPolyfill.cs @@ -17,6 +17,20 @@ static partial class Polyfill public static T And(ref T location1, T value) where T : struct { + if (Unsafe.SizeOf() == sizeof(byte)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedAnd(ref loc, val); + return Unsafe.As(ref result); + } + if (Unsafe.SizeOf() == sizeof(ushort)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedAnd(ref loc, val); + return Unsafe.As(ref result); + } if (Unsafe.SizeOf() == sizeof(int)) { ref var loc = ref Unsafe.As(ref location1); @@ -39,6 +53,20 @@ public static T And(ref T location1, T value) public static T Or(ref T location1, T value) where T : struct { + if (Unsafe.SizeOf() == sizeof(byte)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedOr(ref loc, val); + return Unsafe.As(ref result); + } + if (Unsafe.SizeOf() == sizeof(ushort)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedOr(ref loc, val); + return Unsafe.As(ref result); + } if (Unsafe.SizeOf() == sizeof(int)) { ref var loc = ref Unsafe.As(ref location1); @@ -56,6 +84,62 @@ public static T Or(ref T location1, T value) throw new System.NotSupportedException(); } } + static byte InterlockedAnd(ref byte location, byte value) + { + var current = location; + while (true) + { + var newValue = (byte) (current & value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static ushort InterlockedAnd(ref ushort location, ushort value) + { + var current = location; + while (true) + { + var newValue = (ushort) (current & value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static byte InterlockedOr(ref byte location, byte value) + { + var current = location; + while (true) + { + var newValue = (byte) (current | value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static ushort InterlockedOr(ref ushort location, ushort value) + { + var current = location; + while (true) + { + var newValue = (ushort) (current | value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } static int InterlockedAnd(ref int location, int value) { var current = location; diff --git a/src/Split/uap10.0/InterlockedPolyfill.cs b/src/Split/uap10.0/InterlockedPolyfill.cs index 2dabf01b..8ca0a457 100644 --- a/src/Split/uap10.0/InterlockedPolyfill.cs +++ b/src/Split/uap10.0/InterlockedPolyfill.cs @@ -17,6 +17,20 @@ static partial class Polyfill public static T And(ref T location1, T value) where T : struct { + if (Unsafe.SizeOf() == sizeof(byte)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedAnd(ref loc, val); + return Unsafe.As(ref result); + } + if (Unsafe.SizeOf() == sizeof(ushort)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedAnd(ref loc, val); + return Unsafe.As(ref result); + } if (Unsafe.SizeOf() == sizeof(int)) { ref var loc = ref Unsafe.As(ref location1); @@ -39,6 +53,20 @@ public static T And(ref T location1, T value) public static T Or(ref T location1, T value) where T : struct { + if (Unsafe.SizeOf() == sizeof(byte)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedOr(ref loc, val); + return Unsafe.As(ref result); + } + if (Unsafe.SizeOf() == sizeof(ushort)) + { + ref var loc = ref Unsafe.As(ref location1); + var val = Unsafe.As(ref value); + var result = InterlockedOr(ref loc, val); + return Unsafe.As(ref result); + } if (Unsafe.SizeOf() == sizeof(int)) { ref var loc = ref Unsafe.As(ref location1); @@ -56,6 +84,62 @@ public static T Or(ref T location1, T value) throw new System.NotSupportedException(); } } + static byte InterlockedAnd(ref byte location, byte value) + { + var current = location; + while (true) + { + var newValue = (byte) (current & value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static ushort InterlockedAnd(ref ushort location, ushort value) + { + var current = location; + while (true) + { + var newValue = (ushort) (current & value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static byte InterlockedOr(ref byte location, byte value) + { + var current = location; + while (true) + { + var newValue = (byte) (current | value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } + static ushort InterlockedOr(ref ushort location, ushort value) + { + var current = location; + while (true) + { + var newValue = (ushort) (current | value); + var oldValue = Interlocked.CompareExchange(ref location, newValue, current); + if (oldValue == current) + { + return oldValue; + } + current = oldValue; + } + } static int InterlockedAnd(ref int location, int value) { var current = location; diff --git a/src/Tests/PolyfillTests_Interlocked.cs b/src/Tests/PolyfillTests_Interlocked.cs index 6cb02567..3613fbe0 100644 --- a/src/Tests/PolyfillTests_Interlocked.cs +++ b/src/Tests/PolyfillTests_Interlocked.cs @@ -64,6 +64,97 @@ public async Task InterlockedOrEnum() await Assert.That(original).IsEqualTo(Permissions.Read); await Assert.That(permissions).IsEqualTo(Permissions.Read | Permissions.Write); } + + [Flags] + enum ByteFlags : byte + { + None = 0, + A = 1, + B = 2, + High = 0x80 + } + + [Flags] + enum ShortFlags : short + { + None = 0, + A = 1, + B = 2, + High = 0x4000 + } + + [Test] + public async Task InterlockedAndByteEnum() + { + var value = ByteFlags.A | ByteFlags.B | ByteFlags.High; + var original = Interlocked.And(ref value, ByteFlags.A | ByteFlags.High); + await Assert.That(original).IsEqualTo(ByteFlags.A | ByteFlags.B | ByteFlags.High); + await Assert.That(value).IsEqualTo(ByteFlags.A | ByteFlags.High); + } + + [Test] + public async Task InterlockedOrByteEnum() + { + var value = ByteFlags.A; + var original = Interlocked.Or(ref value, ByteFlags.High); + await Assert.That(original).IsEqualTo(ByteFlags.A); + await Assert.That(value).IsEqualTo(ByteFlags.A | ByteFlags.High); + } + + [Test] + public async Task InterlockedAndShortEnum() + { + var value = ShortFlags.A | ShortFlags.B | ShortFlags.High; + var original = Interlocked.And(ref value, ShortFlags.B | ShortFlags.High); + await Assert.That(original).IsEqualTo(ShortFlags.A | ShortFlags.B | ShortFlags.High); + await Assert.That(value).IsEqualTo(ShortFlags.B | ShortFlags.High); + } + + [Test] + public async Task InterlockedOrShortEnum() + { + var value = ShortFlags.A; + var original = Interlocked.Or(ref value, ShortFlags.High); + await Assert.That(original).IsEqualTo(ShortFlags.A); + await Assert.That(value).IsEqualTo(ShortFlags.A | ShortFlags.High); + } + + [Test] + public async Task InterlockedAndOrSmallIntegers() + { + byte byteValue = 0xFF; + await Assert.That(Interlocked.And(ref byteValue, 0x0F)).IsEqualTo((byte) 0xFF); + await Assert.That(byteValue).IsEqualTo((byte) 0x0F); + await Assert.That(Interlocked.Or(ref byteValue, 0xF0)).IsEqualTo((byte) 0x0F); + await Assert.That(byteValue).IsEqualTo((byte) 0xFF); + + sbyte sbyteValue = -1; + await Assert.That(Interlocked.And(ref sbyteValue, 0x0F)).IsEqualTo((sbyte) -1); + await Assert.That(sbyteValue).IsEqualTo((sbyte) 0x0F); + await Assert.That(Interlocked.Or(ref sbyteValue, -128)).IsEqualTo((sbyte) 0x0F); + await Assert.That(sbyteValue).IsEqualTo((sbyte) -113); + + short shortValue = -1; + await Assert.That(Interlocked.And(ref shortValue, 0x0FF0)).IsEqualTo((short) -1); + await Assert.That(shortValue).IsEqualTo((short) 0x0FF0); + await Assert.That(Interlocked.Or(ref shortValue, 0x000F)).IsEqualTo((short) 0x0FF0); + await Assert.That(shortValue).IsEqualTo((short) 0x0FFF); + + ushort ushortValue = 0xFFFF; + await Assert.That(Interlocked.And(ref ushortValue, 0x0FF0)).IsEqualTo((ushort) 0xFFFF); + await Assert.That(ushortValue).IsEqualTo((ushort) 0x0FF0); + await Assert.That(Interlocked.Or(ref ushortValue, 0xF000)).IsEqualTo((ushort) 0x0FF0); + await Assert.That(ushortValue).IsEqualTo((ushort) 0xFFF0); + } + + [Test] + public async Task InterlockedAndDoesNotDisturbAdjacentBytes() + { + var values = new byte[] {0xAA, 0xFF, 0xBB}; + var original = Interlocked.And(ref values[1], 0x0F); + await Assert.That(original).IsEqualTo((byte) 0xFF); + await Assert.That(values).IsEquivalentTo(new byte[] {0xAA, 0x0F, 0xBB}); + } } #endif