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 20 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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 AtomicLong
  27. {
  28. private long v;
  29. public AtomicLong(long x)
  30. {
  31. v = x;
  32. }
  33. public override string ToString() => Interlocked.Read(ref v).ToString();
  34. public long Get() => Interlocked.Read(ref v);
  35. public static implicit operator long(AtomicLong aint) => Interlocked.Read(ref aint.v);
  36. public long Set(long value) => Interlocked.Exchange(ref v, value);
  37. public long Add(long x) => Interlocked.Add(ref v, x);
  38. public long Sub(long x) => Interlocked.Add(ref v, -x);
  39. public long Inc() => Interlocked.Increment(ref v);
  40. public long Dec() => Interlocked.Decrement(ref v);
  41. public void CompareExchange(long newV, long compareTo) => Interlocked.CompareExchange(ref v, newV, compareTo);
  42. /// <returns>返回操作前的值</returns>
  43. public long CompareExReturnOri(long newV, long compareTo) => Interlocked.CompareExchange(ref v, newV, compareTo);
  44. }
  45. public struct AtomicBool
  46. {
  47. private int v;//v==0为false,v==1为true
  48. public AtomicBool(bool x)
  49. {
  50. v = x ? 1 : 0;
  51. }
  52. public override string ToString() => (Interlocked.CompareExchange(ref v, -2, -2) == 0) ? "false" : "true";
  53. public bool Get() => (Interlocked.CompareExchange(ref v, -1, -1) != 0);
  54. public static implicit operator bool(AtomicBool abool) => (Interlocked.CompareExchange(ref abool.v, -1, -1) != 0);
  55. public bool Set(bool value) => (Interlocked.Exchange(ref v, value ? 1 : 0) != 0);
  56. /// <returns>赋值前的值是否与将赋予的值不相同</returns>
  57. public bool TrySet(bool value)
  58. {
  59. return (Interlocked.CompareExchange(ref v, value ? 1 : 0, value ? 0 : 1) ^ (value ? 1 : 0)) != 0;
  60. }
  61. public bool And(bool x) => Interlocked.And(ref v, x ? 1 : 0) != 0;
  62. public bool Or(bool x) => Interlocked.Or(ref v, x ? 1 : 0) != 0;
  63. }
  64. /// <summary>
  65. /// 一个能记录Start后完成多少进度的进度条(long),
  66. /// 只允许Start时修改needTime(请确保大于0);
  67. /// 支持TrySet0使未完成的进度条终止清零;支持Set0使进度条强制终止清零;
  68. /// 不支持暂停
  69. /// </summary>
  70. public struct LongProgressContinuously
  71. {
  72. private long endT = long.MaxValue;
  73. private long needT;
  74. public LongProgressContinuously(long needTime)
  75. {
  76. if (needTime <= 0) Debugger.Output("Bug:LongProgressContinuously.needTime (" + needTime.ToString() + ") is less than 0.");
  77. this.needT = needTime;
  78. }
  79. public long GetEndTime() => Interlocked.CompareExchange(ref endT, -2, -2);
  80. public long GetNeedTime() => Interlocked.CompareExchange(ref needT, -2, -2);
  81. public override string ToString() => "EndTime:" + Interlocked.CompareExchange(ref endT, -2, -2).ToString() + " ms, NeedTime:" + Interlocked.CompareExchange(ref needT, -2, -2).ToString() + " ms";
  82. public bool IsFinished()
  83. {
  84. return Interlocked.CompareExchange(ref endT, -2, -2) <= Environment.TickCount64;
  85. }
  86. /// <summary>
  87. /// GetProgress<0则表明未开始
  88. /// </summary>
  89. public long GetProgress()
  90. {
  91. long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - Environment.TickCount64;
  92. if (cutime <= 0) return Interlocked.CompareExchange(ref needT, -2, -2);
  93. return Interlocked.CompareExchange(ref needT, -2, -2) - cutime;
  94. }
  95. /// <summary>
  96. /// <0则表明未开始
  97. /// </summary>
  98. public static implicit operator long(LongProgressContinuously pLong) => pLong.GetProgress();
  99. /// <summary>
  100. /// GetProgressDouble<0则表明未开始
  101. /// </summary>
  102. public double GetProgressDouble()
  103. {
  104. long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - Environment.TickCount64;
  105. if (cutime <= 0) return 1;
  106. return 1.0 - ((double)cutime / Interlocked.CompareExchange(ref needT, -2, -2));
  107. }
  108. public bool Start(long needTime)
  109. {
  110. if (needTime <= 0)
  111. {
  112. Debugger.Output("Warning:Start LongProgressContinuously with the needTime (" + needTime.ToString() + ") which is less than 0.");
  113. return false;
  114. }
  115. //规定只有Start可以修改needT,且需要先访问endTime,从而避免锁(某种程度上endTime可以认为是needTime的锁)
  116. if (Interlocked.CompareExchange(ref endT, Environment.TickCount64 + needTime, long.MaxValue) != long.MaxValue) return false;
  117. if (needTime <= 2) Debugger.Output("Warning:the field of LongProgressContinuously is " + needTime.ToString() + ",which is too small.");
  118. Interlocked.Exchange(ref this.needT, needTime);
  119. return true;
  120. }
  121. public bool Start()
  122. {
  123. long needTime = Interlocked.CompareExchange(ref needT, -2, -2);
  124. if (Interlocked.CompareExchange(ref endT, Environment.TickCount64 + needTime, long.MaxValue) != long.MaxValue) return false;
  125. return true;
  126. }
  127. public void Set0() => Interlocked.Exchange(ref endT, long.MaxValue);
  128. public bool TrySet0()
  129. {
  130. if (Environment.TickCount64 < Interlocked.CompareExchange(ref endT, -2, -2))
  131. {
  132. Interlocked.Exchange(ref endT, long.MaxValue);
  133. return true;
  134. }
  135. return false;
  136. }
  137. //增加其他新的写操作可能导致不安全
  138. }
  139. /// <summary>
  140. /// 一个保证在[0,maxValue]的可变int,支持可变的maxValue(请确保大于0)
  141. /// </summary>
  142. public struct IntWithVariableRange
  143. {
  144. private int v;
  145. private int maxV;
  146. private readonly object vLock = new();
  147. public IntWithVariableRange(int value, int maxValue)
  148. {
  149. if (maxValue < 0)
  150. {
  151. Debugger.Output("Warning:Try to set IntWithVariableRange.maxValue to " + maxValue.ToString() + ".");
  152. maxValue = 0;
  153. }
  154. v = value < maxValue ? value : maxValue;
  155. this.maxV = maxValue;
  156. }
  157. public override string ToString()
  158. {
  159. lock (vLock)
  160. {
  161. return "value:" + v.ToString() + " ,maxValue:" + maxV.ToString();
  162. }
  163. }
  164. public int GetValue() { lock (vLock) return v; }
  165. public static implicit operator int(IntWithVariableRange aint) => aint.GetValue();
  166. public int GetMaxV() { lock (vLock) return maxV; }
  167. /// <summary>
  168. /// 若maxValue<=0则maxValue设为0并返回False
  169. /// </summary>
  170. public bool SetMaxV(int maxValue)
  171. {
  172. if (maxValue < 0) maxValue = 0;
  173. lock (vLock)
  174. {
  175. maxV = maxValue;
  176. if (v > maxValue) v = maxValue;
  177. }
  178. return maxValue > 0;
  179. }
  180. /// <summary>
  181. /// 应当保证该maxValue>=0
  182. /// </summary>
  183. public void SetPositiveMaxV(int maxValue)
  184. {
  185. lock (vLock)
  186. {
  187. maxV = maxValue;
  188. if (v > maxValue) v = maxValue;
  189. }
  190. }
  191. /// <summary>
  192. /// 应当保证该value>=0
  193. /// </summary>
  194. public int SetPositiveV(int value)
  195. {
  196. lock (vLock)
  197. {
  198. return v = (value > maxV) ? maxV : value;
  199. }
  200. }
  201. public int SetV(int value)
  202. {
  203. if (value < 0) value = 0;
  204. lock (vLock)
  205. {
  206. return v = (value > maxV) ? maxV : value;
  207. }
  208. }
  209. /// <summary>
  210. /// 返回实际改变量
  211. /// </summary>
  212. public int AddV(int addV)
  213. {
  214. lock (vLock)
  215. {
  216. int previousV = v;
  217. v += addV;
  218. if (v < 0) v = 0;
  219. if (v > maxV) v = maxV;
  220. return v - previousV;
  221. }
  222. }
  223. /// <summary>
  224. /// 应当保证该增加值大于0,返回实际改变量
  225. /// </summary>
  226. public int AddPositiveV(int addPositiveV)
  227. {
  228. lock (vLock)
  229. {
  230. addPositiveV = Math.Min(addPositiveV, maxV - v);
  231. v += addPositiveV;
  232. }
  233. return addPositiveV;
  234. }
  235. /// <summary>
  236. /// 应当保证该减少值大于0,返回实际改变量
  237. /// </summary>
  238. public int SubPositiveV(int subPositiveV)
  239. {
  240. lock (vLock)
  241. {
  242. subPositiveV = Math.Min(subPositiveV, v);
  243. v -= subPositiveV;
  244. }
  245. return subPositiveV;
  246. }
  247. /// <summary>
  248. /// 试图加到满,如果无法加到maxValue则不加并返回-1
  249. /// </summary>
  250. public int TryAddAll(int addV)
  251. {
  252. lock (vLock)
  253. {
  254. if (maxV - v <= addV)
  255. {
  256. addV = maxV - v;
  257. v = maxV;
  258. return addV;
  259. }
  260. return 0;
  261. }
  262. }
  263. }
  264. /// <summary>
  265. /// 一个保证在[0,maxValue]的可变long,支持可变的maxValue(请确保大于0)
  266. /// </summary>
  267. public struct LongWithVariableRange
  268. {
  269. private long v;
  270. private long maxV;
  271. private readonly object vLock = new();
  272. public LongWithVariableRange(long value, long maxValue)
  273. {
  274. if (maxValue < 0)
  275. {
  276. Debugger.Output("Warning:Try to set LongWithVariableRange.maxValue to " + maxValue.ToString() + ".");
  277. maxValue = 0;
  278. }
  279. v = value < maxValue ? value : maxValue;
  280. this.maxV = maxValue;
  281. }
  282. public LongWithVariableRange(long maxValue)
  283. {
  284. if (maxValue < 0)
  285. {
  286. Debugger.Output("Warning:Try to set LongWithVariableRange.maxValue to " + maxValue.ToString() + ".");
  287. maxValue = 0;
  288. }
  289. v = this.maxV = maxValue;
  290. }
  291. public override string ToString()
  292. {
  293. lock (vLock)
  294. {
  295. return "value:" + v.ToString() + " ,maxValue:" + maxV.ToString();
  296. }
  297. }
  298. public long GetValue() { lock (vLock) return v; }
  299. public static implicit operator long(LongWithVariableRange aint) => aint.GetValue();
  300. public long GetMaxV() { lock (vLock) return maxV; }
  301. /// <summary>
  302. /// 若maxValue<=0则maxValue设为0并返回False
  303. /// </summary>
  304. public bool SetMaxV(long maxValue)
  305. {
  306. if (maxValue < 0) maxValue = 0;
  307. lock (vLock)
  308. {
  309. maxV = maxValue;
  310. if (v > maxValue) v = maxValue;
  311. }
  312. return maxValue > 0;
  313. }
  314. /// <summary>
  315. /// 应当保证该maxValue>=0
  316. /// </summary>
  317. public void SetPositiveMaxV(long maxValue)
  318. {
  319. lock (vLock)
  320. {
  321. maxV = maxValue;
  322. if (v > maxValue) v = maxValue;
  323. }
  324. }
  325. /// <summary>
  326. /// 应当保证该value>=0
  327. /// </summary>
  328. public long SetPositiveV(long value)
  329. {
  330. lock (vLock)
  331. {
  332. return v = (value > maxV) ? maxV : value;
  333. }
  334. }
  335. public long SetV(long value)
  336. {
  337. if (value < 0) value = 0;
  338. lock (vLock)
  339. {
  340. return v = (value > maxV) ? maxV : value;
  341. }
  342. }
  343. /// <summary>
  344. /// 返回实际改变量
  345. /// </summary>
  346. public long AddV(long addV)
  347. {
  348. lock (vLock)
  349. {
  350. long previousV = v;
  351. v += addV;
  352. if (v < 0) v = 0;
  353. if (v > maxV) v = maxV;
  354. return v - previousV;
  355. }
  356. }
  357. /// <summary>
  358. /// 应当保证该增加值大于0,返回实际改变量
  359. /// </summary>
  360. public long AddPositiveV(long addPositiveV)
  361. {
  362. lock (vLock)
  363. {
  364. addPositiveV = Math.Min(addPositiveV, maxV - v);
  365. v += addPositiveV;
  366. }
  367. return addPositiveV;
  368. }
  369. /// <summary>
  370. /// 应当保证该减少值大于0,返回实际改变量
  371. /// </summary>
  372. public long SubPositiveV(long subPositiveV)
  373. {
  374. lock (vLock)
  375. {
  376. subPositiveV = Math.Min(subPositiveV, v);
  377. v -= subPositiveV;
  378. }
  379. return subPositiveV;
  380. }
  381. /// <summary>
  382. /// 试图加到满,如果无法加到maxValue则不加并返回-1
  383. /// </summary>
  384. public long TryAddAll(long addV)
  385. {
  386. lock (vLock)
  387. {
  388. if (maxV - v <= addV)
  389. {
  390. addV = maxV - v;
  391. v = maxV;
  392. return addV;
  393. }
  394. return -1;
  395. }
  396. }
  397. }
  398. /// <summary>
  399. /// 一个保证在[0,maxNum],每CDms自动更新的可变int,支持可变的CD、maxNum(请确保大于0)
  400. /// </summary>
  401. public struct IntNumUpdateByCD
  402. {
  403. private int num;
  404. private int maxNum;
  405. private int cd;
  406. private long updateTime = 0;
  407. private object numLock = new();
  408. public IntNumUpdateByCD(int num, int maxNum, int cd)
  409. {
  410. if (num < 0) Debugger.Output("Bug:IntNumUpdateByCD.num (" + num.ToString() + ") is less than 0.");
  411. if (maxNum < 0) Debugger.Output("Bug:IntNumUpdateByCD.maxNum (" + maxNum.ToString() + ") is less than 0.");
  412. if (cd <= 0) Debugger.Output("Bug:IntNumUpdateByCD.cd (" + cd.ToString() + ") is less than 0.");
  413. this.num = (num < maxNum) ? num : maxNum;
  414. this.maxNum = maxNum;
  415. this.cd = cd;
  416. this.updateTime = Environment.TickCount64;
  417. }
  418. public IntNumUpdateByCD(int maxNum, int cd)
  419. {
  420. if (maxNum < 0) Debugger.Output("Bug:IntNumUpdateByCD.maxNum (" + maxNum.ToString() + ") is less than 0.");
  421. if (cd <= 0) Debugger.Output("Bug:IntNumUpdateByCD.cd (" + cd.ToString() + ") is less than 0.");
  422. this.num = this.maxNum = maxNum;
  423. this.cd = cd;
  424. }
  425. public int GetMaxNum() { lock (numLock) return maxNum; }
  426. public int GetCD() { lock (numLock) return cd; }
  427. public int GetNum(long time)
  428. {
  429. lock (numLock)
  430. {
  431. if (num < maxNum && time - updateTime >= cd)
  432. {
  433. int add = (int)Math.Min(maxNum - num, (time - updateTime) / cd);
  434. updateTime += add * cd;
  435. return (num += add);
  436. }
  437. return num;
  438. }
  439. }
  440. public static implicit operator int(IntNumUpdateByCD aint) => aint.GetNum(Environment.TickCount64);
  441. /// <summary>
  442. /// 应当保证该subV>=0
  443. /// </summary>
  444. public int TrySub(int subV)
  445. {
  446. if (subV < 0) Debugger.Output("Bug:IntNumUpdateByCD Try to sub " + subV.ToString() + ", which is less than 0.");
  447. long time = Environment.TickCount64;
  448. lock (numLock)
  449. {
  450. if (num < maxNum && time - updateTime >= cd)
  451. {
  452. int add = (int)Math.Min(maxNum - num, (time - updateTime) / cd);
  453. updateTime += add * cd;
  454. num += add;
  455. }
  456. if (num == maxNum) updateTime = time;
  457. num -= subV = Math.Min(subV, num);
  458. }
  459. return subV;
  460. }
  461. /// <summary>
  462. /// 应当保证该addV>=0
  463. /// </summary>
  464. public void TryAdd(int addV)
  465. {
  466. if (addV < 0) Debugger.Output("Bug:IntNumUpdateByCD Try to add " + addV.ToString() + ", which is less than 0.");
  467. lock (numLock)
  468. {
  469. num += Math.Min(addV, maxNum - num);
  470. }
  471. }
  472. /// <summary>
  473. /// 若maxNum<=0则maxNum及Num设为0并返回False
  474. /// </summary>
  475. public bool SetMaxNumAndNum(int maxNum)
  476. {
  477. if (maxNum < 0) maxNum = 0;
  478. lock (numLock)
  479. {
  480. this.num = this.maxNum = maxNum;
  481. }
  482. return maxNum > 0;
  483. }
  484. /// <summary>
  485. /// 应当保证该maxnum>=0
  486. /// </summary>
  487. public void SetPositiveMaxNumAndNum(int maxNum)
  488. {
  489. lock (numLock)
  490. {
  491. this.num = this.maxNum = maxNum;
  492. }
  493. }
  494. /// <summary>
  495. /// 应当保证该maxnum>=0
  496. /// </summary>
  497. public void SetPositiveMaxNum(int maxNum)
  498. {
  499. lock (numLock)
  500. {
  501. if ((this.maxNum = maxNum) < num)
  502. num = maxNum;
  503. }
  504. }
  505. /// <summary>
  506. /// 若maxNum<=0则maxNum及Num设为0并返回False
  507. /// </summary>
  508. public bool SetMaxNum(int maxNum)
  509. {
  510. if (maxNum < 0) maxNum = 0;
  511. lock (numLock)
  512. {
  513. if ((this.maxNum = maxNum) < num)
  514. num = maxNum;
  515. }
  516. return maxNum > 0;
  517. }
  518. /// <summary>
  519. /// 若num<0则num设为0并返回False
  520. /// </summary>
  521. public bool SetNum(int num)
  522. {
  523. lock (numLock)
  524. {
  525. if (num < 0)
  526. {
  527. this.num = 0;
  528. updateTime = Environment.TickCount64;
  529. return false;
  530. }
  531. if (num < maxNum)
  532. {
  533. if (this.num == maxNum) updateTime = Environment.TickCount64;
  534. this.num = num;
  535. }
  536. else this.num = maxNum;
  537. return true;
  538. }
  539. }
  540. /// <summary>
  541. /// 应当保证该num>=0
  542. /// </summary>
  543. public void SetPositiveNum(int num)
  544. {
  545. lock (numLock)
  546. {
  547. if (num < maxNum)
  548. {
  549. if (this.num == maxNum) updateTime = Environment.TickCount64;
  550. this.num = num;
  551. }
  552. else this.num = maxNum;
  553. }
  554. }
  555. public void SetCD(int cd)
  556. {
  557. lock (numLock)
  558. {
  559. if (cd <= 0) Debugger.Output("Bug:Set IntNumUpdateByCD.cd to " + cd.ToString() + ".");
  560. this.cd = cd;
  561. }
  562. }
  563. }
  564. }