📄 interlockedex.cs
字号:
namespace NCindy.Threading
{
using System;
using System.Runtime.InteropServices;
using System.Threading;
public static class InterlockedEx
{
public static int And(ref int target, int with)
{
int comparand;
int num2 = target;
do
{
comparand = num2;
num2 = Interlocked.CompareExchange(ref target, comparand & with, comparand);
}
while (comparand != num2);
return num2;
}
public static bool BitTestAndCompliment(ref int target, int bitNum)
{
int with = ((int) 1) << bitNum;
return ((Xor(ref target, with) & with) != 0);
}
public static bool BitTestAndReset(ref int target, int bitNum)
{
int num = ((int) 1) << bitNum;
return ((And(ref target, ~num) & num) != 0);
}
public static bool BitTestAndSet(ref int target, int bitNum)
{
int with = ((int) 1) << bitNum;
return ((Or(ref target, with) & with) != 0);
}
public static bool IfThen<T>(ref T val, T @if, T then) where T: class
{
return (Interlocked.CompareExchange<T>(ref val, then, @if) == @if);
}
public static bool IfThen(ref int val, int @if, int then)
{
return (Interlocked.CompareExchange(ref val, then, @if) == @if);
}
public static bool IfThen(ref int val, int @if, int then, out int prevVal)
{
prevVal = Interlocked.CompareExchange(ref val, then, @if);
return (prevVal == @if);
}
public static bool IfThen<T>(ref T val, T @if, T then, out T prevVal) where T: class
{
prevVal = Interlocked.CompareExchange<T>(ref val, then, @if);
return (((T) prevVal) == @if);
}
public static int MaskedAdd(ref int target, int value, int mask)
{
int comparand;
int num2 = target;
do
{
comparand = num2 & mask;
num2 = Interlocked.CompareExchange(ref target, comparand + value, comparand);
}
while (comparand != num2);
return num2;
}
public static int MaskedAnd(ref int target, int with, int mask)
{
int comparand;
int num2 = target;
do
{
comparand = num2 & mask;
num2 = Interlocked.CompareExchange(ref target, comparand & with, comparand);
}
while (comparand != num2);
return num2;
}
public static int MaskedExchange(ref int target, int mask, int value)
{
int num;
int comparand = target;
do
{
num = comparand;
comparand = Interlocked.CompareExchange(ref target, (num & ~mask) | value, comparand);
}
while (num != comparand);
return comparand;
}
public static int MaskedOr(ref int target, int with, int mask)
{
int comparand;
int num2 = target;
do
{
comparand = num2 & mask;
num2 = Interlocked.CompareExchange(ref target, comparand | with, comparand);
}
while (comparand != num2);
return num2;
}
public static int MaskedXor(ref int target, int with, int mask)
{
int comparand;
int num2 = target;
do
{
comparand = num2 & mask;
num2 = Interlocked.CompareExchange(ref target, comparand ^ with, comparand);
}
while (comparand != num2);
return num2;
}
public static int Max(ref int target, int val)
{
int comparand;
int num2 = target;
do
{
comparand = num2;
num2 = Interlocked.CompareExchange(ref target, Math.Max(comparand, val), comparand);
}
while (comparand != num2);
return num2;
}
public static int Min(ref int target, int val)
{
int comparand;
int num2 = target;
do
{
comparand = num2;
num2 = Interlocked.CompareExchange(ref target, Math.Min(comparand, val), comparand);
}
while (comparand != num2);
return num2;
}
public static int Or(ref int target, int with)
{
int comparand;
int num2 = target;
do
{
comparand = num2;
num2 = Interlocked.CompareExchange(ref target, comparand | with, comparand);
}
while (comparand != num2);
return num2;
}
public static int Xor(ref int target, int with)
{
int comparand;
int num2 = target;
do
{
comparand = num2;
num2 = Interlocked.CompareExchange(ref target, comparand ^ with, comparand);
}
while (comparand != num2);
return num2;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -