aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/deprecated/atomic/atomic.h
blob: b376f34efa065b5113e66bd6dd392617c8c6751d (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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#pragma once

#include <util/system/defaults.h>

using TAtomicBase = intptr_t;
using TAtomic = volatile TAtomicBase;

#if defined(__GNUC__)
    #include "atomic_gcc.h"
#elif defined(_MSC_VER)
    #include "atomic_win.h"
#else
    #error unsupported platform
#endif

#if !defined(ATOMIC_COMPILER_BARRIER)
    #define ATOMIC_COMPILER_BARRIER()
#endif

static inline TAtomicBase AtomicSub(TAtomic& a, TAtomicBase v) {
    return AtomicAdd(a, -v);
}

static inline TAtomicBase AtomicGetAndSub(TAtomic& a, TAtomicBase v) {
    return AtomicGetAndAdd(a, -v);
}

#if defined(USE_GENERIC_SETGET)
static inline TAtomicBase AtomicGet(const TAtomic& a) {
    return a;
}

static inline void AtomicSet(TAtomic& a, TAtomicBase v) {
    a = v;
}
#endif

static inline bool AtomicTryLock(TAtomic* a) {
    return AtomicCas(a, 1, 0);
}

static inline bool AtomicTryAndTryLock(TAtomic* a) {
    return (AtomicGet(*a) == 0) && AtomicTryLock(a);
}

static inline void AtomicUnlock(TAtomic* a) {
    ATOMIC_COMPILER_BARRIER();
    AtomicSet(*a, 0);
}

#include "atomic_ops.h"