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.

Atomic.cs 9.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. using Google.Protobuf.WellKnownTypes;
  2. using System;
  3. using System.Threading;
  4. namespace Preparation.Utility
  5. {
  6. //其对应属性不应当有set访问器,避免不安全的=赋值
  7. public abstract class Atomic
  8. {
  9. }
  10. public class AtomicInt : Atomic
  11. {
  12. protected int v;
  13. public AtomicInt(int x)
  14. {
  15. v = x;
  16. }
  17. public override string ToString() => Interlocked.CompareExchange(ref v, -1, -1).ToString();
  18. public int Get() => Interlocked.CompareExchange(ref v, -1, -1);
  19. public static implicit operator int(AtomicInt aint) => Interlocked.CompareExchange(ref aint.v, -1, -1);
  20. /// <returns>返回操作前的值</returns>
  21. public virtual int SetReturnOri(int value) => Interlocked.Exchange(ref v, value);
  22. public virtual int Add(int x) => Interlocked.Add(ref v, x);
  23. /// <summary>
  24. /// 注意:确保参数为非负数
  25. /// </summary>
  26. public virtual int AddPositive(int x) => Interlocked.Add(ref v, x);
  27. public virtual int Sub(int x) => Interlocked.Add(ref v, -x);
  28. /// <summary>
  29. /// 注意:确保参数为非负数
  30. /// </summary>
  31. public virtual int SubPositive(int x) => Interlocked.Add(ref v, -x);
  32. public virtual int Inc() => Interlocked.Increment(ref v);
  33. public int Dec() => Interlocked.Decrement(ref v);
  34. /// <returns>返回操作前的值</returns>
  35. public virtual int CompareExReturnOri(int newV, int compareTo) => Interlocked.CompareExchange(ref v, newV, compareTo);
  36. }
  37. /// <summary>
  38. /// 参数要求倍率speed(默认1)以及AtomicInt类的Score,
  39. /// 在发生正向的变化时,自动给Score加上正向变化的差乘以speed。
  40. /// 注意:AtomicIntOnlyAddScore本身为AtomicInt,提供的Score可能构成环而死锁。
  41. /// </summary>
  42. public class AtomicIntOnlyAddScore : AtomicInt
  43. {
  44. public AtomicInt Score { get; set; };
  45. public AtomicDouble speed;
  46. public AtomicIntOnlyAddScore(int x, AtomicInt Score, double speed = 1.0) : base(x)
  47. {
  48. this.Score = Score;
  49. this.speed = new(speed);
  50. }
  51. /// <returns>返回操作前的值</returns>
  52. public override int SetReturnOri(int value)
  53. {
  54. int previousV = Interlocked.Exchange(ref v, value);
  55. if (value - previousV > 0)
  56. Score.AddPositive((int)(speed * (value - previousV)));
  57. return previousV;
  58. }
  59. public override int Add(int x)
  60. {
  61. if (x > 0) Score.AddPositive((int)(speed * x));
  62. return Interlocked.Add(ref v, x);
  63. }
  64. /// <summary>
  65. /// 注意:确保参数为非负数
  66. /// </summary>
  67. public override int AddPositive(int x)
  68. {
  69. Score.AddPositive((int)(speed * x));
  70. return Interlocked.Add(ref v, x);
  71. }
  72. public override int Sub(int x)
  73. {
  74. if (x < 0) Score.AddPositive((int)(speed * -x));
  75. return Interlocked.Add(ref v, -x);
  76. }
  77. /// <summary>
  78. /// 注意:确保参数为非负数
  79. /// </summary>
  80. public override int SubPositive(int x)
  81. {
  82. return Interlocked.Add(ref v, -x);
  83. }
  84. public override int Inc()
  85. {
  86. Score.AddPositive((int)(speed));
  87. return Interlocked.Increment(ref v);
  88. }
  89. /// <returns>返回操作前的值</returns>
  90. public override int CompareExReturnOri(int newV, int compareTo)
  91. {
  92. int previousV = Interlocked.CompareExchange(ref v, newV, compareTo);
  93. if (newV - previousV > 0)
  94. Score.AddPositive((int)(speed * (newV - previousV)));
  95. return previousV;
  96. }
  97. }
  98. public class AtomicLong : Atomic
  99. {
  100. protected long v;
  101. public AtomicLong(long x)
  102. {
  103. v = x;
  104. }
  105. public override string ToString() => Interlocked.CompareExchange(ref v, -1, -1).ToString();
  106. public long Get() => Interlocked.CompareExchange(ref v, -1, -1);
  107. public static implicit operator long(AtomicLong along) => Interlocked.CompareExchange(ref along.v, -1, -1);
  108. /// <returns>返回操作前的值</returns>
  109. public virtual long SetReturnOri(long value) => Interlocked.Exchange(ref v, value);
  110. public virtual long Add(long x) => Interlocked.Add(ref v, x);
  111. /// <summary>
  112. /// 注意:确保参数为非负数
  113. /// </summary>
  114. public virtual long AddPositive(long x) => Interlocked.Add(ref v, x);
  115. public virtual long Sub(long x) => Interlocked.Add(ref v, -x);
  116. /// <summary>
  117. /// 注意:确保参数为非负数
  118. /// </summary>
  119. public virtual long SubPositive(long x) => Interlocked.Add(ref v, -x);
  120. public virtual long Inc() => Interlocked.Increment(ref v);
  121. public long Dec() => Interlocked.Decrement(ref v);
  122. /// <returns>返回操作前的值</returns>
  123. public virtual long CompareExReturnOri(long newV, long compareTo) => Interlocked.CompareExchange(ref v, newV, compareTo);
  124. }
  125. /// <summary>
  126. /// 参数要求倍率speed(默认1)以及AtomicLong类的Score,
  127. /// 在发生正向的变化时,自动给Score加上正向变化的差乘以speed。
  128. /// 注意:AtomicLongOnlyAddScore本身为AtomicLong,提供的Score可能构成环而死锁。
  129. /// </summary>
  130. public class AtomicLongOnlyAddScore : AtomicLong
  131. {
  132. public AtomicInt Score { get; set; };
  133. public AtomicDouble speed;
  134. public AtomicLongOnlyAddScore(long x, AtomicLong Score, double speed = 1.0) : base(x)
  135. {
  136. this.Score = Score;
  137. this.speed = new(speed);
  138. }
  139. /// <returns>返回操作前的值</returns>
  140. public override long SetReturnOri(long value)
  141. {
  142. long previousV = Interlocked.Exchange(ref v, value);
  143. if (value - previousV > 0)
  144. Score.AddPositive((long)(speed * (value - previousV)));
  145. return previousV;
  146. }
  147. public override long Add(long x)
  148. {
  149. if (x > 0) Score.AddPositive((long)(speed * x));
  150. return Interlocked.Add(ref v, x);
  151. }
  152. /// <summary>
  153. /// 注意:确保参数为非负数
  154. /// </summary>
  155. public override long AddPositive(long x)
  156. {
  157. Score.AddPositive((long)(speed * x));
  158. return Interlocked.Add(ref v, x);
  159. }
  160. public override long Sub(long x)
  161. {
  162. if (x < 0) Score.AddPositive((long)(speed * -x));
  163. return Interlocked.Add(ref v, -x);
  164. }
  165. /// <summary>
  166. /// 注意:确保参数为非负数
  167. /// </summary>
  168. public override long SubPositive(long x)
  169. {
  170. return Interlocked.Add(ref v, -x);
  171. }
  172. public override long Inc()
  173. {
  174. Score.AddPositive((long)(speed));
  175. return Interlocked.Increment(ref v);
  176. }
  177. /// <returns>返回操作前的值</returns>
  178. public override long CompareExReturnOri(long newV, long compareTo)
  179. {
  180. long previousV = Interlocked.CompareExchange(ref v, newV, compareTo);
  181. if (newV - previousV > 0)
  182. Score.AddPositive((long)(speed * (newV - previousV)));
  183. return previousV;
  184. }
  185. }
  186. public class AtomicDouble : Atomic
  187. {
  188. private double v;
  189. public AtomicDouble(double x)
  190. {
  191. v = x;
  192. }
  193. public override string ToString() => Interlocked.CompareExchange(ref v, -2.0, -2.0).ToString();
  194. public double Get() => Interlocked.CompareExchange(ref v, -2.0, -2.0);
  195. public static implicit operator double(AtomicDouble adouble) => Interlocked.CompareExchange(ref adouble.v, -2.0, -2.0);
  196. /// <returns>返回操作前的值</returns>
  197. public double SetReturnOri(double value) => Interlocked.Exchange(ref v, value);
  198. /// <returns>返回操作前的值</returns>
  199. public double CompareExReturnOri(double newV, double compareTo) => Interlocked.CompareExchange(ref v, newV, compareTo);
  200. }
  201. public class AtomicBool : Atomic
  202. {
  203. private int v;//v&1==0为false,v&1==1为true
  204. public AtomicBool(bool x)
  205. {
  206. v = x ? 1 : 0;
  207. }
  208. public override string ToString() => ((Interlocked.CompareExchange(ref v, -2, -2) & 1) == 0) ? "false" : "true";
  209. public bool Get() => ((Interlocked.CompareExchange(ref v, -2, -2) & 1) == 1);
  210. public static implicit operator bool(AtomicBool abool) => abool.Get();
  211. /// <returns>返回操作前的值</returns>
  212. public bool SetReturnOri(bool value) => ((Interlocked.Exchange(ref v, value ? 1 : 0) & 1) == 1);
  213. /// <returns>赋值前的值是否与将赋予的值不相同</returns>
  214. public bool TrySet(bool value)
  215. {
  216. return ((Interlocked.Exchange(ref v, value ? 1 : 0) & 1) != (value ? 1 : 0));
  217. }
  218. public bool And(bool x) => (Interlocked.And(ref v, x ? 1 : 0) & 1) == 1;
  219. public bool Or(bool x) => (Interlocked.Or(ref v, x ? 1 : 0) & 1) == 1;
  220. /// <returns>返回操作后的值</returns>
  221. public bool Reverse() => (Interlocked.Increment(ref v) & 1) == 1;
  222. public bool Xor(bool x) => (Interlocked.Add(ref v, x ? 1 : 0) & 1) == 1;
  223. }
  224. }