aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/messagebus/misc/atomic_box.h
blob: 401621f93353f7e7ed2e66786de8d4a6dcb5eba4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#pragma once

#include <util/system/atomic.h>

// TAtomic with human interface
template <typename T>
class TAtomicBox {
private:
    union {
        TAtomic Value;
        // when T is enum, it is convenient to inspect its content in gdb
        T ValueForDebugger;
    };

    static_assert(sizeof(T) <= sizeof(TAtomic), "expect sizeof(T) <= sizeof(TAtomic)");

public:
    TAtomicBox(T value = T())
        : Value(value)
    {
    }

    void Set(T value) {
        AtomicSet(Value, (TAtomic)value);
    }

    T Get() const {
        return (T)AtomicGet(Value);
    }

    bool CompareAndSet(T expected, T set) {
        return AtomicCas(&Value, (TAtomicBase)set, (TAtomicBase)expected);
    }
};