From 42afd92370c176df586a3ff5828e10af71f619eb Mon Sep 17 00:00:00 2001 From: shangfengh <3495281661@qq.com> Date: Tue, 24 Oct 2023 16:25:41 +0800 Subject: [PATCH 1/4] feat: :sparkles: add the SafeValue StartTime --- logic/GameClass/GameObj/Map/Chest.cs | 4 +- .../Utility/SafeValue/InTheRange.cs | 236 ++++++----- .../Utility/SafeValue/TimeBased.cs | 380 +++++++++++++++++- 3 files changed, 505 insertions(+), 115 deletions(-) diff --git a/logic/GameClass/GameObj/Map/Chest.cs b/logic/GameClass/GameObj/Map/Chest.cs index aae11fa0..bf4e833b 100644 --- a/logic/GameClass/GameObj/Map/Chest.cs +++ b/logic/GameClass/GameObj/Map/Chest.cs @@ -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; } } } diff --git a/logic/Preparation/Utility/SafeValue/InTheRange.cs b/logic/Preparation/Utility/SafeValue/InTheRange.cs index 44595be6..1437e581 100644 --- a/logic/Preparation/Utility/SafeValue/InTheRange.cs +++ b/logic/Preparation/Utility/SafeValue/InTheRange.cs @@ -13,6 +13,7 @@ public class IntInTheVariableRange private int v; private int maxV; private readonly object vLock = new(); + #region 构造与读取 public IntInTheVariableRange(int value, int maxValue) { if (maxValue < 0) @@ -50,7 +51,10 @@ public override string ToString() public int GetValue() { lock (vLock) return v; } public static implicit operator int(IntInTheVariableRange aint) => aint.GetValue(); public int GetMaxV() { lock (vLock) return maxV; } + public bool IsMaxV() { lock (vLock) return v == maxV; } + #endregion + #region 普通设置MaxV与Value的值的方法 /// /// 若maxValue<=0则maxValue设为0并返回False /// @@ -82,29 +86,7 @@ public void SetPositiveMaxV(int maxValue) if (v > maxValue) v = maxValue; } } - /// - /// 如果当前值大于maxValue,则更新maxValue失败 - /// - public bool TrySetMaxV(int maxValue) - { - lock (vLock) - { - if (v > maxValue) return false; - maxV = maxValue; - return true; - } - } - /// - /// 应当保证该value>=0 - /// - public int SetPositiveV(int value) - { - lock (vLock) - { - return v = (value > maxV) ? maxV : value; - } - } public int SetV(int value) { if (value <= 0) @@ -119,6 +101,19 @@ public int SetV(int value) return v = (value > maxV) ? maxV : value; } } + /// + /// 应当保证该value>=0 + /// + public int SetPositiveV(int value) + { + lock (vLock) + { + return v = (value > maxV) ? maxV : value; + } + } + #endregion + + #region 普通运算 /// 返回实际改变量 public int AddV(int addV) { @@ -193,30 +188,26 @@ public int SubPositiveV(int subPositiveV) } return subPositiveV; } + #endregion + #region 特殊条件的设置MaxV与Value的值的方法 /// - /// 试图加到满,如果无法加到maxValue则不加并返回-1 + /// 如果当前值大于maxValue,则更新maxValue失败 /// - /// 返回实际改变量 - public int TryAddToMaxV(int addV) + public bool TrySetMaxV(int maxValue) { lock (vLock) { - if (maxV - v <= addV) - { - addV = maxV - v; - v = maxV; - return addV; - } - return -1; + if (v > maxValue) return false; + maxV = maxValue; + return true; } } - - public bool Set0IfNotMax() + public bool Set0IfNotMaxor0() { lock (vLock) { - if (v < maxV) + if (v < maxV && v > 0) { v = 0; return true; @@ -236,14 +227,27 @@ public bool Set0IfMax() } return false; } + #endregion - public bool IsMaxV() + #region 特殊条件的运算 + /// + /// 试图加到满,如果无法加到maxValue则不加并返回-1 + /// + /// 返回实际改变量 + public int TryAddToMaxV(int addV) { lock (vLock) { - return v == maxV; + if (maxV - v <= addV) + { + addV = maxV - v; + v = maxV; + return addV; + } + return -1; } } + #endregion } /// @@ -254,6 +258,7 @@ public class LongInTheVariableRange private long v; private long maxV; private readonly object vLock = new(); + #region 构造与读取 public LongInTheVariableRange(long value, long maxValue) { if (maxValue < 0) @@ -291,7 +296,16 @@ public override string ToString() public long GetValue() { lock (vLock) return v; } public static implicit operator long(LongInTheVariableRange aint) => aint.GetValue(); public long GetMaxV() { lock (vLock) return maxV; } + public bool IsMaxV() + { + lock (vLock) + { + return v == maxV; + } + } + #endregion + #region 普通设置MaxV与Value的值的方法 /// /// 若maxValue<=0则maxValue设为0并返回False /// @@ -323,29 +337,7 @@ public void SetPositiveMaxV(long maxValue) if (v > maxValue) v = maxValue; } } - /// - /// 如果当前值大于maxValue,则更新maxValue失败 - /// - public bool TrySetMaxV(long maxValue) - { - lock (vLock) - { - if (v > maxValue) return false; - maxV = maxValue; - return true; - } - } - /// - /// 应当保证该value>=0 - /// - public long SetPositiveV(long value) - { - lock (vLock) - { - return v = (value > maxV) ? maxV : value; - } - } public long SetV(long value) { if (value <= 0) @@ -360,6 +352,19 @@ public long SetV(long value) return v = (value > maxV) ? maxV : value; } } + /// + /// 应当保证该value>=0 + /// + public long SetPositiveV(long value) + { + lock (vLock) + { + return v = (value > maxV) ? maxV : value; + } + } + #endregion + + #region 普通运算 /// 返回实际改变量 public long AddV(long addV) { @@ -434,22 +439,19 @@ public long SubPositiveV(long subPositiveV) } return subPositiveV; } + #endregion + #region 特殊条件的设置MaxV与Value的值的方法 /// - /// 试图加到满,如果无法加到maxValue则不加并返回-1 + /// 如果当前值大于maxValue,则更新maxValue失败 /// - /// 返回实际改变量 - public long TryAddToMaxV(long addV) + public bool TrySetMaxV(long maxValue) { lock (vLock) { - if (maxV - v <= addV) - { - addV = maxV - v; - v = maxV; - return addV; - } - return -1; + if (v > maxValue) return false; + maxV = maxValue; + return true; } } @@ -477,14 +479,27 @@ public bool Set0IfMax() } return false; } + #endregion - public bool IsMaxV() + #region 特殊条件的运算 + /// + /// 试图加到满,如果无法加到maxValue则不加并返回-1 + /// + /// 返回实际改变量 + public long TryAddToMaxV(long addV) { lock (vLock) { - return v == maxV; + if (maxV - v <= addV) + { + addV = maxV - v; + v = maxV; + return addV; + } + return -1; } } + #endregion } /// @@ -495,6 +510,7 @@ public class DoubleInTheVariableRange private double v; private double maxV; private readonly object vLock = new(); + #region 构造与读取 public DoubleInTheVariableRange(double value, double maxValue) { if (maxValue < 0) @@ -532,7 +548,16 @@ public override string ToString() public double GetValue() { lock (vLock) return v; } public static implicit operator double(DoubleInTheVariableRange adouble) => adouble.GetValue(); public double GetMaxV() { lock (vLock) return maxV; } + public bool IsMaxV() + { + lock (vLock) + { + return v == maxV; + } + } + #endregion + #region 普通设置MaxV与Value的值的方法 /// /// 若maxValue<=0则maxValue设为0并返回False /// @@ -564,29 +589,7 @@ public void SetPositiveMaxV(double maxValue) if (v > maxValue) v = maxValue; } } - /// - /// 如果当前值大于maxValue,则更新maxValue失败 - /// - public bool TrySetMaxV(double maxValue) - { - lock (vLock) - { - if (v > maxValue) return false; - maxV = maxValue; - return true; - } - } - /// - /// 应当保证该value>=0 - /// - public double SetPositiveV(double value) - { - lock (vLock) - { - return v = (value > maxV) ? maxV : value; - } - } public double SetV(double value) { if (value <= 0) @@ -601,6 +604,19 @@ public double SetV(double value) return v = (value > maxV) ? maxV : value; } } + /// + /// 应当保证该value>=0 + /// + public double SetPositiveV(double value) + { + lock (vLock) + { + return v = (value > maxV) ? maxV : value; + } + } + #endregion + + #region 普通运算 /// 返回实际改变量 public double AddV(double addV) { @@ -675,22 +691,19 @@ public double SubPositiveV(double subPositiveV) } return subPositiveV; } + #endregion + #region 特殊条件的设置MaxV与Value的值的方法 /// - /// 试图加到满,如果无法加到maxValue则不加并返回-1 + /// 如果当前值大于maxValue,则更新maxValue失败 /// - /// 返回实际改变量 - public double TryAddToMaxV(double addV) + public bool TrySetMaxV(double maxValue) { lock (vLock) { - if (maxV - v <= addV) - { - addV = maxV - v; - v = maxV; - return addV; - } - return -1; + if (v > maxValue) return false; + maxV = maxValue; + return true; } } @@ -718,13 +731,26 @@ public bool Set0IfMax() } return false; } + #endregion - public bool IsMaxV() + #region 特殊条件的运算 + /// + /// 试图加到满,如果无法加到maxValue则不加并返回-1 + /// + /// 返回实际改变量 + public double TryAddToMaxV(double addV) { lock (vLock) { - return v == maxV; + if (maxV - v <= addV) + { + addV = maxV - v; + v = maxV; + return addV; + } + return -1; } } + #endregion } } \ No newline at end of file diff --git a/logic/Preparation/Utility/SafeValue/TimeBased.cs b/logic/Preparation/Utility/SafeValue/TimeBased.cs index 566441c2..7f340681 100644 --- a/logic/Preparation/Utility/SafeValue/TimeBased.cs +++ b/logic/Preparation/Utility/SafeValue/TimeBased.cs @@ -5,23 +5,47 @@ namespace Preparation.Utility { //其对应属性不应当有set访问器,避免不安全的=赋值 + /// + /// 记录上次Start的时间,尚未Start则为long.MaxValue + /// 当前不为long.MaxValue则不能Start + /// + public class StartTime + { + private long _time; + public StartTime(long time) + { + _time = time; + } + public StartTime() { _time = long.MaxValue; } + public long Get() => Interlocked.CompareExchange(ref _time, -2, -2); + /// 返回操作前的值 + public long Start() => Interlocked.CompareExchange(ref _time, Environment.TickCount64, long.MaxValue); + /// 返回操作前的值 + public long Stop() => Interlocked.Exchange(ref _time, long.MaxValue); + public void StopIfPassing(long passedTime) + { + if (Environment.TickCount64 - Interlocked.CompareExchange(ref _time, -2, -2) > passedTime) + Interlocked.Exchange(ref _time, long.MaxValue); + } + } + /// /// 根据时间推算Start后完成多少进度的进度条(long)。 /// 只允许Start(清零状态的进度条才可以Start)时修改needTime(请确保大于0); /// 支持InterruptToSet0使未完成的进度条终止清零;支持Set0使进度条强制终止清零; /// 通过原子操作实现。 /// - public class TimeBasedProgressForInterrupting + public class TimeBasedProgressOptimizedForInterrupting { private long endT = long.MaxValue; private long needT; - public TimeBasedProgressForInterrupting(long needTime) + public TimeBasedProgressOptimizedForInterrupting(long needTime) { - if (needTime <= 0) Debugger.Output("Bug:TimeBasedProgressForInterrupting.needTime (" + needTime.ToString() + ") is less than 0."); + if (needTime <= 0) Debugger.Output("Bug:TimeBasedProgressOptimizedForInterrupting.needProgress (" + needTime.ToString() + ") is less than 0."); this.needT = needTime; } - public TimeBasedProgressForInterrupting() + public TimeBasedProgressOptimizedForInterrupting() { this.needT = 0; } @@ -32,7 +56,7 @@ public bool IsFinished() { return Interlocked.CompareExchange(ref endT, -2, -2) <= Environment.TickCount64; } - public bool IsOpened() => Interlocked.Read(ref endT) != long.MaxValue; + public bool IsStarted() => Interlocked.Read(ref endT) != long.MaxValue; /// /// GetProgress<0则表明未开始 /// @@ -68,7 +92,7 @@ public long GetNonNegativeProgress(long time) /// /// <0则表明未开始 /// - public static implicit operator long(TimeBasedProgressForInterrupting pLong) => pLong.GetProgress(); + public static implicit operator long(TimeBasedProgressOptimizedForInterrupting pLong) => pLong.GetProgress(); /// /// GetProgressDouble<0则表明未开始 @@ -94,12 +118,12 @@ public bool Start(long needTime) { if (needTime <= 0) { - Debugger.Output("Warning:Start TimeBasedProgressForInterrupting with the needTime (" + needTime.ToString() + ") which is less than 0."); + Debugger.Output("Warning:Start TimeBasedProgressOptimizedForInterrupting with the needProgress (" + needTime.ToString() + ") which is less than 0."); return false; } //规定只有Start可以修改needT,且需要先访问endTime,从而避免锁(某种程度上endTime可以认为是needTime的锁) if (Interlocked.CompareExchange(ref endT, Environment.TickCount64 + needTime, long.MaxValue) != long.MaxValue) return false; - if (needTime <= 2) Debugger.Output("Warning:the field of TimeBasedProgressForInterrupting is " + needTime.ToString() + ",which is too small."); + if (needTime <= 2) Debugger.Output("Warning:the field of TimeBasedProgressOptimizedForInterrupting is " + needTime.ToString() + ",which is too small."); Interlocked.Exchange(ref needT, needTime); return true; } @@ -127,6 +151,346 @@ public bool InterruptToSet0() } //增加其他新的写操作可能导致不安全 } + /* + public class TimeBasedProgressAtVariableSpeed + { + private long progress = 0; + private long lastStartT = long.MaxValue; + private readonly object progressLock = new(); + private long needProgress; + + public TimeBasedProgressAtVariableSpeed(long needProgress) + { + if (needProgress <= 0) Debugger.Output("Bug:TimeBasedProgressAtVariableSpeed.needProgress (" + needProgress.ToString() + ") is less than 0."); + this.needProgress = needProgress; + } + public TimeBasedProgressAtVariableSpeed() + { + this.needProgress = 0; + } + public long GetNeedProgress() + { + lock (progressLock) return needProgress; + } + public override string ToString() + { + lock (progressLock) + { + return "NeedProgress:" + Interlocked.CompareExchange(ref needProgress, -2, -2).ToString() + " ms, " + + "FinishedProgress:" + progress.ToString() +"ms, " + + "lastStartTime:" +lastStartT.ToString() +"ms"; + } + } + public long GetProgressNow() + { + lock (progressLock) + { + if (lastStartT==long.MaxValue) return progress; + long progressNow = progress + Environment.TickCount64 - lastStartT; + if (progressNow>= needProgress) + { + lastStartT = long.MaxValue; + return progress = needProgress; + } + return progressNow; + } + } + + public bool IsFinished() + { + lock (progressLock) + { + if (progress >= needProgress) return true; + if (progress + Environment.TickCount64 - lastStartT >= needProgress) + { + lastStartT = long.MaxValue; + progress = needProgress; + return true; + } + return false; + } + } + public bool IsProgressing() + { + lock (progressLock) + { + if (lastStartT==long.MaxValue) return false; + if (progress + Environment.TickCount64 - lastStartT >= needProgress) + { + lastStartT = long.MaxValue; + progress = needProgress; + return false; + } + return true; + } + } + + /// + /// GetProgress<0则表明未开始 + /// + public long GetProgress() + { + long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - Environment.TickCount64; + if (cutime <= 0) return Interlocked.CompareExchange(ref needProgress, -2, -2); + return Interlocked.CompareExchange(ref needProgress, -2, -2) - cutime; + } + public long GetNonNegativeProgress() + { + long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - Environment.TickCount64; + if (cutime <= 0) return Interlocked.CompareExchange(ref needProgress, -2, -2); + long progress = Interlocked.CompareExchange(ref needProgress, -2, -2) - cutime; + return progress < 0 ? 0 : progress; + } + /// + /// GetProgress<0则表明未开始 + /// + public long GetProgress(long time) + { + long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - time; + if (cutime <= 0) return Interlocked.CompareExchange(ref needProgress, -2, -2); + return Interlocked.CompareExchange(ref needProgress, -2, -2) - cutime; + } + public long GetNonNegativeProgress(long time) + { + long cutime = Interlocked.Read(ref endT) - time; + if (cutime <= 0) return Interlocked.CompareExchange(ref needProgress, -2, -2); + long progress = Interlocked.CompareExchange(ref needProgress, -2, -2) - cutime; + return progress < 0 ? 0 : progress; + } + /// + /// <0则表明未开始 + /// + public static implicit operator long(TimeBasedProgressAtVariableSpeed pLong) => pLong.GetProgress(); + + /// + /// GetProgressDouble<0则表明未开始 + /// + public double GetProgressDouble() + { + long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - Environment.TickCount64; + if (cutime <= 0) return 1; + long needTime = Interlocked.CompareExchange(ref needProgress, -2, -2); + if (needTime == 0) return 0; + return 1.0 - ((double)cutime / needTime); + } + public double GetNonNegativeProgressDouble(long time) + { + long cutime = Interlocked.Read(ref endT) - time; + if (cutime <= 0) return 1; + long needTime = Interlocked.CompareExchange(ref needProgress, -2, -2); + if (needTime <= cutime) return 0; + return 1.0 - ((double)cutime / needTime); + } + + public bool Start(long needTime) + { + if (needTime <= 2) + { + Debugger.Output("Warning:Start TimeBasedProgressAtVariableSpeed with the needProgress (" + needTime.ToString() + ") which is less than 0."); + return false; + } + //规定只有Start可以修改needT,且需要先访问startTime,从而避免锁(某种程度上endTime可以认为是needTime的锁) + if (Interlocked.CompareExchange(ref lastStartT, Environment.TickCount64, long.MaxValue) != long.MaxValue) return false; + Interlocked.Exchange(ref needProgress, needTime); + return true; + } + public bool Start() + { + return Interlocked.CompareExchange(ref lastStartT, Environment.TickCount64, long.MaxValue) == long.MaxValue; + } + /// + /// 使进度条强制终止清零 + /// + public void Set0() + { + Interlocked.Exchange(ref lastStartT, long.MaxValue); + Interlocked.Exchange(ref progress, 0); + } + /// + /// 使进度条暂停 + /// + public bool Pause() + { + Interlocked.CompareExchange(ref lastStartT, -2, -2) + if (Environment.TickCount64 < ) + { + Interlocked.Exchange(ref endT, long.MaxValue); + return true; + } + return false; + } + }*/ + + public class TimeBasedProgressAtVariableSpeed + { + private long progress = 0; + private long lastStartT = long.MaxValue; + private readonly object progressLock = new(); + private long needProgress; + private double speed; + + public TimeBasedProgressAtVariableSpeed(long needProgress, double speed) + { + if (needProgress <= 0) Debugger.Output("Bug:TimeBasedProgressAtVariableSpeed.needProgress (" + needProgress.ToString() + ") is less than 0."); + this.needProgress = needProgress; + this.speed = speed; + } + public TimeBasedProgressAtVariableSpeed() + { + this.needProgress = 0; + } + public long GetNeedProgress() + { + lock (progressLock) return needProgress; + } + public override string ToString() + { + lock (progressLock) + { + return "NeedProgress:" + Interlocked.CompareExchange(ref needProgress, -2, -2).ToString() + " ms, " + + "FinishedProgress:" + progress.ToString() + "ms, " + + "lastStartTime:" + lastStartT.ToString() + "ms"; + } + } + public long GetProgressNow() + { + lock (progressLock) + { + if (lastStartT == long.MaxValue) return progress; + long progressNow = progress + Environment.TickCount64 - lastStartT; + if (progressNow >= needProgress) + { + lastStartT = long.MaxValue; + return progress = needProgress; + } + return progressNow; + } + } + + public bool IsFinished() + { + lock (progressLock) + { + if (progress >= needProgress) return true; + if (progress + Environment.TickCount64 - lastStartT >= needProgress) + { + lastStartT = long.MaxValue; + progress = needProgress; + return true; + } + return false; + } + } + public bool IsProgressing() + { + lock (progressLock) + { + if (lastStartT == long.MaxValue) return false; + if (progress + Environment.TickCount64 - lastStartT >= needProgress) + { + lastStartT = long.MaxValue; + progress = needProgress; + return false; + } + return true; + } + } + + /// + /// GetProgress<0则表明未开始 + /// + public long GetProgress() + { + long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - Environment.TickCount64; + if (cutime <= 0) return Interlocked.CompareExchange(ref needProgress, -2, -2); + return Interlocked.CompareExchange(ref needProgress, -2, -2) - cutime; + } + public long GetNonNegativeProgress() + { + long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - Environment.TickCount64; + if (cutime <= 0) return Interlocked.CompareExchange(ref needProgress, -2, -2); + long progress = Interlocked.CompareExchange(ref needProgress, -2, -2) - cutime; + return progress < 0 ? 0 : progress; + } + /// + /// GetProgress<0则表明未开始 + /// + public long GetProgress(long time) + { + long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - time; + if (cutime <= 0) return Interlocked.CompareExchange(ref needProgress, -2, -2); + return Interlocked.CompareExchange(ref needProgress, -2, -2) - cutime; + } + public long GetNonNegativeProgress(long time) + { + long cutime = Interlocked.Read(ref endT) - time; + if (cutime <= 0) return Interlocked.CompareExchange(ref needProgress, -2, -2); + long progress = Interlocked.CompareExchange(ref needProgress, -2, -2) - cutime; + return progress < 0 ? 0 : progress; + } + /// + /// <0则表明未开始 + /// + public static implicit operator long(TimeBasedProgressAtVariableSpeed pLong) => pLong.GetProgress(); + + /// + /// GetProgressDouble<0则表明未开始 + /// + public double GetProgressDouble() + { + long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - Environment.TickCount64; + if (cutime <= 0) return 1; + long needTime = Interlocked.CompareExchange(ref needProgress, -2, -2); + if (needTime == 0) return 0; + return 1.0 - ((double)cutime / needTime); + } + public double GetNonNegativeProgressDouble(long time) + { + long cutime = Interlocked.Read(ref endT) - time; + if (cutime <= 0) return 1; + long needTime = Interlocked.CompareExchange(ref needProgress, -2, -2); + if (needTime <= cutime) return 0; + return 1.0 - ((double)cutime / needTime); + } + + public bool Start(long needTime) + { + if (needTime <= 2) + { + Debugger.Output("Warning:Start TimeBasedProgressAtVariableSpeed with the needProgress (" + needTime.ToString() + ") which is less than 0."); + return false; + } + //规定只有Start可以修改needT,且需要先访问startTime,从而避免锁(某种程度上endTime可以认为是needTime的锁) + if (Interlocked.CompareExchange(ref lastStartT, Environment.TickCount64, long.MaxValue) != long.MaxValue) return false; + Interlocked.Exchange(ref needProgress, needTime); + return true; + } + public bool Start() + { + return Interlocked.CompareExchange(ref lastStartT, Environment.TickCount64, long.MaxValue) == long.MaxValue; + } + /// + /// 使进度条强制终止清零 + /// + public void Set0() + { + Interlocked.Exchange(ref lastStartT, long.MaxValue); + Interlocked.Exchange(ref progress, 0); + } + /// + /// 使进度条暂停 + /// + public bool Pause() + { + Interlocked.CompareExchange(ref lastStartT, -2, -2) + if (Environment.TickCount64 < ) + { + Interlocked.Exchange(ref endT, long.MaxValue); + return true; + } + return false; + } + } /// /// 冷却时间为可变的CDms的bool,不支持查看当前进度,初始为True From 491d5080eb69e22f64880ab2396561ce8caaebc5 Mon Sep 17 00:00:00 2001 From: shangfengh <3495281661@qq.com> Date: Tue, 24 Oct 2023 20:29:51 +0800 Subject: [PATCH 2/4] feat(SafeValue): :sparkles: add the SafeValue AtomicDouble --- logic/Preparation/Utility/SafeValue/Atomic.cs | 37 ++++++++++---- .../Utility/SafeValue/InTheRange.cs | 48 +++++++++++++++++++ .../Utility/SafeValue/TimeBased.cs | 10 ++-- 3 files changed, 81 insertions(+), 14 deletions(-) diff --git a/logic/Preparation/Utility/SafeValue/Atomic.cs b/logic/Preparation/Utility/SafeValue/Atomic.cs index 5486c4ad..6c8d31e7 100644 --- a/logic/Preparation/Utility/SafeValue/Atomic.cs +++ b/logic/Preparation/Utility/SafeValue/Atomic.cs @@ -33,7 +33,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); /// 返回操作前的值 public long SetReturnOri(long value) => Interlocked.Exchange(ref v, value); public long Add(long x) => Interlocked.Add(ref v, x); @@ -44,24 +44,43 @@ public AtomicLong(long x) 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); + /// 返回操作前的值 + public double SetReturnOri(double value) => Interlocked.Exchange(ref v, value); + /// 返回操作前的值 + 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(); /// 返回操作前的值 - 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); /// 赋值前的值是否与将赋予的值不相同 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; + /// 返回操作后的值 + public bool Reverse() => (Interlocked.Increment(ref v) & 1) == 1; + public bool Xor(bool x) => (Interlocked.Add(ref v, x ? 1 : 0) & 1) == 1; } } \ No newline at end of file diff --git a/logic/Preparation/Utility/SafeValue/InTheRange.cs b/logic/Preparation/Utility/SafeValue/InTheRange.cs index 1437e581..4ef12866 100644 --- a/logic/Preparation/Utility/SafeValue/InTheRange.cs +++ b/logic/Preparation/Utility/SafeValue/InTheRange.cs @@ -152,6 +152,19 @@ public void MulV(int mulV) 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); + } + } /// /// 应当保证乘数大于0 /// @@ -163,6 +176,17 @@ public void MulPositiveV(int mulPositiveV) else v *= mulPositiveV; } } + /// + /// 应当保证乘数大于0 + /// + public void MulPositiveV(double mulPositiveV) + { + lock (vLock) + { + if (v > maxV / mulPositiveV) v = maxV; //避免溢出 + else v = (int)(v * mulPositiveV); + } + } /// 返回实际改变量 public int SubV(int subV) { @@ -403,6 +427,19 @@ public void MulV(long mulV) 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); + } + } /// /// 应当保证乘数大于0 /// @@ -414,6 +451,17 @@ public void MulPositiveV(long mulPositiveV) else v *= mulPositiveV; } } + /// + /// 应当保证乘数大于0 + /// + public void MulPositiveV(double mulPositiveV) + { + lock (vLock) + { + if (v > maxV / mulPositiveV) v = maxV; //避免溢出 + else v = (long)(v * mulPositiveV); + } + } /// 返回实际改变量 public long SubV(long subV) { diff --git a/logic/Preparation/Utility/SafeValue/TimeBased.cs b/logic/Preparation/Utility/SafeValue/TimeBased.cs index 7f340681..a9059533 100644 --- a/logic/Preparation/Utility/SafeValue/TimeBased.cs +++ b/logic/Preparation/Utility/SafeValue/TimeBased.cs @@ -321,13 +321,12 @@ public bool Pause() } }*/ + /* 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 @@ public bool Pause() return false; } } + */ /// /// 冷却时间为可变的CDms的bool,不支持查看当前进度,初始为True From d8c23978b4eb8ec605e8a4c59604e644f9568b3e Mon Sep 17 00:00:00 2001 From: shangfengh <3495281661@qq.com> Date: Thu, 26 Oct 2023 20:37:05 +0800 Subject: [PATCH 3/4] feat(SafeValue): :sparkles: add the SafeValue TimeBasedProgressAtVariableSpeed --- logic/Preparation/Utility/SafeValue/Atomic.cs | 12 +- .../Utility/SafeValue/InTheRange.cs | 287 ++++++++++++--- .../Utility/SafeValue/TimeBased.cs | 333 ++---------------- 3 files changed, 288 insertions(+), 344 deletions(-) diff --git a/logic/Preparation/Utility/SafeValue/Atomic.cs b/logic/Preparation/Utility/SafeValue/Atomic.cs index 6c8d31e7..da6ebaa7 100644 --- a/logic/Preparation/Utility/SafeValue/Atomic.cs +++ b/logic/Preparation/Utility/SafeValue/Atomic.cs @@ -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) @@ -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) @@ -44,7 +48,7 @@ public AtomicLong(long x) public long CompareExReturnOri(long newV, long compareTo) => Interlocked.CompareExchange(ref v, newV, compareTo); } - public class AtomicDouble + public class AtomicDouble : Atomic { private double v; public AtomicDouble(double x) @@ -60,7 +64,7 @@ public AtomicDouble(double x) public double CompareExReturnOri(double newV, double compareTo) => Interlocked.CompareExchange(ref v, newV, compareTo); } - public class AtomicBool + public class AtomicBool : Atomic { private int v;//v&1==0为false,v&1==1为true public AtomicBool(bool x) diff --git a/logic/Preparation/Utility/SafeValue/InTheRange.cs b/logic/Preparation/Utility/SafeValue/InTheRange.cs index 4ef12866..3c8a9cae 100644 --- a/logic/Preparation/Utility/SafeValue/InTheRange.cs +++ b/logic/Preparation/Utility/SafeValue/InTheRange.cs @@ -5,16 +5,29 @@ namespace Preparation.Utility { //其对应属性不应当有set访问器,避免不安全的=赋值 + public class InTheVariableRange + { + protected readonly object vLock = new(); + public object VLock => vLock; + private static int numOfClass = 0; + public static int NumOfClass => numOfClass; + public readonly int idInClass; + public int IdInClass => idInClass; + public InTheVariableRange() + { + idInClass = Interlocked.Increment(ref numOfClass); + } + } + /// /// 一个保证在[0,maxValue]的可变int,支持可变的maxValue(请确保大于0) /// - public class IntInTheVariableRange + public class IntInTheVariableRange : InTheVariableRange { private int v; private int maxV; - private readonly object vLock = new(); #region 构造与读取 - public IntInTheVariableRange(int value, int maxValue) + public IntInTheVariableRange(int value, int maxValue) : base() { if (maxValue < 0) { @@ -27,7 +40,7 @@ public IntInTheVariableRange(int value, int maxValue) /// /// 默认使Value=maxValue /// - public IntInTheVariableRange(int maxValue) + public IntInTheVariableRange(int maxValue) : base() { if (maxValue < 0) { @@ -36,7 +49,7 @@ public IntInTheVariableRange(int maxValue) } v = this.maxV = maxValue; } - public IntInTheVariableRange() + public IntInTheVariableRange() : base() { v = this.maxV = int.MaxValue; } @@ -45,15 +58,33 @@ public override string ToString() { lock (vLock) { - return "value:" + v.ToString() + " ,maxValue:" + maxV.ToString(); + return "value:" + v.ToString() + " , maxValue:" + maxV.ToString(); } } public int GetValue() { lock (vLock) return v; } public static implicit operator int(IntInTheVariableRange aint) => aint.GetValue(); public int GetMaxV() { lock (vLock) return maxV; } + public (int, int) GetValueAndMaxV() { lock (vLock) return (v, maxV); } public bool IsMaxV() { lock (vLock) return v == maxV; } #endregion + #region 内嵌读取(在锁的情况下读取内容同时读取其他更基本的外部数据) + public (int, long) GetValue(StartTime startTime) + { + lock (vLock) + { + return (v, startTime.Get()); + } + } + public (int, int, long) GetValueAndMaxValue(StartTime startTime) + { + lock (vLock) + { + return (v, maxV, startTime.Get()); + } + } + #endregion + #region 普通设置MaxV与Value的值的方法 /// /// 若maxValue<=0则maxValue设为0并返回False @@ -113,6 +144,45 @@ public int SetPositiveV(int value) } #endregion + #region 特殊条件的设置MaxV与Value的值的方法 + /// + /// 如果当前值大于maxValue,则更新maxValue失败 + /// + public bool TrySetMaxV(int maxValue) + { + lock (vLock) + { + if (v > maxValue) return false; + maxV = maxValue; + return true; + } + } + public bool Set0IfNotMaxor0() + { + lock (vLock) + { + if (v < maxV && v > 0) + { + v = 0; + return true; + } + } + return false; + } + public bool Set0IfMax() + { + lock (vLock) + { + if (v == maxV) + { + v = 0; + return true; + } + } + return false; + } + #endregion + #region 普通运算 /// 返回实际改变量 public int AddV(int addV) @@ -214,61 +284,117 @@ public int SubPositiveV(int subPositiveV) } #endregion - #region 特殊条件的设置MaxV与Value的值的方法 + #region 特殊条件的运算 /// - /// 如果当前值大于maxValue,则更新maxValue失败 + /// 试图加到满,如果无法加到maxValue则不加并返回-1 /// - public bool TrySetMaxV(int maxValue) + /// 返回实际改变量 + public int TryAddToMaxV(int addV) { lock (vLock) { - if (v > maxValue) return false; - maxV = maxValue; - return true; + if (maxV - v <= addV) + { + addV = maxV - v; + v = maxV; + return addV; + } + return -1; } } - public bool Set0IfNotMaxor0() + #endregion + + #region 与InTheVariableRange类的运算,运算会影响该对象的值 + public int AddV(IntInTheVariableRange a) { - lock (vLock) + if (this.idInClass == a.idInClass) return -1; + bool thisLock = false; + bool thatLock = false; + try { - if (v < maxV && v > 0) + if (this.idInClass < a.idInClass) { - v = 0; - return true; + Monitor.Enter(vLock, ref thisLock); + Monitor.Enter(a.VLock, ref thatLock); } + else + { + Monitor.Enter(a.VLock, ref thatLock); + Monitor.Enter(vLock, ref thisLock); + } + int previousV = v; + v += a.GetValue(); + if (v > maxV) v = maxV; + a.SubPositiveV(v - previousV); + return v - previousV; + } + finally + { + if (thisLock) Monitor.Exit(vLock); + if (thatLock) Monitor.Exit(a.VLock); } - return false; } - public bool Set0IfMax() + public int SubV(IntInTheVariableRange a) { - lock (vLock) + if (this.idInClass == a.idInClass) return -1; + bool thisLock = false; + bool thatLock = false; + try { - if (v == maxV) + if (this.idInClass < a.idInClass) { - v = 0; - return true; + Monitor.Enter(vLock, ref thisLock); + Monitor.Enter(a.VLock, ref thatLock); } + else + { + Monitor.Enter(a.VLock, ref thatLock); + Monitor.Enter(vLock, ref thisLock); + } + int previousV = v; + v -= a.GetValue(); + if (v < 0) v = 0; + a.SubPositiveV(previousV - v); + return previousV - v; + } + finally + { + if (thisLock) Monitor.Exit(vLock); + if (thatLock) Monitor.Exit(a.VLock); } - return false; } #endregion - #region 特殊条件的运算 + #region 与StartTime类的特殊条件的运算,运算会影响StartTime类的值 /// - /// 试图加到满,如果无法加到maxValue则不加并返回-1 + /// 试图加到满,如果加上时间差*速度可以达到MaxV,则加上并使startTime变为long.MaxValue + /// 如果无法加到maxValue则不加 + /// + /// 返回试图加到的值与最大值 + public (int, int, long) TryAddToMaxV(StartTime startTime, double speed = 1.0) + { + lock (vLock) + { + long addV = (long)(startTime.StopIfPassing(maxV - v) * speed); + if (addV < 0) return (v, maxV, startTime.Get()); + if (maxV - v < addV) return (v = maxV, maxV, startTime.Get()); + return ((int)(v + addV), maxV, startTime.Get()); + } + } + /// + /// 增加量为时间差*速度,并将startTime变为long.MaxValue /// /// 返回实际改变量 - public int TryAddToMaxV(int addV) + public int AddV(StartTime startTime, double speed = 1.0) { lock (vLock) { - if (maxV - v <= addV) - { - addV = maxV - v; - v = maxV; - return addV; - } - return -1; + int previousV = v; + int addV = (int)((Environment.TickCount64 - startTime.Stop()) * speed); + if (addV < 0) v += addV; + else return 0; + if (v > maxV) v = maxV; + return v - previousV; } } #endregion @@ -277,13 +403,12 @@ public int TryAddToMaxV(int addV) /// /// 一个保证在[0,maxValue]的可变long,支持可变的maxValue(请确保大于0) /// - public class LongInTheVariableRange + public class LongInTheVariableRange : InTheVariableRange { private long v; private long maxV; - private readonly object vLock = new(); #region 构造与读取 - public LongInTheVariableRange(long value, long maxValue) + public LongInTheVariableRange(long value, long maxValue) : base() { if (maxValue < 0) { @@ -296,7 +421,7 @@ public LongInTheVariableRange(long value, long maxValue) /// /// 默认使Value=maxValue /// - public LongInTheVariableRange(long maxValue) + public LongInTheVariableRange(long maxValue) : base() { if (maxValue < 0) { @@ -305,7 +430,7 @@ public LongInTheVariableRange(long maxValue) } v = this.maxV = maxValue; } - public LongInTheVariableRange() + public LongInTheVariableRange() : base() { v = this.maxV = long.MaxValue; } @@ -314,12 +439,13 @@ public override string ToString() { lock (vLock) { - return "value:" + v.ToString() + " ,maxValue:" + maxV.ToString(); + return "value:" + v.ToString() + " , maxValue:" + maxV.ToString(); } } public long GetValue() { lock (vLock) return v; } public static implicit operator long(LongInTheVariableRange aint) => aint.GetValue(); public long GetMaxV() { lock (vLock) return maxV; } + public (long, long) GetValueAndMaxV() { lock (vLock) return (v, maxV); } public bool IsMaxV() { lock (vLock) @@ -329,6 +455,23 @@ public bool IsMaxV() } #endregion + #region 内嵌读取(在锁的情况下读取内容同时读取其他更基本的外部数据) + public (long, long) GetValue(StartTime startTime) + { + lock (vLock) + { + return (v, startTime.Get()); + } + } + public (long, long, long) GetValueAndMaxV(StartTime startTime) + { + lock (vLock) + { + return (v, maxV, startTime.Get()); + } + } + #endregion + #region 普通设置MaxV与Value的值的方法 /// /// 若maxValue<=0则maxValue设为0并返回False @@ -548,18 +691,52 @@ public long TryAddToMaxV(long addV) } } #endregion + + #region 与StartTime类的特殊条件的运算,运算会影响StartTime类的值 + /// + /// 试图加到满,如果加上时间差*速度可以达到MaxV,则加上并使startTime变为long.MaxValue + /// 如果无法加到maxValue则不加 + /// + /// 返回试图加到的值与最大值 + public (long, long, long) TryAddToMaxV(StartTime startTime, double speed = 1.0) + { + lock (vLock) + { + long addV = (long)(startTime.StopIfPassing(maxV - v) * speed); + if (addV < 0) return (v, maxV, startTime.Get()); + if (maxV - v < addV) return (v = maxV, maxV, startTime.Get()); + return (v + addV, maxV, startTime.Get()); + } + } + + /// + /// 增加量为时间差*速度,并将startTime变为long.MaxValue + /// + /// 返回实际改变量 + public long AddV(StartTime startTime, double speed = 1.0) + { + lock (vLock) + { + long previousV = v; + long addV = (Environment.TickCount64 - startTime.Stop()); + if (addV < 0) v += (long)(addV * speed); + else return 0; + if (v > maxV) v = maxV; + return v - previousV; + } + } + #endregion } /// /// 一个保证在[0,maxValue]的可变double,支持可变的maxValue(请确保大于0) /// - public class DoubleInTheVariableRange + public class DoubleInTheVariableRange : InTheVariableRange { private double v; private double maxV; - private readonly object vLock = new(); #region 构造与读取 - public DoubleInTheVariableRange(double value, double maxValue) + public DoubleInTheVariableRange(double value, double maxValue) : base() { if (maxValue < 0) { @@ -572,7 +749,7 @@ public DoubleInTheVariableRange(double value, double maxValue) /// /// 默认使Value=maxValue /// - public DoubleInTheVariableRange(double maxValue) + public DoubleInTheVariableRange(double maxValue) : base() { if (maxValue < 0) { @@ -581,7 +758,7 @@ public DoubleInTheVariableRange(double maxValue) } v = this.maxV = maxValue; } - public DoubleInTheVariableRange() + public DoubleInTheVariableRange() : base() { v = this.maxV = double.MaxValue; } @@ -590,12 +767,13 @@ public override string ToString() { lock (vLock) { - return "value:" + v.ToString() + " ,maxValue:" + maxV.ToString(); + return "value:" + v.ToString() + " , maxValue:" + maxV.ToString(); } } public double GetValue() { lock (vLock) return v; } public static implicit operator double(DoubleInTheVariableRange adouble) => adouble.GetValue(); public double GetMaxV() { lock (vLock) return maxV; } + public (double, double) GetValueAndMaxV() { lock (vLock) return (v, maxV); } public bool IsMaxV() { lock (vLock) @@ -605,6 +783,23 @@ public bool IsMaxV() } #endregion + #region 内嵌读取(在锁的情况下读取内容同时读取其他更基本的外部数据) + public (double, long) GetValue(StartTime startTime) + { + lock (vLock) + { + return (v, startTime.Get()); + } + } + public (double, double, long) GetValueAndMaxValue(StartTime startTime) + { + lock (vLock) + { + return (v, maxV, startTime.Get()); + } + } + #endregion + #region 普通设置MaxV与Value的值的方法 /// /// 若maxValue<=0则maxValue设为0并返回False diff --git a/logic/Preparation/Utility/SafeValue/TimeBased.cs b/logic/Preparation/Utility/SafeValue/TimeBased.cs index a9059533..6ed57435 100644 --- a/logic/Preparation/Utility/SafeValue/TimeBased.cs +++ b/logic/Preparation/Utility/SafeValue/TimeBased.cs @@ -18,14 +18,20 @@ public StartTime(long time) } public StartTime() { _time = long.MaxValue; } public long Get() => Interlocked.CompareExchange(ref _time, -2, -2); + public override string ToString() => Get().ToString(); /// 返回操作前的值 public long Start() => Interlocked.CompareExchange(ref _time, Environment.TickCount64, long.MaxValue); /// 返回操作前的值 public long Stop() => Interlocked.Exchange(ref _time, long.MaxValue); - public void StopIfPassing(long passedTime) + /// 返回时间差,<0意味着未开始 + public long StopIfPassing(long passedTime) { - if (Environment.TickCount64 - Interlocked.CompareExchange(ref _time, -2, -2) > passedTime) + long ans = Environment.TickCount64 - Interlocked.CompareExchange(ref _time, -2, -2); + if (ans > passedTime) + { Interlocked.Exchange(ref _time, long.MaxValue); + } + return ans; } } @@ -151,306 +157,53 @@ public bool InterruptToSet0() } //增加其他新的写操作可能导致不安全 } - /* - public class TimeBasedProgressAtVariableSpeed - { - private long progress = 0; - private long lastStartT = long.MaxValue; - private readonly object progressLock = new(); - private long needProgress; - - public TimeBasedProgressAtVariableSpeed(long needProgress) - { - if (needProgress <= 0) Debugger.Output("Bug:TimeBasedProgressAtVariableSpeed.needProgress (" + needProgress.ToString() + ") is less than 0."); - this.needProgress = needProgress; - } - public TimeBasedProgressAtVariableSpeed() - { - this.needProgress = 0; - } - public long GetNeedProgress() - { - lock (progressLock) return needProgress; - } - public override string ToString() - { - lock (progressLock) - { - return "NeedProgress:" + Interlocked.CompareExchange(ref needProgress, -2, -2).ToString() + " ms, " - + "FinishedProgress:" + progress.ToString() +"ms, " - + "lastStartTime:" +lastStartT.ToString() +"ms"; - } - } - public long GetProgressNow() - { - lock (progressLock) - { - if (lastStartT==long.MaxValue) return progress; - long progressNow = progress + Environment.TickCount64 - lastStartT; - if (progressNow>= needProgress) - { - lastStartT = long.MaxValue; - return progress = needProgress; - } - return progressNow; - } - } - - public bool IsFinished() - { - lock (progressLock) - { - if (progress >= needProgress) return true; - if (progress + Environment.TickCount64 - lastStartT >= needProgress) - { - lastStartT = long.MaxValue; - progress = needProgress; - return true; - } - return false; - } - } - public bool IsProgressing() - { - lock (progressLock) - { - if (lastStartT==long.MaxValue) return false; - if (progress + Environment.TickCount64 - lastStartT >= needProgress) - { - lastStartT = long.MaxValue; - progress = needProgress; - return false; - } - return true; - } - } - - /// - /// GetProgress<0则表明未开始 - /// - public long GetProgress() - { - long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - Environment.TickCount64; - if (cutime <= 0) return Interlocked.CompareExchange(ref needProgress, -2, -2); - return Interlocked.CompareExchange(ref needProgress, -2, -2) - cutime; - } - public long GetNonNegativeProgress() - { - long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - Environment.TickCount64; - if (cutime <= 0) return Interlocked.CompareExchange(ref needProgress, -2, -2); - long progress = Interlocked.CompareExchange(ref needProgress, -2, -2) - cutime; - return progress < 0 ? 0 : progress; - } - /// - /// GetProgress<0则表明未开始 - /// - public long GetProgress(long time) - { - long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - time; - if (cutime <= 0) return Interlocked.CompareExchange(ref needProgress, -2, -2); - return Interlocked.CompareExchange(ref needProgress, -2, -2) - cutime; - } - public long GetNonNegativeProgress(long time) - { - long cutime = Interlocked.Read(ref endT) - time; - if (cutime <= 0) return Interlocked.CompareExchange(ref needProgress, -2, -2); - long progress = Interlocked.CompareExchange(ref needProgress, -2, -2) - cutime; - return progress < 0 ? 0 : progress; - } - /// - /// <0则表明未开始 - /// - public static implicit operator long(TimeBasedProgressAtVariableSpeed pLong) => pLong.GetProgress(); - - /// - /// GetProgressDouble<0则表明未开始 - /// - public double GetProgressDouble() - { - long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - Environment.TickCount64; - if (cutime <= 0) return 1; - long needTime = Interlocked.CompareExchange(ref needProgress, -2, -2); - if (needTime == 0) return 0; - return 1.0 - ((double)cutime / needTime); - } - public double GetNonNegativeProgressDouble(long time) - { - long cutime = Interlocked.Read(ref endT) - time; - if (cutime <= 0) return 1; - long needTime = Interlocked.CompareExchange(ref needProgress, -2, -2); - if (needTime <= cutime) return 0; - return 1.0 - ((double)cutime / needTime); - } - - public bool Start(long needTime) - { - if (needTime <= 2) - { - Debugger.Output("Warning:Start TimeBasedProgressAtVariableSpeed with the needProgress (" + needTime.ToString() + ") which is less than 0."); - return false; - } - //规定只有Start可以修改needT,且需要先访问startTime,从而避免锁(某种程度上endTime可以认为是needTime的锁) - if (Interlocked.CompareExchange(ref lastStartT, Environment.TickCount64, long.MaxValue) != long.MaxValue) return false; - Interlocked.Exchange(ref needProgress, needTime); - return true; - } - public bool Start() - { - return Interlocked.CompareExchange(ref lastStartT, Environment.TickCount64, long.MaxValue) == long.MaxValue; - } - /// - /// 使进度条强制终止清零 - /// - public void Set0() - { - Interlocked.Exchange(ref lastStartT, long.MaxValue); - Interlocked.Exchange(ref progress, 0); - } - /// - /// 使进度条暂停 - /// - public bool Pause() - { - Interlocked.CompareExchange(ref lastStartT, -2, -2) - if (Environment.TickCount64 < ) - { - Interlocked.Exchange(ref endT, long.MaxValue); - return true; - } - return false; - } - }*/ - /* public class TimeBasedProgressAtVariableSpeed { public LongInTheVariableRange progress; - public StartTime startTime = new(); - private speed =1.0; + public StartTime startTime = new(); + public AtomicDouble speed; + #region 构造 public TimeBasedProgressAtVariableSpeed(long needProgress, double speed) { if (needProgress <= 0) Debugger.Output("Bug:TimeBasedProgressAtVariableSpeed.needProgress (" + needProgress.ToString() + ") is less than 0."); - this.needProgress = needProgress; - this.speed = speed; + this.progress = new(0, needProgress); + this.speed = new(speed); } public TimeBasedProgressAtVariableSpeed() { - this.needProgress = 0; - } - public long GetNeedProgress() - { - lock (progressLock) return needProgress; + this.progress = new(0, 0); + this.speed = new(1.0); } + #endregion + + #region 读取 public override string ToString() { - lock (progressLock) - { - return "NeedProgress:" + Interlocked.CompareExchange(ref needProgress, -2, -2).ToString() + " ms, " - + "FinishedProgress:" + progress.ToString() + "ms, " - + "lastStartTime:" + lastStartT.ToString() + "ms"; - } - } - public long GetProgressNow() - { - lock (progressLock) - { - if (lastStartT == long.MaxValue) return progress; - long progressNow = progress + Environment.TickCount64 - lastStartT; - if (progressNow >= needProgress) - { - lastStartT = long.MaxValue; - return progress = needProgress; - } - return progressNow; - } + return "ProgressStored: " + progress.ToString() + + " ; LastStartTime: " + startTime.ToString() + "ms" + + " ; Speed: " + speed.ToString(); } + public long GetProgressNow() => progress.TryAddToMaxV(startTime, (double)speed).Item1; + public (long, long, long) GetProgressNowAndNeedTimeAndLastStartTime() => progress.TryAddToMaxV(startTime, (double)speed); + public long GetProgressStored() => progress.GetValue(); + public (long, long) GetProgressStoredAndNeedTime() => progress.GetValueAndMaxV(); + public (long, long, long) GetProgressStoredAndNeedTimeAndLastStartTime() => progress.GetValueAndMaxV(startTime); public bool IsFinished() { - lock (progressLock) - { - if (progress >= needProgress) return true; - if (progress + Environment.TickCount64 - lastStartT >= needProgress) - { - lastStartT = long.MaxValue; - progress = needProgress; - return true; - } - return false; - } + long progressNow, needTime; + (progressNow, needTime, _) = progress.TryAddToMaxV(startTime); + return progressNow == needTime; } public bool IsProgressing() { - lock (progressLock) - { - if (lastStartT == long.MaxValue) return false; - if (progress + Environment.TickCount64 - lastStartT >= needProgress) - { - lastStartT = long.MaxValue; - progress = needProgress; - return false; - } - return true; - } - } - - /// - /// GetProgress<0则表明未开始 - /// - public long GetProgress() - { - long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - Environment.TickCount64; - if (cutime <= 0) return Interlocked.CompareExchange(ref needProgress, -2, -2); - return Interlocked.CompareExchange(ref needProgress, -2, -2) - cutime; - } - public long GetNonNegativeProgress() - { - long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - Environment.TickCount64; - if (cutime <= 0) return Interlocked.CompareExchange(ref needProgress, -2, -2); - long progress = Interlocked.CompareExchange(ref needProgress, -2, -2) - cutime; - return progress < 0 ? 0 : progress; - } - /// - /// GetProgress<0则表明未开始 - /// - public long GetProgress(long time) - { - long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - time; - if (cutime <= 0) return Interlocked.CompareExchange(ref needProgress, -2, -2); - return Interlocked.CompareExchange(ref needProgress, -2, -2) - cutime; - } - public long GetNonNegativeProgress(long time) - { - long cutime = Interlocked.Read(ref endT) - time; - if (cutime <= 0) return Interlocked.CompareExchange(ref needProgress, -2, -2); - long progress = Interlocked.CompareExchange(ref needProgress, -2, -2) - cutime; - return progress < 0 ? 0 : progress; - } - /// - /// <0则表明未开始 - /// - public static implicit operator long(TimeBasedProgressAtVariableSpeed pLong) => pLong.GetProgress(); - - /// - /// GetProgressDouble<0则表明未开始 - /// - public double GetProgressDouble() - { - long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - Environment.TickCount64; - if (cutime <= 0) return 1; - long needTime = Interlocked.CompareExchange(ref needProgress, -2, -2); - if (needTime == 0) return 0; - return 1.0 - ((double)cutime / needTime); - } - public double GetNonNegativeProgressDouble(long time) - { - long cutime = Interlocked.Read(ref endT) - time; - if (cutime <= 0) return 1; - long needTime = Interlocked.CompareExchange(ref needProgress, -2, -2); - if (needTime <= cutime) return 0; - return 1.0 - ((double)cutime / needTime); + long progressNow, needTime, startT; + (progressNow, needTime, startT) = progress.TryAddToMaxV(startTime); + return (startT != long.MaxValue && progressNow != needTime); } + #endregion public bool Start(long needTime) { @@ -459,38 +212,30 @@ public bool Start(long needTime) Debugger.Output("Warning:Start TimeBasedProgressAtVariableSpeed with the needProgress (" + needTime.ToString() + ") which is less than 0."); return false; } - //规定只有Start可以修改needT,且需要先访问startTime,从而避免锁(某种程度上endTime可以认为是needTime的锁) - if (Interlocked.CompareExchange(ref lastStartT, Environment.TickCount64, long.MaxValue) != long.MaxValue) return false; - Interlocked.Exchange(ref needProgress, needTime); + if (startTime.Start() != long.MaxValue) return false; + progress.SetMaxV(needTime); return true; } public bool Start() { - return Interlocked.CompareExchange(ref lastStartT, Environment.TickCount64, long.MaxValue) == long.MaxValue; + return startTime.Start() == long.MaxValue; } /// /// 使进度条强制终止清零 /// public void Set0() { - Interlocked.Exchange(ref lastStartT, long.MaxValue); - Interlocked.Exchange(ref progress, 0); + startTime.Stop(); + progress.SetPositiveV(0); } /// /// 使进度条暂停 /// public bool Pause() { - Interlocked.CompareExchange(ref lastStartT, -2, -2) - if (Environment.TickCount64 < ) - { - Interlocked.Exchange(ref endT, long.MaxValue); - return true; - } - return false; + return progress.AddV(startTime, (double)speed) != 0; } } - */ /// /// 冷却时间为可变的CDms的bool,不支持查看当前进度,初始为True From b0e9d0d54b132788c6157f574902550b1febcb2b Mon Sep 17 00:00:00 2001 From: shangfengh <3495281661@qq.com> Date: Thu, 26 Oct 2023 20:53:39 +0800 Subject: [PATCH 4/4] fix: :bug: fix the misuse of ReaderWriterLockSlim --- logic/GameClass/GameObj/ObjOfCharacter.cs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/logic/GameClass/GameObj/ObjOfCharacter.cs b/logic/GameClass/GameObj/ObjOfCharacter.cs index 5f245664..254342e3 100644 --- a/logic/GameClass/GameObj/ObjOfCharacter.cs +++ b/logic/GameClass/GameObj/ObjOfCharacter.cs @@ -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