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

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