aboutsummaryrefslogtreecommitdiffstats
path: root/util/system/guard.h
blob: d47a1a90ebb4201f3ebc654075df884f50a52acf (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#pragma once

#include <util/generic/noncopyable.h>
 
template <class T> 
struct TCommonLockOps { 
    static inline void Acquire(T* t) noexcept {
        t->Acquire(); 
    } 
 
    static inline void Release(T* t) noexcept {
        t->Release(); 
    } 
}; 
 
template <class T>
struct TTryLockOps: public TCommonLockOps<T> { 
    static inline bool TryAcquire(T* t) noexcept {
        return t->TryAcquire();
    }
};

//must be used with great care 
template <class TOps>
struct TInverseLockOps: public TOps { 
    template <class T>
    static inline void Acquire(T* t) noexcept {
        TOps::Release(t);
    }

    template <class T>
    static inline void Release(T* t) noexcept {
        TOps::Acquire(t);
    }
};

template <class T, class TOps = TCommonLockOps<T>> 
class TGuard: public TNonCopyable { 
public: 
    inline TGuard(const T& t) noexcept {
        Init(&t); 
    } 
 
    inline TGuard(const T* t) noexcept {
        Init(t); 
    } 
 
    inline TGuard(TGuard&& g) noexcept
        : T_(g.T_) 
    { 
        g.T_ = nullptr;
    } 
 
    inline ~TGuard() {
        Release(); 
    } 
 
    inline void Release() noexcept {
        if (WasAcquired()) { 
            TOps::Release(T_); 
            T_ = nullptr;
        } 
    } 
 
    explicit inline operator bool() const noexcept {
        return WasAcquired(); 
    } 
 
    inline bool WasAcquired() const noexcept {
        return T_ != nullptr;
    } 
 
    inline T* GetMutex() const noexcept {
        return T_; 
    } 
 
private: 
    inline void Init(const T* t) noexcept {
        T_ = const_cast<T*>(t); 
        TOps::Acquire(T_); 
    } 
 
private: 
    T* T_; 
}; 

/* 
 * { 
 *     auto guard = Guard(Lock_); 
 *     some code under guard 
 * } 
 */ 
template <class T> 
static inline TGuard<T> Guard(const T& t) { 
    return {&t}; 
} 
 
/* 
 * with_lock (Lock_) { 
 *     some code under guard 
 * } 
 */ 
#define with_lock(X)                                              \ 
    if (auto Y_GENERATE_UNIQUE_ID(__guard) = ::Guard(X); false) { \ 
    } else 
 
/* 
 * auto guard = Guard(Lock_); 
 * ... some code under lock 
 * { 
 *     auto unguard = Unguard(guard); 
 *     ... some code not under lock 
 * } 
 * ... some code under lock 
 */ 
template <class T, class TOps = TCommonLockOps<T>> 
using TInverseGuard = TGuard<T, TInverseLockOps<TOps>>; 

template <class T, class TOps> 
static inline TInverseGuard<T, TOps> Unguard(const TGuard<T, TOps>& guard) { 
    return {guard.GetMutex()}; 
} 
 
template <class T> 
static inline TInverseGuard<T> Unguard(const T& mutex) { 
    return {&mutex}; 
} 
 
template <class T, class TOps = TTryLockOps<T>> 
class TTryGuard: public TNonCopyable {
public: 
    inline TTryGuard(const T& t) noexcept {
        Init(&t); 
    } 

    inline TTryGuard(const T* t) noexcept {
        Init(t); 
    } 

    inline TTryGuard(TTryGuard&& g) noexcept
        : T_(g.T_)
    {
        g.T_ = nullptr;
    }

    inline ~TTryGuard() {
        Release(); 
    } 

    inline void Release() noexcept {
        if (WasAcquired()) { 
            TOps::Release(T_); 
            T_ = nullptr;
        }
    } 

    inline bool WasAcquired() const noexcept {
        return T_ != nullptr;
    } 

    explicit inline operator bool() const noexcept {
        return WasAcquired(); 
    } 
 
private: 
    inline void Init(const T* t) noexcept {
        T_ = nullptr;
        T* tMutable = const_cast<T*>(t); 
        if (TOps::TryAcquire(tMutable)) { 
            T_ = tMutable; 
        }
    } 

private: 
    T* T_; 
};