Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions src/Polyfill/InterlockedPolyfill.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,22 @@ static partial class Polyfill
public static T And<T>(ref T location1, T value)
where T : struct
{
if (Unsafe.SizeOf<T>() == sizeof(byte))
{
ref var loc = ref Unsafe.As<T, byte>(ref location1);
var val = Unsafe.As<T, byte>(ref value);
var result = InterlockedAnd(ref loc, val);
return Unsafe.As<byte, T>(ref result);
}

if (Unsafe.SizeOf<T>() == sizeof(ushort))
{
ref var loc = ref Unsafe.As<T, ushort>(ref location1);
var val = Unsafe.As<T, ushort>(ref value);
var result = InterlockedAnd(ref loc, val);
return Unsafe.As<ushort, T>(ref result);
}

if (Unsafe.SizeOf<T>() == sizeof(int))
{
ref var loc = ref Unsafe.As<T, int>(ref location1);
Expand All @@ -46,6 +62,22 @@ public static T And<T>(ref T location1, T value)
public static T Or<T>(ref T location1, T value)
where T : struct
{
if (Unsafe.SizeOf<T>() == sizeof(byte))
{
ref var loc = ref Unsafe.As<T, byte>(ref location1);
var val = Unsafe.As<T, byte>(ref value);
var result = InterlockedOr(ref loc, val);
return Unsafe.As<byte, T>(ref result);
}

if (Unsafe.SizeOf<T>() == sizeof(ushort))
{
ref var loc = ref Unsafe.As<T, ushort>(ref location1);
var val = Unsafe.As<T, ushort>(ref value);
var result = InterlockedOr(ref loc, val);
return Unsafe.As<ushort, T>(ref result);
}

if (Unsafe.SizeOf<T>() == sizeof(int))
{
ref var loc = ref Unsafe.As<T, int>(ref location1);
Expand All @@ -66,6 +98,70 @@ public static T Or<T>(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;
Expand Down
84 changes: 84 additions & 0 deletions src/Split/net10.0/InterlockedPolyfill.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@ static partial class Polyfill
public static T And<T>(ref T location1, T value)
where T : struct
{
if (Unsafe.SizeOf<T>() == sizeof(byte))
{
ref var loc = ref Unsafe.As<T, byte>(ref location1);
var val = Unsafe.As<T, byte>(ref value);
var result = InterlockedAnd(ref loc, val);
return Unsafe.As<byte, T>(ref result);
}
if (Unsafe.SizeOf<T>() == sizeof(ushort))
{
ref var loc = ref Unsafe.As<T, ushort>(ref location1);
var val = Unsafe.As<T, ushort>(ref value);
var result = InterlockedAnd(ref loc, val);
return Unsafe.As<ushort, T>(ref result);
}
if (Unsafe.SizeOf<T>() == sizeof(int))
{
ref var loc = ref Unsafe.As<T, int>(ref location1);
Expand All @@ -39,6 +53,20 @@ public static T And<T>(ref T location1, T value)
public static T Or<T>(ref T location1, T value)
where T : struct
{
if (Unsafe.SizeOf<T>() == sizeof(byte))
{
ref var loc = ref Unsafe.As<T, byte>(ref location1);
var val = Unsafe.As<T, byte>(ref value);
var result = InterlockedOr(ref loc, val);
return Unsafe.As<byte, T>(ref result);
}
if (Unsafe.SizeOf<T>() == sizeof(ushort))
{
ref var loc = ref Unsafe.As<T, ushort>(ref location1);
var val = Unsafe.As<T, ushort>(ref value);
var result = InterlockedOr(ref loc, val);
return Unsafe.As<ushort, T>(ref result);
}
if (Unsafe.SizeOf<T>() == sizeof(int))
{
ref var loc = ref Unsafe.As<T, int>(ref location1);
Expand All @@ -56,6 +84,62 @@ public static T Or<T>(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;
Expand Down
84 changes: 84 additions & 0 deletions src/Split/net461/InterlockedPolyfill.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@ static partial class Polyfill
public static T And<T>(ref T location1, T value)
where T : struct
{
if (Unsafe.SizeOf<T>() == sizeof(byte))
{
ref var loc = ref Unsafe.As<T, byte>(ref location1);
var val = Unsafe.As<T, byte>(ref value);
var result = InterlockedAnd(ref loc, val);
return Unsafe.As<byte, T>(ref result);
}
if (Unsafe.SizeOf<T>() == sizeof(ushort))
{
ref var loc = ref Unsafe.As<T, ushort>(ref location1);
var val = Unsafe.As<T, ushort>(ref value);
var result = InterlockedAnd(ref loc, val);
return Unsafe.As<ushort, T>(ref result);
}
if (Unsafe.SizeOf<T>() == sizeof(int))
{
ref var loc = ref Unsafe.As<T, int>(ref location1);
Expand All @@ -39,6 +53,20 @@ public static T And<T>(ref T location1, T value)
public static T Or<T>(ref T location1, T value)
where T : struct
{
if (Unsafe.SizeOf<T>() == sizeof(byte))
{
ref var loc = ref Unsafe.As<T, byte>(ref location1);
var val = Unsafe.As<T, byte>(ref value);
var result = InterlockedOr(ref loc, val);
return Unsafe.As<byte, T>(ref result);
}
if (Unsafe.SizeOf<T>() == sizeof(ushort))
{
ref var loc = ref Unsafe.As<T, ushort>(ref location1);
var val = Unsafe.As<T, ushort>(ref value);
var result = InterlockedOr(ref loc, val);
return Unsafe.As<ushort, T>(ref result);
}
if (Unsafe.SizeOf<T>() == sizeof(int))
{
ref var loc = ref Unsafe.As<T, int>(ref location1);
Expand All @@ -56,6 +84,62 @@ public static T Or<T>(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;
Expand Down
Loading
Loading