atomic_box.h 719 B

12345678910111213141516171819202122232425262728293031323334
  1. #pragma once
  2. #include <library/cpp/deprecated/atomic/atomic.h>
  3. // TAtomic with human interface
  4. template <typename T>
  5. class TAtomicBox {
  6. private:
  7. union {
  8. TAtomic Value;
  9. // when T is enum, it is convenient to inspect its content in gdb
  10. T ValueForDebugger;
  11. };
  12. static_assert(sizeof(T) <= sizeof(TAtomic), "expect sizeof(T) <= sizeof(TAtomic)");
  13. public:
  14. TAtomicBox(T value = T())
  15. : Value(value)
  16. {
  17. }
  18. void Set(T value) {
  19. AtomicSet(Value, (TAtomic)value);
  20. }
  21. T Get() const {
  22. return (T)AtomicGet(Value);
  23. }
  24. bool CompareAndSet(T expected, T set) {
  25. return AtomicCas(&Value, (TAtomicBase)set, (TAtomicBase)expected);
  26. }
  27. };