atomic.h 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #pragma once
  2. #include <util/system/defaults.h>
  3. using TAtomicBase = intptr_t;
  4. using TAtomic = volatile TAtomicBase;
  5. #if defined(__GNUC__)
  6. #include "atomic_gcc.h"
  7. #elif defined(_MSC_VER)
  8. #include "atomic_win.h"
  9. #else
  10. #error unsupported platform
  11. #endif
  12. #if !defined(ATOMIC_COMPILER_BARRIER)
  13. #define ATOMIC_COMPILER_BARRIER()
  14. #endif
  15. static inline TAtomicBase AtomicSub(TAtomic& a, TAtomicBase v) {
  16. return AtomicAdd(a, -v);
  17. }
  18. static inline TAtomicBase AtomicGetAndSub(TAtomic& a, TAtomicBase v) {
  19. return AtomicGetAndAdd(a, -v);
  20. }
  21. #if defined(USE_GENERIC_SETGET)
  22. static inline TAtomicBase AtomicGet(const TAtomic& a) {
  23. return a;
  24. }
  25. static inline void AtomicSet(TAtomic& a, TAtomicBase v) {
  26. a = v;
  27. }
  28. #endif
  29. static inline bool AtomicTryLock(TAtomic* a) {
  30. return AtomicCas(a, 1, 0);
  31. }
  32. static inline bool AtomicTryAndTryLock(TAtomic* a) {
  33. return (AtomicGet(*a) == 0) && AtomicTryLock(a);
  34. }
  35. static inline void AtomicUnlock(TAtomic* a) {
  36. ATOMIC_COMPILER_BARRIER();
  37. AtomicSet(*a, 0);
  38. }
  39. #include "atomic_ops.h"