-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlifecycle.h
More file actions
195 lines (157 loc) · 4.52 KB
/
lifecycle.h
File metadata and controls
195 lines (157 loc) · 4.52 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
188
189
190
191
192
193
194
195
#pragma once
#include <mutex>
#include <condition_variable>
#include <vector>
#include <thread>
#include <stdexcept>
class lifecycle final {
public:
lifecycle() : _owner_threads() {
_owner_threads.reserve(32);
}
~lifecycle() {
std::unique_lock<std::mutex> lock{ _mutex };
if (_use_count > 0) {
// lifecycle `lock` and `unlock` is not paired
// dtor should not throw exception, so just abort
std::abort();
}
}
void release() {
std::unique_lock<std::mutex> lock{ _mutex };
_released = true;
auto pos = find_owner_thread();
if (pos != _owner_threads.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();
}
}
_cond.wait(lock, [this] {
return _use_count == 0;
});
}
bool lock(bool& already_locked) {
std::unique_lock<std::mutex> lock{ _mutex };
if (_released) {
return false;
}
auto pos = find_owner_thread();
if (pos != _owner_threads.end()) {
already_locked = true;
return true;
}
owner_thread item{};
_owner_threads.push_back(item);
_use_count++;
already_locked = false;
return true;
}
void unlock(bool already_locked) {
if (already_locked) {
return;
}
std::unique_lock<std::mutex> lock{ _mutex };
auto pos = find_owner_thread();
if (pos == _owner_threads.end()) {
throw std::logic_error{"lifecycle `unlock` isn't paired with `lock` in the same thread"};
}
if (pos->dec_use_count) {
_use_count--;
}
_owner_threads.erase(pos);
if (_released && _use_count == 0) {
_cond.notify_all();
}
}
private:
std::mutex _mutex;
std::condition_variable _cond;
int32_t _use_count = 0;
bool _released = false;
struct owner_thread {
std::thread::id id = std::this_thread::get_id();
bool dec_use_count = true;
};
std::vector<owner_thread> _owner_threads;
private:
// already locked
std::vector<owner_thread>::iterator find_owner_thread() {
auto tid = std::this_thread::get_id();
return std::find_if(_owner_threads.begin(), _owner_threads.end(),
[tid](const auto& item) {
return item.id == tid;
}
);
}
};
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(object_lifecycle_ptr<T> lc) : _lc(lc) {
_id = std::this_thread::get_id();
_obj = _lc->lock(_already_locked);
}
~object_wrapper() {
if (std::this_thread::get_id() != _id) {
// object_wrapper ctor and dtor are not in the same thread,
// dtor should not throw exception, so just abort
std::abort();
}
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:
std::thread::id _id;
object_lifecycle_ptr<T> _lc;
bool _already_locked;
T* _obj;
};
template<class T>
auto use_object(object_lifecycle_ptr<T> lc) {
return object_wrapper<T>{lc};
}