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 4.0 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using System;
  2. using System.Threading;
  3. namespace Preparation.Utility
  4. {
  5. //其对应属性不应当有set访问器,避免不安全的=赋值
  6. public class AtomicInt
  7. {
  8. private int v;
  9. public AtomicInt(int x)
  10. {
  11. v = x;
  12. }
  13. public override string ToString() => Interlocked.CompareExchange(ref v, -1, -1).ToString();
  14. public int Get() => Interlocked.CompareExchange(ref v, -1, -1);
  15. public static implicit operator int(AtomicInt aint) => Interlocked.CompareExchange(ref aint.v, -1, -1);
  16. /// <returns>返回操作前的值</returns>
  17. public int SetReturnOri(int value) => Interlocked.Exchange(ref v, value);
  18. public int Add(int x) => Interlocked.Add(ref v, x);
  19. public int Sub(int x) => Interlocked.Add(ref v, -x);
  20. public int Inc() => Interlocked.Increment(ref v);
  21. public int Dec() => Interlocked.Decrement(ref v);
  22. /// <returns>返回操作前的值</returns>
  23. public int CompareExReturnOri(int newV, int compareTo) => Interlocked.CompareExchange(ref v, newV, compareTo);
  24. }
  25. public class AtomicLong
  26. {
  27. private long v;
  28. public AtomicLong(long x)
  29. {
  30. v = x;
  31. }
  32. public override string ToString() => Interlocked.Read(ref v).ToString();
  33. public long Get() => Interlocked.Read(ref v);
  34. public static implicit operator long(AtomicLong along) => Interlocked.Read(ref along.v);
  35. /// <returns>返回操作前的值</returns>
  36. public long SetReturnOri(long value) => Interlocked.Exchange(ref v, value);
  37. public long Add(long x) => Interlocked.Add(ref v, x);
  38. public long Sub(long x) => Interlocked.Add(ref v, -x);
  39. public long Inc() => Interlocked.Increment(ref v);
  40. public long Dec() => Interlocked.Decrement(ref v);
  41. /// <returns>返回操作前的值</returns>
  42. public long CompareExReturnOri(long newV, long compareTo) => Interlocked.CompareExchange(ref v, newV, compareTo);
  43. }
  44. public class AtomicDouble
  45. {
  46. private double v;
  47. public AtomicDouble(double x)
  48. {
  49. v = x;
  50. }
  51. public override string ToString() => Interlocked.CompareExchange(ref v, -2.0, -2.0).ToString();
  52. public double Get() => Interlocked.CompareExchange(ref v, -2.0, -2.0);
  53. public static implicit operator double(AtomicDouble adouble) => Interlocked.CompareExchange(ref adouble.v, -2.0, -2.0);
  54. /// <returns>返回操作前的值</returns>
  55. public double SetReturnOri(double value) => Interlocked.Exchange(ref v, value);
  56. /// <returns>返回操作前的值</returns>
  57. public double CompareExReturnOri(double newV, double compareTo) => Interlocked.CompareExchange(ref v, newV, compareTo);
  58. }
  59. public class AtomicBool
  60. {
  61. private int v;//v&1==0为false,v&1==1为true
  62. public AtomicBool(bool x)
  63. {
  64. v = x ? 1 : 0;
  65. }
  66. public override string ToString() => ((Interlocked.CompareExchange(ref v, -2, -2) & 1) == 0) ? "false" : "true";
  67. public bool Get() => ((Interlocked.CompareExchange(ref v, -2, -2) & 1) == 1);
  68. public static implicit operator bool(AtomicBool abool) => abool.Get();
  69. /// <returns>返回操作前的值</returns>
  70. public bool SetReturnOri(bool value) => ((Interlocked.Exchange(ref v, value ? 1 : 0) & 1) == 1);
  71. /// <returns>赋值前的值是否与将赋予的值不相同</returns>
  72. public bool TrySet(bool value)
  73. {
  74. return ((Interlocked.Exchange(ref v, value ? 1 : 0) & 1) != (value ? 1 : 0));
  75. }
  76. public bool And(bool x) => (Interlocked.And(ref v, x ? 1 : 0) & 1) == 1;
  77. public bool Or(bool x) => (Interlocked.Or(ref v, x ? 1 : 0) & 1) == 1;
  78. /// <returns>返回操作后的值</returns>
  79. public bool Reverse() => (Interlocked.Increment(ref v) & 1) == 1;
  80. public bool Xor(bool x) => (Interlocked.Add(ref v, x ? 1 : 0) & 1) == 1;
  81. }
  82. }