You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

SafeValue.cs 14 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. using System;
  2. using System.Threading;
  3. namespace Preparation.Utility
  4. {
  5. //理论上结构体最好不可变,这里采用了可变结构。
  6. //其对应属性不应当有set访问器,避免不安全的=赋值
  7. public struct AtomicInt
  8. {
  9. private int v;
  10. public AtomicInt(int x)
  11. {
  12. v = x;
  13. }
  14. public override string ToString() => Interlocked.CompareExchange(ref v, -1, -1).ToString();
  15. public int Get() => Interlocked.CompareExchange(ref v, -1, -1);
  16. public static implicit operator int(AtomicInt aint) => Interlocked.CompareExchange(ref aint.v, -1, -1);
  17. public int Set(int value) => Interlocked.Exchange(ref v, value);
  18. public int Add(int x) => Interlocked.Add(ref v, x);
  19. public int Sub(int x) => Interlocked.Add(ref v, -x);
  20. public int Inc() => Interlocked.Increment(ref v);
  21. public int Dec() => Interlocked.Decrement(ref v);
  22. public void CompareExchange(int newV, int compareTo) => Interlocked.CompareExchange(ref v, newV, compareTo);
  23. /// <returns>返回操作前的值</returns>
  24. public int CompareExReturnOri(int newV, int compareTo) => Interlocked.CompareExchange(ref v, newV, compareTo);
  25. }
  26. public struct AtomicBool
  27. {
  28. private int v;//v==0为false,v!=0(v==1或v==-1)为true
  29. public AtomicBool(bool x)
  30. {
  31. v = x ? 1 : 0;
  32. }
  33. public override string ToString() => (Interlocked.CompareExchange(ref v, -2, -2) == 0) ? "false" : "true";
  34. public bool Get() => (Interlocked.CompareExchange(ref v, -1, -1) != 0);
  35. public static implicit operator bool(AtomicBool abool) => (Interlocked.CompareExchange(ref abool.v, -1, -1) != 0);
  36. public bool Set(bool value) => (Interlocked.Exchange(ref v, value ? 1 : 0) != 0);
  37. /// <returns>赋值前的值是否与将赋予的值不相同</returns>
  38. public bool TrySet(bool value)
  39. {
  40. return (Interlocked.CompareExchange(ref v, value ? 1 : 0, value ? 0 : 1) ^ (value ? 1 : 0)) != 0;
  41. }
  42. public bool Invert() => Interlocked.Add(ref v, -1) != 0;
  43. public bool And(bool x) => Interlocked.And(ref v, x ? 1 : 0) != 0;
  44. public bool Or(bool x) => Interlocked.Or(ref v, x ? 1 : 0) != 0;
  45. }
  46. /// <summary>
  47. /// 一个能记录Start后完成多少进度的进度条(int),
  48. /// 只允许Start时修改needTime(请确保大于0);
  49. /// 支持TrySet0使未完成的进度条终止清零;支持Set0使进度条强制终止清零;
  50. /// 不支持暂停
  51. /// </summary>
  52. public struct IntProgressContinuously
  53. {
  54. private long endT = long.MaxValue;
  55. private long needT;
  56. public IntProgressContinuously(long needTime)
  57. {
  58. if (needTime <= 0) Debugger.Output("Bug:IntProgressContinuously.needTime (" + needTime.ToString() + ") is less than 0.");
  59. this.needT = needTime;
  60. }
  61. public long GetEndTime() => Interlocked.CompareExchange(ref endT, -2, -2);
  62. public long GetNeedTime() => Interlocked.CompareExchange(ref needT, -2, -2);
  63. public override string ToString() => "EndTime:" + Interlocked.CompareExchange(ref endT, -2, -2).ToString() + " ms, NeedTime:" + Interlocked.CompareExchange(ref needT, -2, -2).ToString() + " ms";
  64. public bool IsFinished()
  65. {
  66. return Interlocked.CompareExchange(ref endT, -2, -2) <= Environment.TickCount64;
  67. }
  68. /// <summary>
  69. /// GetProgress<0则表明未开始
  70. /// </summary>
  71. public long GetProgress()
  72. {
  73. long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - Environment.TickCount64;
  74. if (cutime <= 0) return Interlocked.CompareExchange(ref needT, -2, -2);
  75. return Interlocked.CompareExchange(ref needT, -2, -2) - cutime;
  76. }
  77. /// <summary>
  78. /// GetProgressDouble<0则表明未开始
  79. /// </summary>
  80. public double GetProgressDouble()
  81. {
  82. long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - Environment.TickCount64;
  83. if (cutime <= 0) return 1;
  84. return 1.0 - ((double)cutime / Interlocked.CompareExchange(ref needT, -2, -2));
  85. }
  86. public bool Start(long needTime)
  87. {
  88. if (needTime <= 0)
  89. {
  90. Debugger.Output("Warning:Start IntProgressContinuously with the needTime (" + needTime.ToString() + ") which is less than 0.");
  91. return false;
  92. }
  93. //规定只有Start可以修改needT,且需要先访问endTime,从而避免锁(某种程度上endTime可以认为是needTime的锁)
  94. if (Interlocked.CompareExchange(ref endT, Environment.TickCount64 + needTime, long.MaxValue) != long.MaxValue) return false;
  95. if (needTime <= 2) Debugger.Output("Warning:the field of IntProgressContinuously is " + needTime.ToString() + ",which is too small.");
  96. Interlocked.Exchange(ref this.needT, needTime);
  97. return true;
  98. }
  99. public bool Start()
  100. {
  101. long needTime = Interlocked.CompareExchange(ref needT, -2, -2);
  102. if (Interlocked.CompareExchange(ref endT, Environment.TickCount64 + needTime, long.MaxValue) != long.MaxValue) return false;
  103. return true;
  104. }
  105. public void Set0() => Interlocked.Exchange(ref endT, long.MaxValue);
  106. public bool TrySet0()
  107. {
  108. if (Environment.TickCount64 < Interlocked.CompareExchange(ref endT, -2, -2))
  109. {
  110. Interlocked.Exchange(ref endT, long.MaxValue);
  111. return true;
  112. }
  113. return false;
  114. }
  115. //增加其他新的写操作可能导致不安全
  116. }
  117. /// <summary>
  118. /// 一个保证在[0,maxValue]的可变int,支持可变的maxValue(请确保大于0)
  119. /// </summary>
  120. public struct IntWithVariableRange
  121. {
  122. private int v;
  123. private int maxV;
  124. private readonly object vLock = new();
  125. public IntWithVariableRange(int value, int maxValue)
  126. {
  127. if (maxValue < 0)
  128. {
  129. Debugger.Output("Warning:Try to set IntWithVariableRange.maxValue to " + maxValue.ToString() + ".");
  130. maxValue = 0;
  131. }
  132. v = value < maxValue ? value : maxValue;
  133. this.maxV = maxValue;
  134. }
  135. public override string ToString()
  136. {
  137. lock (vLock)
  138. {
  139. return "value:" + v.ToString() + " ,maxValue:" + maxV.ToString();
  140. }
  141. }
  142. public int GetValue() { lock (vLock) return v; }
  143. public int GetMaxV() { lock (vLock) return maxV; }
  144. /// <summary>
  145. /// 若maxValue<=0则maxValue设为0并返回False
  146. /// </summary>
  147. public bool SetMaxV(int maxValue)
  148. {
  149. if (maxValue < 0) maxValue = 0;
  150. lock (vLock)
  151. {
  152. maxV = maxValue;
  153. if (v > maxValue) v = maxValue;
  154. }
  155. return maxValue > 0;
  156. }
  157. /// <summary>
  158. /// 应当保证该maxValue>=0
  159. /// </summary>
  160. public void SetPositiveMaxV(int maxValue)
  161. {
  162. lock (vLock)
  163. {
  164. maxV = maxValue;
  165. if (v > maxValue) v = maxValue;
  166. }
  167. }
  168. /// <summary>
  169. /// 应当保证该value>=0
  170. /// </summary>
  171. public int SetPositiveV(int value)
  172. {
  173. lock (vLock)
  174. {
  175. return v = (value > maxV) ? maxV : value;
  176. }
  177. }
  178. public int SetV(int value)
  179. {
  180. if (value < 0) value = 0;
  181. lock (vLock)
  182. {
  183. return v = (value > maxV) ? maxV : value;
  184. }
  185. }
  186. /// <summary>
  187. /// 返回实际改变量
  188. /// </summary>
  189. public int AddV(int addV)
  190. {
  191. lock (vLock)
  192. {
  193. int previousV = v;
  194. v += addV;
  195. if (v < 0) v = 0;
  196. if (v > maxV) v = maxV;
  197. return v - previousV;
  198. }
  199. }
  200. /// <summary>
  201. /// 应当保证该增加值大于0,返回实际改变量
  202. /// </summary>
  203. public int AddPositiveV(int addPositiveV)
  204. {
  205. lock (vLock)
  206. {
  207. addPositiveV = Math.Min(addPositiveV, maxV - v);
  208. v += addPositiveV;
  209. }
  210. return addPositiveV;
  211. }
  212. /// <summary>
  213. /// 应当保证该减少值大于0,返回实际改变量
  214. /// </summary>
  215. public int SubPositiveV(int subPositiveV)
  216. {
  217. lock (vLock)
  218. {
  219. subPositiveV = Math.Min(subPositiveV, v);
  220. v -= subPositiveV;
  221. }
  222. return subPositiveV;
  223. }
  224. }
  225. /// <summary>
  226. /// 一个保证在[0,maxNum],每CDms自动更新的可变int,支持可变的CD、maxNum(请确保大于0)
  227. /// </summary>
  228. public struct IntNumUpdateByCD
  229. {
  230. private int num;
  231. private int maxNum;
  232. private int cd;
  233. private long updateTime = 0;
  234. private object numLock = new();
  235. public IntNumUpdateByCD(int num, int maxNum, int cd)
  236. {
  237. if (num < 0) Debugger.Output("Bug:IntNumUpdateByCD.num (" + num.ToString() + ") is less than 0.");
  238. if (maxNum < 0) Debugger.Output("Bug:IntNumUpdateByCD.maxNum (" + maxNum.ToString() + ") is less than 0.");
  239. if (cd <= 0) Debugger.Output("Bug:IntNumUpdateByCD.cd (" + cd.ToString() + ") is less than 0.");
  240. this.num = (num < maxNum) ? num : maxNum;
  241. this.maxNum = maxNum;
  242. this.cd = cd;
  243. this.updateTime = Environment.TickCount64;
  244. }
  245. public IntNumUpdateByCD(int maxNum, int cd)
  246. {
  247. if (maxNum < 0) Debugger.Output("Bug:IntNumUpdateByCD.maxNum (" + maxNum.ToString() + ") is less than 0.");
  248. if (cd <= 0) Debugger.Output("Bug:IntNumUpdateByCD.cd (" + cd.ToString() + ") is less than 0.");
  249. this.num = this.maxNum = maxNum;
  250. this.cd = cd;
  251. }
  252. public int GetMaxNum() { lock (numLock) return maxNum; }
  253. public int GetCD() { lock (numLock) return cd; }
  254. public int GetNum(long time)
  255. {
  256. lock (numLock)
  257. {
  258. if (num < maxNum && time - updateTime >= cd)
  259. {
  260. int add = (int)Math.Min(maxNum - num, (time - updateTime) / cd);
  261. updateTime += add * cd;
  262. return (num += add);
  263. }
  264. return num;
  265. }
  266. }
  267. /// <summary>
  268. /// 应当保证该subV>=0
  269. /// </summary>
  270. public int TrySub(int subV)
  271. {
  272. if (subV < 0) Debugger.Output("Bug:IntNumUpdateByCD Try to sub " + subV.ToString() + ", which is less than 0.");
  273. long time = Environment.TickCount64;
  274. lock (numLock)
  275. {
  276. if (num < maxNum && time - updateTime >= cd)
  277. {
  278. int add = (int)Math.Min(maxNum - num, (time - updateTime) / cd);
  279. updateTime += add * cd;
  280. num += add;
  281. }
  282. if (num == maxNum) updateTime = time;
  283. num -= subV = Math.Min(subV, num);
  284. }
  285. return subV;
  286. }
  287. /// <summary>
  288. /// 应当保证该addV>=0
  289. /// </summary>
  290. public void TryAdd(int addV)
  291. {
  292. if (addV < 0) Debugger.Output("Bug:IntNumUpdateByCD Try to add " + addV.ToString() + ", which is less than 0.");
  293. lock (numLock)
  294. {
  295. num += Math.Min(addV, maxNum - num);
  296. }
  297. }
  298. /// <summary>
  299. /// 若maxNum<=0则maxNum及Num设为0并返回False
  300. /// </summary>
  301. public bool SetMaxNumAndNum(int maxNum)
  302. {
  303. if (maxNum < 0) maxNum = 0;
  304. lock (numLock)
  305. {
  306. this.num = this.maxNum = maxNum;
  307. }
  308. return maxNum > 0;
  309. }
  310. /// <summary>
  311. /// 应当保证该maxnum>=0
  312. /// </summary>
  313. public void SetPositiveMaxNumAndNum(int maxNum)
  314. {
  315. lock (numLock)
  316. {
  317. this.num = this.maxNum = maxNum;
  318. }
  319. }
  320. /// <summary>
  321. /// 应当保证该maxnum>=0
  322. /// </summary>
  323. public void SetPositiveMaxNum(int maxNum)
  324. {
  325. lock (numLock)
  326. {
  327. if ((this.maxNum = maxNum) < num)
  328. num = maxNum;
  329. }
  330. }
  331. /// <summary>
  332. /// 若maxNum<=0则maxNum及Num设为0并返回False
  333. /// </summary>
  334. public bool SetMaxNum(int maxNum)
  335. {
  336. if (maxNum < 0) maxNum = 0;
  337. lock (numLock)
  338. {
  339. if ((this.maxNum = maxNum) < num)
  340. num = maxNum;
  341. }
  342. return maxNum > 0;
  343. }
  344. /// <summary>
  345. /// 若num<0则num设为0并返回False
  346. /// </summary>
  347. public bool SetNum(int num)
  348. {
  349. lock (numLock)
  350. {
  351. if (num < 0) { this.num = 0; return false; }
  352. this.num = (num < maxNum) ? num : maxNum;
  353. return true;
  354. }
  355. }
  356. /// <summary>
  357. /// 应当保证该num>=0
  358. /// </summary>
  359. public void SetPositiveNum(int num)
  360. {
  361. lock (numLock)
  362. {
  363. this.num = (num < maxNum) ? num : maxNum;
  364. }
  365. }
  366. public void SetCD(int cd)
  367. {
  368. lock (numLock)
  369. {
  370. if (cd <= 0) Debugger.Output("Bug:Set IntNumUpdateByCD.cd to " + cd.ToString() + ".");
  371. this.cd = cd;
  372. }
  373. }
  374. }
  375. }