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.

SafeValueLong.cs 5.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using System;
  2. using System.Threading;
  3. namespace Preparation.Utility
  4. {
  5. //其对应属性不应当有set访问器,避免不安全的=赋值
  6. public class AtomicLong
  7. {
  8. private long v;
  9. public AtomicLong(long x)
  10. {
  11. v = x;
  12. }
  13. public override string ToString() => Interlocked.Read(ref v).ToString();
  14. public long Get() => Interlocked.Read(ref v);
  15. public static implicit operator long(AtomicLong aint) => Interlocked.Read(ref aint.v);
  16. /// <returns>返回操作前的值</returns>
  17. public long SetReturnOri(long value) => Interlocked.Exchange(ref v, value);
  18. public long Add(long x) => Interlocked.Add(ref v, x);
  19. public long Sub(long x) => Interlocked.Add(ref v, -x);
  20. public long Inc() => Interlocked.Increment(ref v);
  21. public long Dec() => Interlocked.Decrement(ref v);
  22. /// <returns>返回操作前的值</returns>
  23. public long CompareExReturnOri(long newV, long compareTo) => Interlocked.CompareExchange(ref v, newV, compareTo);
  24. }
  25. /// <summary>
  26. /// 一个保证在[0,maxValue]的可变long,支持可变的maxValue(请确保大于0)
  27. /// </summary>
  28. public class LongWithVariableRange
  29. {
  30. private long v;
  31. private long maxV;
  32. private readonly object vLock = new();
  33. public LongWithVariableRange(long value, long maxValue)
  34. {
  35. if (maxValue < 0)
  36. {
  37. Debugger.Output("Warning:Try to set SafaValues.LongWithVariableRange.maxValue to " + maxValue.ToString() + ".");
  38. maxValue = 0;
  39. }
  40. v = value < maxValue ? value : maxValue;
  41. this.maxV = maxValue;
  42. }
  43. /// <summary>
  44. /// 默认使Value=maxValue
  45. /// </summary>
  46. public LongWithVariableRange(long maxValue)
  47. {
  48. if (maxValue < 0)
  49. {
  50. Debugger.Output("Warning:Try to set SafaValues.LongWithVariableRange.maxValue to " + maxValue.ToString() + ".");
  51. maxValue = 0;
  52. }
  53. v = this.maxV = maxValue;
  54. }
  55. public LongWithVariableRange()
  56. {
  57. v = this.maxV = long.MaxValue;
  58. }
  59. public override string ToString()
  60. {
  61. lock (vLock)
  62. {
  63. return "value:" + v.ToString() + " ,maxValue:" + maxV.ToString();
  64. }
  65. }
  66. public long GetValue() { lock (vLock) return v; }
  67. public static implicit operator long(LongWithVariableRange aint) => aint.GetValue();
  68. public long GetMaxV() { lock (vLock) return maxV; }
  69. /// <summary>
  70. /// 若maxValue<=0则maxValue设为0并返回False
  71. /// </summary>
  72. public bool SetMaxV(long maxValue)
  73. {
  74. if (maxValue < 0) maxValue = 0;
  75. lock (vLock)
  76. {
  77. maxV = maxValue;
  78. if (v > maxValue) v = maxValue;
  79. }
  80. return maxValue > 0;
  81. }
  82. /// <summary>
  83. /// 应当保证该maxValue>=0
  84. /// </summary>
  85. public void SetPositiveMaxV(long maxValue)
  86. {
  87. lock (vLock)
  88. {
  89. maxV = maxValue;
  90. if (v > maxValue) v = maxValue;
  91. }
  92. }
  93. /// <summary>
  94. /// 应当保证该value>=0
  95. /// </summary>
  96. public long SetPositiveV(long value)
  97. {
  98. lock (vLock)
  99. {
  100. return v = (value > maxV) ? maxV : value;
  101. }
  102. }
  103. public long SetV(long value)
  104. {
  105. if (value < 0) value = 0;
  106. lock (vLock)
  107. {
  108. return v = (value > maxV) ? maxV : value;
  109. }
  110. }
  111. /// <summary>
  112. /// 返回实际改变量
  113. /// </summary>
  114. public long AddV(long addV)
  115. {
  116. lock (vLock)
  117. {
  118. long previousV = v;
  119. v += addV;
  120. if (v < 0) v = 0;
  121. if (v > maxV) v = maxV;
  122. return v - previousV;
  123. }
  124. }
  125. /// <summary>
  126. /// 应当保证该增加值大于0,返回实际改变量
  127. /// </summary>
  128. public long AddPositiveV(long addPositiveV)
  129. {
  130. lock (vLock)
  131. {
  132. addPositiveV = Math.Min(addPositiveV, maxV - v);
  133. v += addPositiveV;
  134. }
  135. return addPositiveV;
  136. }
  137. /// <summary>
  138. /// 应当保证该减少值大于0,返回实际改变量
  139. /// </summary>
  140. public long SubPositiveV(long subPositiveV)
  141. {
  142. lock (vLock)
  143. {
  144. subPositiveV = Math.Min(subPositiveV, v);
  145. v -= subPositiveV;
  146. }
  147. return subPositiveV;
  148. }
  149. /// <summary>
  150. /// 试图加到满,如果无法加到maxValue则不加并返回-1
  151. /// </summary>
  152. public long TryAddAll(long addV)
  153. {
  154. lock (vLock)
  155. {
  156. if (maxV - v <= addV)
  157. {
  158. addV = maxV - v;
  159. v = maxV;
  160. return addV;
  161. }
  162. return -1;
  163. }
  164. }
  165. }
  166. }