atomic_win.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #pragma once
  2. #include <intrin.h>
  3. #define USE_GENERIC_SETGET
  4. #if defined(_i386_)
  5. #pragma intrinsic(_InterlockedIncrement)
  6. #pragma intrinsic(_InterlockedDecrement)
  7. #pragma intrinsic(_InterlockedExchangeAdd)
  8. #pragma intrinsic(_InterlockedExchange)
  9. #pragma intrinsic(_InterlockedCompareExchange)
  10. static inline intptr_t AtomicIncrement(TAtomic& a) {
  11. return _InterlockedIncrement((volatile long*)&a);
  12. }
  13. static inline intptr_t AtomicGetAndIncrement(TAtomic& a) {
  14. return _InterlockedIncrement((volatile long*)&a) - 1;
  15. }
  16. static inline intptr_t AtomicDecrement(TAtomic& a) {
  17. return _InterlockedDecrement((volatile long*)&a);
  18. }
  19. static inline intptr_t AtomicGetAndDecrement(TAtomic& a) {
  20. return _InterlockedDecrement((volatile long*)&a) + 1;
  21. }
  22. static inline intptr_t AtomicAdd(TAtomic& a, intptr_t b) {
  23. return _InterlockedExchangeAdd((volatile long*)&a, b) + b;
  24. }
  25. static inline intptr_t AtomicGetAndAdd(TAtomic& a, intptr_t b) {
  26. return _InterlockedExchangeAdd((volatile long*)&a, b);
  27. }
  28. static inline intptr_t AtomicSwap(TAtomic* a, intptr_t b) {
  29. return _InterlockedExchange((volatile long*)a, b);
  30. }
  31. static inline bool AtomicCas(TAtomic* a, intptr_t exchange, intptr_t compare) {
  32. return _InterlockedCompareExchange((volatile long*)a, exchange, compare) == compare;
  33. }
  34. static inline intptr_t AtomicGetAndCas(TAtomic* a, intptr_t exchange, intptr_t compare) {
  35. return _InterlockedCompareExchange((volatile long*)a, exchange, compare);
  36. }
  37. #else // _x86_64_
  38. #pragma intrinsic(_InterlockedIncrement64)
  39. #pragma intrinsic(_InterlockedDecrement64)
  40. #pragma intrinsic(_InterlockedExchangeAdd64)
  41. #pragma intrinsic(_InterlockedExchange64)
  42. #pragma intrinsic(_InterlockedCompareExchange64)
  43. static inline intptr_t AtomicIncrement(TAtomic& a) {
  44. return _InterlockedIncrement64((volatile __int64*)&a);
  45. }
  46. static inline intptr_t AtomicGetAndIncrement(TAtomic& a) {
  47. return _InterlockedIncrement64((volatile __int64*)&a) - 1;
  48. }
  49. static inline intptr_t AtomicDecrement(TAtomic& a) {
  50. return _InterlockedDecrement64((volatile __int64*)&a);
  51. }
  52. static inline intptr_t AtomicGetAndDecrement(TAtomic& a) {
  53. return _InterlockedDecrement64((volatile __int64*)&a) + 1;
  54. }
  55. static inline intptr_t AtomicAdd(TAtomic& a, intptr_t b) {
  56. return _InterlockedExchangeAdd64((volatile __int64*)&a, b) + b;
  57. }
  58. static inline intptr_t AtomicGetAndAdd(TAtomic& a, intptr_t b) {
  59. return _InterlockedExchangeAdd64((volatile __int64*)&a, b);
  60. }
  61. static inline intptr_t AtomicSwap(TAtomic* a, intptr_t b) {
  62. return _InterlockedExchange64((volatile __int64*)a, b);
  63. }
  64. static inline bool AtomicCas(TAtomic* a, intptr_t exchange, intptr_t compare) {
  65. return _InterlockedCompareExchange64((volatile __int64*)a, exchange, compare) == compare;
  66. }
  67. static inline intptr_t AtomicGetAndCas(TAtomic* a, intptr_t exchange, intptr_t compare) {
  68. return _InterlockedCompareExchange64((volatile __int64*)a, exchange, compare);
  69. }
  70. static inline intptr_t AtomicOr(TAtomic& a, intptr_t b) {
  71. return _InterlockedOr64(&a, b) | b;
  72. }
  73. static inline intptr_t AtomicAnd(TAtomic& a, intptr_t b) {
  74. return _InterlockedAnd64(&a, b) & b;
  75. }
  76. static inline intptr_t AtomicXor(TAtomic& a, intptr_t b) {
  77. return _InterlockedXor64(&a, b) ^ b;
  78. }
  79. #endif // _x86_
  80. //TODO
  81. static inline void AtomicBarrier() {
  82. TAtomic val = 0;
  83. AtomicSwap(&val, 0);
  84. }