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.

TimeBased.cs 15 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. using System;
  2. using System.Threading;
  3. namespace Preparation.Utility
  4. {
  5. //其对应属性不应当有set访问器,避免不安全的=赋值
  6. /// <summary>
  7. /// 根据时间推算Start后完成多少进度的进度条(long)。
  8. /// 只允许Start(清零状态的进度条才可以Start)时修改needTime(请确保大于0);
  9. /// 支持InterruptToSet0使未完成的进度条终止清零;支持Set0使进度条强制终止清零;
  10. /// 通过原子操作实现。
  11. /// </summary>
  12. public class TimeBasedProgressForInterrupting
  13. {
  14. private long endT = long.MaxValue;
  15. private long needT;
  16. public TimeBasedProgressForInterrupting(long needTime)
  17. {
  18. if (needTime <= 0) Debugger.Output("Bug:TimeBasedProgressForInterrupting.needTime (" + needTime.ToString() + ") is less than 0.");
  19. this.needT = needTime;
  20. }
  21. public TimeBasedProgressForInterrupting()
  22. {
  23. this.needT = 0;
  24. }
  25. public long GetEndTime() => Interlocked.CompareExchange(ref endT, -2, -2);
  26. public long GetNeedTime() => Interlocked.CompareExchange(ref needT, -2, -2);
  27. public override string ToString() => "EndTime:" + Interlocked.CompareExchange(ref endT, -2, -2).ToString() + " ms, NeedTime:" + Interlocked.CompareExchange(ref needT, -2, -2).ToString() + " ms";
  28. public bool IsFinished()
  29. {
  30. return Interlocked.CompareExchange(ref endT, -2, -2) <= Environment.TickCount64;
  31. }
  32. public bool IsOpened() => Interlocked.Read(ref endT) != long.MaxValue;
  33. /// <summary>
  34. /// GetProgress<0则表明未开始
  35. /// </summary>
  36. public long GetProgress()
  37. {
  38. long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - Environment.TickCount64;
  39. if (cutime <= 0) return Interlocked.CompareExchange(ref needT, -2, -2);
  40. return Interlocked.CompareExchange(ref needT, -2, -2) - cutime;
  41. }
  42. public long GetNonNegativeProgress()
  43. {
  44. long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - Environment.TickCount64;
  45. if (cutime <= 0) return Interlocked.CompareExchange(ref needT, -2, -2);
  46. long progress = Interlocked.CompareExchange(ref needT, -2, -2) - cutime;
  47. return progress < 0 ? 0 : progress;
  48. }
  49. /// <summary>
  50. /// GetProgress<0则表明未开始
  51. /// </summary>
  52. public long GetProgress(long time)
  53. {
  54. long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - time;
  55. if (cutime <= 0) return Interlocked.CompareExchange(ref needT, -2, -2);
  56. return Interlocked.CompareExchange(ref needT, -2, -2) - cutime;
  57. }
  58. public long GetNonNegativeProgress(long time)
  59. {
  60. long cutime = Interlocked.Read(ref endT) - time;
  61. if (cutime <= 0) return Interlocked.CompareExchange(ref needT, -2, -2);
  62. long progress = Interlocked.CompareExchange(ref needT, -2, -2) - cutime;
  63. return progress < 0 ? 0 : progress;
  64. }
  65. /// <summary>
  66. /// <0则表明未开始
  67. /// </summary>
  68. public static implicit operator long(TimeBasedProgressForInterrupting pLong) => pLong.GetProgress();
  69. /// <summary>
  70. /// GetProgressDouble<0则表明未开始
  71. /// </summary>
  72. public double GetProgressDouble()
  73. {
  74. long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - Environment.TickCount64;
  75. if (cutime <= 0) return 1;
  76. long needTime = Interlocked.CompareExchange(ref needT, -2, -2);
  77. if (needTime == 0) return 0;
  78. return 1.0 - ((double)cutime / needTime);
  79. }
  80. public double GetNonNegativeProgressDouble(long time)
  81. {
  82. long cutime = Interlocked.Read(ref endT) - time;
  83. if (cutime <= 0) return 1;
  84. long needTime = Interlocked.CompareExchange(ref needT, -2, -2);
  85. if (needTime <= cutime) return 0;
  86. return 1.0 - ((double)cutime / needTime);
  87. }
  88. public bool Start(long needTime)
  89. {
  90. if (needTime <= 0)
  91. {
  92. Debugger.Output("Warning:Start TimeBasedProgressForInterrupting 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 TimeBasedProgressForInterrupting is " + needTime.ToString() + ",which is too small.");
  98. Interlocked.Exchange(ref 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. /// <summary>
  108. /// 使进度条强制终止清零
  109. /// </summary>
  110. public void Set0() => Interlocked.Exchange(ref endT, long.MaxValue);
  111. /// <summary>
  112. /// 使未完成的进度条终止清零
  113. /// </summary>
  114. public bool InterruptToSet0()
  115. {
  116. if (Environment.TickCount64 < Interlocked.CompareExchange(ref endT, -2, -2))
  117. {
  118. Interlocked.Exchange(ref endT, long.MaxValue);
  119. return true;
  120. }
  121. return false;
  122. }
  123. //增加其他新的写操作可能导致不安全
  124. }
  125. /// <summary>
  126. /// 冷却时间为可变的CDms的bool,不支持查看当前进度,初始为True
  127. /// </summary>
  128. public class BoolUpdateEachCD
  129. {
  130. private long cd;
  131. private long nextUpdateTime = 0;
  132. public BoolUpdateEachCD(int cd)
  133. {
  134. if (cd <= 1) Debugger.Output("Bug:BoolUpdateEachCD.cd (" + cd.ToString() + ") is less than 1.");
  135. this.cd = cd;
  136. }
  137. public BoolUpdateEachCD(long cd)
  138. {
  139. if (cd <= 1) Debugger.Output("Bug:BoolUpdateEachCD.cd (" + cd.ToString() + ") is less than 1.");
  140. this.cd = cd;
  141. }
  142. public BoolUpdateEachCD(long cd, long startTime)
  143. {
  144. if (cd <= 1) Debugger.Output("Bug:BoolUpdateEachCD.cd (" + cd.ToString() + ") is less than 1.");
  145. this.cd = cd;
  146. this.nextUpdateTime = startTime;
  147. }
  148. public long GetCD() => Interlocked.Read(ref cd);
  149. public bool TryUse()
  150. {
  151. long needTime = Interlocked.Exchange(ref nextUpdateTime, long.MaxValue);
  152. if (needTime <= Environment.TickCount64)
  153. {
  154. Interlocked.Exchange(ref nextUpdateTime, Environment.TickCount64 + Interlocked.Read(ref cd));
  155. return true;
  156. }
  157. Interlocked.Exchange(ref nextUpdateTime, needTime);
  158. return false;
  159. }
  160. public void SetCD(int cd)
  161. {
  162. if (cd <= 1) Debugger.Output("Bug:BoolUpdateEachCD.cd to " + cd.ToString() + ".");
  163. Interlocked.Exchange(ref this.cd, cd);
  164. }
  165. }
  166. /// <summary>
  167. /// 冷却时间为可变的CDms的进度条,初始为满
  168. /// </summary>
  169. public class LongProgressUpdateEachCD
  170. {
  171. private int isusing = 0;
  172. private long cd;
  173. private long nextUpdateTime = 0;
  174. public LongProgressUpdateEachCD(int cd)
  175. {
  176. if (cd <= 1) Debugger.Output("Bug:LongProgressUpdateEachCD.cd (" + cd.ToString() + ") is less than 1.");
  177. this.cd = cd;
  178. }
  179. public LongProgressUpdateEachCD(long cd)
  180. {
  181. if (cd <= 1) Debugger.Output("Bug:LongProgressUpdateEachCD.cd (" + cd.ToString() + ") is less than 1.");
  182. this.cd = cd;
  183. }
  184. public LongProgressUpdateEachCD(long cd, long startTime)
  185. {
  186. if (cd <= 1) Debugger.Output("Bug:LongProgressUpdateEachCD.cd (" + cd.ToString() + ") is less than 1.");
  187. this.cd = cd;
  188. this.nextUpdateTime = startTime;
  189. }
  190. public long GetRemainingTime()
  191. {
  192. long v = Interlocked.Read(ref nextUpdateTime) - Environment.TickCount64;
  193. return v < 0 ? 0 : v;
  194. }
  195. public long GetCD() => Interlocked.Read(ref cd);
  196. public bool TryUse()
  197. {
  198. if (Interlocked.Exchange(ref isusing, 1) == 1) return false;
  199. long needTime = Interlocked.Read(ref nextUpdateTime);
  200. if (needTime <= Environment.TickCount64)
  201. {
  202. Interlocked.Exchange(ref nextUpdateTime, Environment.TickCount64 + Interlocked.Read(ref cd));
  203. Interlocked.Exchange(ref isusing, 0);
  204. return true;
  205. }
  206. Interlocked.Exchange(ref isusing, 0);
  207. return false;
  208. }
  209. public void SetCD(int cd)
  210. {
  211. if (cd <= 1) Debugger.Output("Bug:Set LongProgressUpdateEachCD.cd to " + cd.ToString() + ".");
  212. Interlocked.Exchange(ref this.cd, cd);
  213. }
  214. }
  215. /// <summary>
  216. /// 一个保证在[0,maxNum],每CDms自动+1的int,支持可变的CD、maxNum(请确保大于0)
  217. /// </summary>
  218. public class IntNumUpdateEachCD
  219. {
  220. private int num;
  221. private int maxNum;
  222. private int cd;
  223. private long updateTime = 0;
  224. private readonly object numLock = new();
  225. public IntNumUpdateEachCD(int num, int maxNum, int cd)
  226. {
  227. if (num < 0) Debugger.Output("Bug:IntNumUpdateEachCD.num (" + num.ToString() + ") is less than 0.");
  228. if (maxNum < 0) Debugger.Output("Bug:IntNumUpdateEachCD.maxNum (" + maxNum.ToString() + ") is less than 0.");
  229. if (cd <= 0) Debugger.Output("Bug:IntNumUpdateEachCD.cd (" + cd.ToString() + ") is less than 0.");
  230. this.num = (num < maxNum) ? num : maxNum;
  231. this.maxNum = maxNum;
  232. this.cd = cd;
  233. this.updateTime = Environment.TickCount64;
  234. }
  235. /// <summary>
  236. /// 默认使num=maxNum
  237. /// </summary>
  238. public IntNumUpdateEachCD(int maxNum, int cd)
  239. {
  240. if (maxNum < 0) Debugger.Output("Bug:IntNumUpdateEachCD.maxNum (" + maxNum.ToString() + ") is less than 0.");
  241. if (cd <= 0) Debugger.Output("Bug:IntNumUpdateEachCD.cd (" + cd.ToString() + ") is less than 0.");
  242. this.num = this.maxNum = maxNum;
  243. this.cd = cd;
  244. }
  245. public IntNumUpdateEachCD()
  246. {
  247. this.num = this.maxNum = 0;
  248. this.cd = int.MaxValue;
  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. public static implicit operator int(IntNumUpdateEachCD aint) => aint.GetNum(Environment.TickCount64);
  266. /// <summary>
  267. /// 应当保证该subV>=0
  268. /// </summary>
  269. public int TrySub(int subV)
  270. {
  271. if (subV < 0) Debugger.Output("Bug:IntNumUpdateEachCD Try to sub " + subV.ToString() + ", which is less than 0.");
  272. long time = Environment.TickCount64;
  273. lock (numLock)
  274. {
  275. if (num < maxNum && time - updateTime >= cd)
  276. {
  277. int add = (int)Math.Min(maxNum - num, (time - updateTime) / cd);
  278. updateTime += add * cd;
  279. num += add;
  280. }
  281. if (num == maxNum) updateTime = time;
  282. num -= subV = Math.Min(subV, num);
  283. }
  284. return subV;
  285. }
  286. /// <summary>
  287. /// 应当保证该addV>=0
  288. /// </summary>
  289. public void TryAdd(int addV)
  290. {
  291. if (addV < 0) Debugger.Output("Bug:IntNumUpdateEachCD Try to add " + addV.ToString() + ", which is less than 0.");
  292. lock (numLock)
  293. {
  294. num += Math.Min(addV, maxNum - num);
  295. }
  296. }
  297. /// <summary>
  298. /// 若maxNum<=0则maxNum及Num设为0并返回False
  299. /// </summary>
  300. public bool SetMaxNumAndNum(int maxNum)
  301. {
  302. if (maxNum < 0) maxNum = 0;
  303. lock (numLock)
  304. {
  305. this.num = this.maxNum = maxNum;
  306. }
  307. return maxNum > 0;
  308. }
  309. /// <summary>
  310. /// 应当保证该maxnum>=0
  311. /// </summary>
  312. public void SetPositiveMaxNumAndNum(int maxNum)
  313. {
  314. lock (numLock)
  315. {
  316. this.num = this.maxNum = maxNum;
  317. }
  318. }
  319. /// <summary>
  320. /// 应当保证该maxnum>=0
  321. /// </summary>
  322. public void SetPositiveMaxNum(int maxNum)
  323. {
  324. lock (numLock)
  325. {
  326. if ((this.maxNum = maxNum) < num)
  327. num = maxNum;
  328. }
  329. }
  330. /// <summary>
  331. /// 若maxNum<=0则maxNum及Num设为0并返回False
  332. /// </summary>
  333. public bool SetMaxNum(int maxNum)
  334. {
  335. if (maxNum < 0) maxNum = 0;
  336. lock (numLock)
  337. {
  338. if ((this.maxNum = maxNum) < num)
  339. num = maxNum;
  340. }
  341. return maxNum > 0;
  342. }
  343. /// <summary>
  344. /// 若num<0则num设为0并返回False
  345. /// </summary>
  346. public bool SetNum(int num)
  347. {
  348. lock (numLock)
  349. {
  350. if (num < 0)
  351. {
  352. this.num = 0;
  353. updateTime = Environment.TickCount64;
  354. return false;
  355. }
  356. if (num < maxNum)
  357. {
  358. if (this.num == maxNum) updateTime = Environment.TickCount64;
  359. this.num = num;
  360. }
  361. else this.num = maxNum;
  362. return true;
  363. }
  364. }
  365. /// <summary>
  366. /// 应当保证该num>=0
  367. /// </summary>
  368. public void SetPositiveNum(int num)
  369. {
  370. lock (numLock)
  371. {
  372. if (num < maxNum)
  373. {
  374. if (this.num == maxNum) updateTime = Environment.TickCount64;
  375. this.num = num;
  376. }
  377. else this.num = maxNum;
  378. }
  379. }
  380. public void SetCD(int cd)
  381. {
  382. lock (numLock)
  383. {
  384. if (cd <= 0) Debugger.Output("Bug:Set IntNumUpdateEachCD.cd to " + cd.ToString() + ".");
  385. this.cd = cd;
  386. }
  387. }
  388. }
  389. }