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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 aint) => Interlocked.Read(ref aint.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 AtomicBool
  45. {
  46. private int v;//v==0为false,v==1为true
  47. public AtomicBool(bool x)
  48. {
  49. v = x ? 1 : 0;
  50. }
  51. public override string ToString() => (Interlocked.CompareExchange(ref v, -2, -2) == 0) ? "false" : "true";
  52. public bool Get() => (Interlocked.CompareExchange(ref v, -1, -1) != 0);
  53. public static implicit operator bool(AtomicBool abool) => (Interlocked.CompareExchange(ref abool.v, -1, -1) != 0);
  54. /// <returns>返回操作前的值</returns>
  55. public bool SetReturnOri(bool value) => (Interlocked.Exchange(ref v, value ? 1 : 0) != 0);
  56. /// <returns>赋值前的值是否与将赋予的值不相同</returns>
  57. public bool TrySet(bool value)
  58. {
  59. return (Interlocked.CompareExchange(ref v, value ? 1 : 0, value ? 0 : 1) ^ (value ? 1 : 0)) != 0;
  60. }
  61. public bool And(bool x) => Interlocked.And(ref v, x ? 1 : 0) != 0;
  62. public bool Or(bool x) => Interlocked.Or(ref v, x ? 1 : 0) != 0;
  63. }
  64. }