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.

SafeValueTime.cs 9.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. using System;
  2. using System.Threading;
  3. namespace Preparation.Utility
  4. {
  5. //其对应属性不应当有set访问器,避免不安全的=赋值
  6. /// <summary>
  7. /// 根据时间推算Start后完成多少进度的进度条(long)。
  8. /// 只允许Start时修改needTime(请确保大于0);
  9. /// 支持TrySet0使未完成的进度条终止清零;支持Set0使进度条强制终止清零;
  10. /// 通过原子操作实现。
  11. /// </summary>
  12. public class LongProgressByTime
  13. {
  14. private long endT = long.MaxValue;
  15. private long needT;
  16. public LongProgressByTime(long needTime)
  17. {
  18. if (needTime <= 0) Debugger.Output("Bug:LongProgressByTime.needTime (" + needTime.ToString() + ") is less than 0.");
  19. this.needT = needTime;
  20. }
  21. public LongProgressByTime()
  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(LongProgressByTime 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 LongProgressByTime 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 LongProgressByTime 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 TrySet0()
  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,支持可变的无锁CD,不支持查看当前进度,初始为True
  127. /// </summary>
  128. public class BoolCoolingDownByCD
  129. {
  130. private long cd;
  131. private long nextUpdateTime = 0;
  132. public BoolCoolingDownByCD(int cd)
  133. {
  134. if (cd <= 1) Debugger.Output("Bug:IntNumUpdateByCD.cd (" + cd.ToString() + ") is less than 1.");
  135. this.cd = cd;
  136. }
  137. public BoolCoolingDownByCD(long cd)
  138. {
  139. if (cd <= 1) Debugger.Output("Bug:IntNumUpdateByCD.cd (" + cd.ToString() + ") is less than 1.");
  140. this.cd = cd;
  141. }
  142. public BoolCoolingDownByCD(long cd, long startTime)
  143. {
  144. if (cd <= 1) Debugger.Output("Bug:IntNumUpdateByCD.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:SetReturnOri IntNumUpdateByCD.cd to " + cd.ToString() + ".");
  163. Interlocked.Exchange(ref this.cd, cd);
  164. }
  165. }
  166. /// <summary>
  167. /// 一个每CDms自动更新的进度条,支持可变的CD,初始为满
  168. /// </summary>
  169. public class LongProgressCoolingDownByCD
  170. {
  171. private int isusing = 0;
  172. private long cd;
  173. private long nextUpdateTime = 0;
  174. public LongProgressCoolingDownByCD(int cd)
  175. {
  176. if (cd <= 1) Debugger.Output("Bug:IntNumUpdateByCD.cd (" + cd.ToString() + ") is less than 1.");
  177. this.cd = cd;
  178. }
  179. public LongProgressCoolingDownByCD(long cd)
  180. {
  181. if (cd <= 1) Debugger.Output("Bug:IntNumUpdateByCD.cd (" + cd.ToString() + ") is less than 1.");
  182. this.cd = cd;
  183. }
  184. public LongProgressCoolingDownByCD(long cd, long startTime)
  185. {
  186. if (cd <= 1) Debugger.Output("Bug:IntNumUpdateByCD.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:SetReturnOri IntNumUpdateByCD.cd to " + cd.ToString() + ".");
  212. Interlocked.Exchange(ref this.cd, cd);
  213. }
  214. }
  215. }