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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. public int AddV(int addV)
  187. {
  188. lock (vLock)
  189. {
  190. v += addV;
  191. if (v < 0) return v = 0;
  192. if (v > maxV) return v = maxV;
  193. return v;
  194. }
  195. }
  196. /// <summary>
  197. /// 应当保证该增加值大于0
  198. /// </summary>
  199. public int AddPositiveV(int addPositiveV)
  200. {
  201. lock (vLock)
  202. {
  203. v += addPositiveV;
  204. if (v > maxV) return v = maxV;
  205. return v;
  206. }
  207. }
  208. /// <summary>
  209. /// 应当保证该减少值大于0
  210. /// </summary>
  211. public int SubPositiveV(int subPositiveV)
  212. {
  213. lock (vLock)
  214. {
  215. v += subPositiveV;
  216. if (v < 0) return v = 0;
  217. return v;
  218. }
  219. }
  220. }
  221. /// <summary>
  222. /// 一个保证在[0,maxNum],每CDms自动更新的可变int,支持可变的CD、maxNum(请确保大于0)
  223. /// </summary>
  224. public struct IntNumUpdateByCD
  225. {
  226. private int num;
  227. private int maxNum;
  228. private int cd;
  229. private long updateTime = 0;
  230. private object numLock = new();
  231. public IntNumUpdateByCD(int num, int maxNum, int cd)
  232. {
  233. if (num < 0) Debugger.Output("Bug:IntNumUpdateByCD.num (" + num.ToString() + ") is less than 0.");
  234. if (maxNum < 0) Debugger.Output("Bug:IntNumUpdateByCD.maxNum (" + maxNum.ToString() + ") is less than 0.");
  235. if (cd <= 0) Debugger.Output("Bug:IntNumUpdateByCD.cd (" + cd.ToString() + ") is less than 0.");
  236. this.num = num;
  237. this.maxNum = maxNum;
  238. this.cd = cd;
  239. this.updateTime = Environment.TickCount64;
  240. }
  241. public IntNumUpdateByCD(int maxNum, int cd)
  242. {
  243. if (maxNum < 0) Debugger.Output("Bug:IntNumUpdateByCD.maxNum (" + maxNum.ToString() + ") is less than 0.");
  244. if (cd <= 0) Debugger.Output("Bug:IntNumUpdateByCD.cd (" + cd.ToString() + ") is less than 0.");
  245. this.num = this.maxNum = maxNum;
  246. this.cd = cd;
  247. }
  248. public int GetMaxNum() { lock (numLock) return maxNum; }
  249. public int GetCD() { lock (numLock) return cd; }
  250. public int GetNum(long time)
  251. {
  252. lock (numLock)
  253. {
  254. if (num < maxNum && time - updateTime >= cd)
  255. {
  256. int add = (int)Math.Min(maxNum - num, (time - updateTime) / cd);
  257. updateTime += add * cd;
  258. return (num += add);
  259. }
  260. return num;
  261. }
  262. }
  263. /// <summary>
  264. /// 应当保证该subV>=0
  265. /// </summary>
  266. public int TrySub(int subV)
  267. {
  268. if (subV < 0) Debugger.Output("Bug:IntNumUpdateByCD Try to sub " + subV.ToString() + ", which is less than 0.");
  269. long time = Environment.TickCount64;
  270. lock (numLock)
  271. {
  272. if (num < maxNum && time - updateTime >= cd)
  273. {
  274. int add = (int)Math.Min(maxNum - num, (time - updateTime) / cd);
  275. updateTime += add * cd;
  276. num += add;
  277. }
  278. if (num == maxNum) updateTime = time;
  279. num -= subV = Math.Min(subV, num);
  280. }
  281. return subV;
  282. }
  283. /// <summary>
  284. /// 应当保证该addV>=0
  285. /// </summary>
  286. public void TryAdd(int addV)
  287. {
  288. if (addV < 0) Debugger.Output("Bug:IntNumUpdateByCD Try to add " + addV.ToString() + ", which is less than 0.");
  289. lock (numLock)
  290. {
  291. num += Math.Min(addV, maxNum - num);
  292. }
  293. }
  294. /// <summary>
  295. /// 若maxNum<=0则maxNum及Num设为0并返回False
  296. /// </summary>
  297. public bool SetMaxNumAndNum(int maxNum)
  298. {
  299. if (maxNum < 0) maxNum = 0;
  300. lock (numLock)
  301. {
  302. this.num = this.maxNum = maxNum;
  303. }
  304. return maxNum > 0;
  305. }
  306. /// <summary>
  307. /// 应当保证该maxnum>=0
  308. /// </summary>
  309. public void SetPositiveMaxNumAndNum(int maxNum)
  310. {
  311. lock (numLock)
  312. {
  313. this.num = this.maxNum = maxNum;
  314. }
  315. }
  316. /// <summary>
  317. /// 应当保证该maxnum>=0
  318. /// </summary>
  319. public void SetPositiveMaxNum(int maxNum)
  320. {
  321. lock (numLock)
  322. {
  323. if ((this.maxNum = maxNum) < num)
  324. num = maxNum;
  325. }
  326. }
  327. /// <summary>
  328. /// 若maxNum<=0则maxNum及Num设为0并返回False
  329. /// </summary>
  330. public bool SetMaxNum(int maxNum)
  331. {
  332. if (maxNum < 0) maxNum = 0;
  333. lock (numLock)
  334. {
  335. if ((this.maxNum = maxNum) < num)
  336. num = maxNum;
  337. }
  338. return maxNum > 0;
  339. }
  340. /// <summary>
  341. /// 若num<0则num设为0并返回False
  342. /// </summary>
  343. public bool SetNum(int num)
  344. {
  345. lock (numLock)
  346. {
  347. if (num < 0) { this.num = 0; return false; }
  348. this.num = num;
  349. return true;
  350. }
  351. }
  352. /// <summary>
  353. /// 应当保证该num>=0
  354. /// </summary>
  355. public void SetPositiveNum(int num)
  356. {
  357. lock (numLock)
  358. {
  359. this.num = num;
  360. }
  361. }
  362. public void SetCD(int cd)
  363. {
  364. lock (numLock)
  365. {
  366. if (cd <= 0) Debugger.Output("Bug:Set IntNumUpdateByCD.cd to " + cd.ToString() + ".");
  367. this.cd = cd;
  368. }
  369. }
  370. }
  371. }