-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlifecycle_v2.h
More file actions
187 lines (150 loc) · 4.12 KB
/
Copy pathlifecycle_v2.h
File metadata and controls
187 lines (150 loc) · 4.12 KB
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
177
178
179
180
181
182
183
184
185
186
187
#pragma once
#include <mutex>
#include <condition_variable>
#include <vector>
#include <thread>
#include <stdexcept>
#include <atomic>
class lifecycle final {
public:
lifecycle() {
}
~lifecycle() {
if (_use_count > 0) {
// lifecycle `lock` and `unlock` are not paired
// dtor should not throw exception, so just abort
std::abort();
}
}
void release() {
_released = true;
auto pos = find_thread_state();
if (pos != thread_states.end()) {
// remove current thread use count
if (pos->dec_use_count) {
pos->dec_use_count = false;
_use_count--;
}
if (_use_count == 0) {
_cond.notify_all();
}
}
std::unique_lock<std::mutex> lock{ _mutex };
_cond.wait(lock, [this] {
return _use_count == 0;
});
}
bool lock(bool& already_locked) {
if (_released) {
return false;
}
auto pos = find_thread_state();
if (pos != thread_states.end()) {
already_locked = true;
return true;
}
thread_state state = {this};
thread_states.push_back(state);
_use_count++;
if (_released) {
unlock(false);
return false;
}
already_locked = false;
return true;
}
void unlock(bool already_locked) {
if (already_locked) {
return;
}
auto pos = find_thread_state();
if (pos == thread_states.end()) {
throw std::logic_error{"lifecycle `unlock` isn't paired with `lock` in the same thread"};
}
if (pos->dec_use_count) {
_use_count--;
}
thread_states.erase(pos);
if (_released && _use_count == 0) {
_cond.notify_all();
}
}
private:
std::mutex _mutex;
std::condition_variable _cond;
std::atomic<int32_t> _use_count = 0;
std::atomic_bool _released = false;
struct thread_state {
lifecycle* lc;
bool dec_use_count = true;
};
static thread_local std::vector<thread_state> thread_states;
private:
std::vector<thread_state>::iterator find_thread_state() {
for (auto iter = thread_states.begin(); iter != thread_states.end(); iter++) {
if (iter->lc == this) {
return iter;
}
}
return thread_states.end();
}
};
template<class T>
class object_lifecycle {
public:
object_lifecycle(T* obj) : _obj(obj) {
if (_obj == nullptr) {
throw std::logic_error{ "object_lifecycle obj is nullptr" };
}
}
void release() {
_lc.release();
}
T* lock(bool& already_locked) {
return _lc.lock(already_locked) ? _obj : nullptr;
}
void unlock(bool already_locked) {
return _lc.unlock(already_locked);
}
private:
lifecycle _lc;
T* _obj;
};
template<class T>
auto make_lifecycle(T* t) {
return std::make_shared<object_lifecycle<T>>(t);
}
template<class T>
using object_lifecycle_ptr = std::shared_ptr<object_lifecycle<T>>;
// usage:
// if (auto obj = use_object(olc); obj) {
// // use obj
// }
template<class T>
class object_wrapper {
public:
explicit object_wrapper(const object_lifecycle_ptr<T>& lc) : _lc(lc) {
_obj = _lc->lock(_already_locked);
}
~object_wrapper() {
if (_obj) {
_lc->unlock(_already_locked);
}
}
operator bool() const {
return _obj != nullptr;
}
T* operator->() const {
return _obj;
}
object_wrapper(const object_wrapper&) = delete;
object_wrapper& operator=(const object_wrapper&) = delete;
private:
object_lifecycle_ptr<T> _lc;
bool _already_locked;
T* _obj;
};
template<class T>
auto use_object(const object_lifecycle_ptr<T>& lc) {
return object_wrapper<T>{lc};
}