Skip to content

Commit

Permalink
Merge pull request #669 from shangfengh/new
Browse files Browse the repository at this point in the history
feat(SafeValue): ✨ add some SafeValue
  • Loading branch information
shangfengh authored Oct 27, 2023
2 parents 21af5d6 + b0e9d0d commit ea69149
Show file tree
Hide file tree
Showing 5 changed files with 548 additions and 137 deletions.
4 changes: 2 additions & 2 deletions logic/GameClass/GameObj/Map/Chest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public Chest(XY initPos) :
private readonly Gadget[] propInChest = new Gadget[GameData.maxNumOfPropInChest] { new NullProp(), new NullProp() };
public Gadget[] PropInChest => propInChest;

private TimeBasedProgressForInterrupting openProgress = new TimeBasedProgressForInterrupting();
public TimeBasedProgressForInterrupting OpenProgress { get => openProgress; }
private TimeBasedProgressOptimizedForInterrupting openProgress = new TimeBasedProgressOptimizedForInterrupting();
public TimeBasedProgressOptimizedForInterrupting OpenProgress { get => openProgress; }
}
}
14 changes: 12 additions & 2 deletions logic/GameClass/GameObj/ObjOfCharacter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,27 @@ public ICharacter? Parent
{
get
{
lock (objOfCharacterReaderWriterLock)
objOfCharacterReaderWriterLock.EnterReadLock();
try
{
return parent;
}
finally
{
objOfCharacterReaderWriterLock.ExitReadLock();
}
}
set
{
lock (objOfCharacterReaderWriterLock)
objOfCharacterReaderWriterLock.EnterWriteLock();
try
{
parent = value;
}
finally
{
objOfCharacterReaderWriterLock.ExitWriteLock();
}
}
}
// LHR注:本来考虑在构造函数里设置parent属性,见THUAI4在游戏引擎中才设置该属性,作罢。——2021/9/24
Expand Down
47 changes: 35 additions & 12 deletions logic/Preparation/Utility/SafeValue/Atomic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
namespace Preparation.Utility
{
//其对应属性不应当有set访问器,避免不安全的=赋值
public class AtomicInt
public abstract class Atomic
{
}

public class AtomicInt : Atomic
{
private int v;
public AtomicInt(int x)
Expand All @@ -24,7 +28,7 @@ public AtomicInt(int x)
public int CompareExReturnOri(int newV, int compareTo) => Interlocked.CompareExchange(ref v, newV, compareTo);
}

public class AtomicLong
public class AtomicLong : Atomic
{
private long v;
public AtomicLong(long x)
Expand All @@ -33,7 +37,7 @@ public AtomicLong(long x)
}
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);
Expand All @@ -44,24 +48,43 @@ public AtomicLong(long x)
public long CompareExReturnOri(long newV, long compareTo) => Interlocked.CompareExchange(ref v, newV, compareTo);
}

public class AtomicBool
public class AtomicDouble : Atomic
{
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 : Atomic
{
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;
}
}
Loading

0 comments on commit ea69149

Please sign in to comment.