Browse Source

feat(SafeValue): add the SafeValue AtomicDouble

dev
shangfengh 2 years ago
parent
commit
491d5080eb
3 changed files with 81 additions and 14 deletions
  1. +28
    -9
      logic/Preparation/Utility/SafeValue/Atomic.cs
  2. +48
    -0
      logic/Preparation/Utility/SafeValue/InTheRange.cs
  3. +5
    -5
      logic/Preparation/Utility/SafeValue/TimeBased.cs

+ 28
- 9
logic/Preparation/Utility/SafeValue/Atomic.cs View File

@@ -33,7 +33,7 @@ namespace Preparation.Utility
}
public override string ToString() => Interlocked.Read(ref v).ToString();
public long Get() => Interlocked.Read(ref v);
public static implicit operator long(AtomicLong aint) => Interlocked.Read(ref aint.v);
public static implicit operator long(AtomicLong along) => Interlocked.Read(ref along.v);
/// <returns>返回操作前的值</returns>
public long SetReturnOri(long value) => Interlocked.Exchange(ref v, value);
public long Add(long x) => Interlocked.Add(ref v, x);
@@ -44,24 +44,43 @@ namespace Preparation.Utility
public long CompareExReturnOri(long newV, long compareTo) => Interlocked.CompareExchange(ref v, newV, compareTo);
}

public class AtomicDouble
{
private double v;
public AtomicDouble(double x)
{
v = x;
}
public override string ToString() => Interlocked.CompareExchange(ref v, -2.0, -2.0).ToString();
public double Get() => Interlocked.CompareExchange(ref v, -2.0, -2.0);
public static implicit operator double(AtomicDouble adouble) => Interlocked.CompareExchange(ref adouble.v, -2.0, -2.0);
/// <returns>返回操作前的值</returns>
public double SetReturnOri(double value) => Interlocked.Exchange(ref v, value);
/// <returns>返回操作前的值</returns>
public double CompareExReturnOri(double newV, double compareTo) => Interlocked.CompareExchange(ref v, newV, compareTo);
}

public class AtomicBool
{
private int v;//v==0为false,v==1为true
private int v;//v&1==0为false,v&1==1为true
public AtomicBool(bool x)
{
v = x ? 1 : 0;
}
public override string ToString() => (Interlocked.CompareExchange(ref v, -2, -2) == 0) ? "false" : "true";
public bool Get() => (Interlocked.CompareExchange(ref v, -1, -1) != 0);
public static implicit operator bool(AtomicBool abool) => (Interlocked.CompareExchange(ref abool.v, -1, -1) != 0);
public override string ToString() => ((Interlocked.CompareExchange(ref v, -2, -2) & 1) == 0) ? "false" : "true";
public bool Get() => ((Interlocked.CompareExchange(ref v, -2, -2) & 1) == 1);
public static implicit operator bool(AtomicBool abool) => abool.Get();
/// <returns>返回操作前的值</returns>
public bool SetReturnOri(bool value) => (Interlocked.Exchange(ref v, value ? 1 : 0) != 0);
public bool SetReturnOri(bool value) => ((Interlocked.Exchange(ref v, value ? 1 : 0) & 1) == 1);
/// <returns>赋值前的值是否与将赋予的值不相同</returns>
public bool TrySet(bool value)
{
return (Interlocked.CompareExchange(ref v, value ? 1 : 0, value ? 0 : 1) ^ (value ? 1 : 0)) != 0;
return ((Interlocked.Exchange(ref v, value ? 1 : 0) & 1) != (value ? 1 : 0));
}
public bool And(bool x) => Interlocked.And(ref v, x ? 1 : 0) != 0;
public bool Or(bool x) => Interlocked.Or(ref v, x ? 1 : 0) != 0;
public bool And(bool x) => (Interlocked.And(ref v, x ? 1 : 0) & 1) == 1;
public bool Or(bool x) => (Interlocked.Or(ref v, x ? 1 : 0) & 1) == 1;
/// <returns>返回操作后的值</returns>
public bool Reverse() => (Interlocked.Increment(ref v) & 1) == 1;
public bool Xor(bool x) => (Interlocked.Add(ref v, x ? 1 : 0) & 1) == 1;
}
}

+ 48
- 0
logic/Preparation/Utility/SafeValue/InTheRange.cs View File

@@ -152,6 +152,19 @@ namespace Preparation.Utility
else v *= mulV;
}
}
public void MulV(double mulV)
{
if (mulV <= 0)
{
lock (vLock) v = 0;
return;
}
lock (vLock)
{
if (v > maxV / mulV) v = maxV; //避免溢出
else v = (int)(v * mulV);
}
}
/// <summary>
/// 应当保证乘数大于0
/// </summary>
@@ -163,6 +176,17 @@ namespace Preparation.Utility
else v *= mulPositiveV;
}
}
/// <summary>
/// 应当保证乘数大于0
/// </summary>
public void MulPositiveV(double mulPositiveV)
{
lock (vLock)
{
if (v > maxV / mulPositiveV) v = maxV; //避免溢出
else v = (int)(v * mulPositiveV);
}
}
/// <returns>返回实际改变量</returns>
public int SubV(int subV)
{
@@ -403,6 +427,19 @@ namespace Preparation.Utility
else v *= mulV;
}
}
public void MulV(double mulV)
{
if (mulV <= 0)
{
lock (vLock) v = 0;
return;
}
lock (vLock)
{
if (v > maxV / mulV) v = maxV; //避免溢出
else v = (long)(v * mulV);
}
}
/// <summary>
/// 应当保证乘数大于0
/// </summary>
@@ -414,6 +451,17 @@ namespace Preparation.Utility
else v *= mulPositiveV;
}
}
/// <summary>
/// 应当保证乘数大于0
/// </summary>
public void MulPositiveV(double mulPositiveV)
{
lock (vLock)
{
if (v > maxV / mulPositiveV) v = maxV; //避免溢出
else v = (long)(v * mulPositiveV);
}
}
/// <returns>返回实际改变量</returns>
public long SubV(long subV)
{


+ 5
- 5
logic/Preparation/Utility/SafeValue/TimeBased.cs View File

@@ -321,13 +321,12 @@ namespace Preparation.Utility
}
}*/

/*
public class TimeBasedProgressAtVariableSpeed
{
private long progress = 0;
private long lastStartT = long.MaxValue;
private readonly object progressLock = new();
private long needProgress;
private double speed;
public LongInTheVariableRange progress;
public StartTime startTime = new();
private speed =1.0;

public TimeBasedProgressAtVariableSpeed(long needProgress, double speed)
{
@@ -491,6 +490,7 @@ namespace Preparation.Utility
return false;
}
}
*/

/// <summary>
/// 冷却时间为可变的CDms的bool,不支持查看当前进度,初始为True


Loading…
Cancel
Save