future
7.61 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#pragma once
#if __cplusplus < 201103L
#error "C++ version lower than C++11"
#endif
#include <mutex>
#include <condition_variable>
#include <memory>
#include <chrono>
#include <cassert>
namespace std {
enum class future_status {
ready,
timeout,
deferred
};
namespace detail {
class shared_state_base {
protected:
typedef void (*deleter_fn)(void *v);
using scoped_lock = std::lock_guard<std::mutex>;
using unique_lock = std::unique_lock<std::mutex>;
public:
explicit shared_state_base(deleter_fn d) : v_(nullptr), d_(d), valid_(true) {}
~shared_state_base() { d_(v_); }
shared_state_base(shared_state_base &&other) = delete;
shared_state_base(const shared_state_base &other) = delete;
shared_state_base &operator=(shared_state_base &&other) = delete;
shared_state_base &operator=(const shared_state_base &other) = delete;
void wait() {
unique_lock lock(m_);
c_.wait(lock, [this] { return has_value(); });
}
template <class Rep, class Period>
std::future_status
wait_for(const std::chrono::duration<Rep, Period> &rel_time) {
unique_lock lock(m_);
if (c_.wait_for(lock, rel_time, [this] { return has_value(); })) {
return std::future_status::ready;
}
return std::future_status::timeout;
}
template <class Clock, class Duration>
std::future_status
wait_until(const std::chrono::time_point<Clock, Duration> &abs_time) {
unique_lock lock(m_);
if (c_.wait_until(lock, abs_time, [this] { return has_value(); })) {
return std::future_status::ready;
}
return std::future_status::timeout;
}
protected:
bool has_value() { return v_ != nullptr; }
protected:
std::mutex m_;
std::condition_variable c_;
void *v_;
deleter_fn d_;
bool valid_;
};
template <typename R>
class shared_state: public shared_state_base {
public:
shared_state() :shared_state_base(default_deleter_) {}
~shared_state() {}
R &get() {
wait();
scoped_lock lock(m_);
assert(valid_);
valid_ = false;
return *(static_cast<R *>(v_));
}
void set(const R &v) {
scoped_lock lock(m_);
assert(!has_value());
v_ = new R(v);
valid_ = true;
c_.notify_one();
}
void set(R &&v) {
scoped_lock lock(m_);
assert(!has_value());
v_ = new R(std::move(v));
valid_ = true;
c_.notify_one();
}
bool valid() {
scoped_lock lock(m_);
return valid_;
}
private:
static void default_deleter_(void *v) { delete static_cast<R *>(v); }
};
} // namespace detail
template <typename R>
class shared_future {
};
template <typename R>
class future {
using state_type = std::shared_ptr<detail::shared_state<R>>;
public:
future() {}
explicit future(const state_type &state) : state_(state) {}
future(future &&other) noexcept: state_(std::move(other.state_)) {
other.state_.reset();
}
future(const future &other) = delete;
~future() {}
future &operator=(future &&other) noexcept {
if (&other != this) {
state_ = std::move(other.state_);
other.state_.reset();
}
return *this;
}
future &operator=(const future &other) = delete;
void swap(future &other) noexcept {
std::swap(state_, other.state_);
}
std::shared_future<R> share() noexcept { return std::shared_future<R>(); }
R get() { return state_->get(); }
bool valid() const noexcept { return state_->valid(); }
void wait() const { state_->wait(); }
template <class Rep, class Period>
std::future_status
wait_for(const std::chrono::duration<Rep, Period> &rel_time) const {
return state_->wait_for(rel_time);
}
template <class Clock, class Duration>
std::future_status
wait_until(const std::chrono::time_point<Clock, Duration> &abs_time) const {
return state_->wait_until(abs_time);
}
private:
state_type state_;
};
template <>
class future<void> {
using state_type = std::shared_ptr<detail::shared_state<int>>;
public:
future() {}
explicit future(const state_type &state) : state_(state) {}
future(future &&other) noexcept: state_(std::move(other.state_)) {
other.state_.reset();
}
future(const future &other) = delete;
~future() {}
future &operator=(future &&other) noexcept {
if (&other != this) {
state_ = std::move(other.state_);
other.state_.reset();
}
return *this;
}
future &operator=(const future &other) = delete;
void swap(future &other) noexcept {
std::swap(state_, other.state_);
}
std::shared_future<void> share() noexcept { return std::shared_future<void>(); }
void get() { state_->get(); }
bool valid() const noexcept { return state_->valid(); }
void wait() const { state_->wait(); }
template <class Rep, class Period>
std::future_status
wait_for(const std::chrono::duration<Rep, Period> &rel_time) const {
return state_->wait_for(rel_time);
}
template <class Clock, class Duration>
std::future_status
wait_until(const std::chrono::time_point<Clock, Duration> &abs_time) const {
return state_->wait_until(abs_time);
}
private:
state_type state_;
};
template <typename R>
class promise {
using state_type = std::shared_ptr<detail::shared_state<R>>;
public:
promise() : state_(new detail::shared_state<R>()) {}
promise(promise &&other) noexcept: state_(std::move(other.state_)) {
other.state_.reset();
}
promise(const promise &other) = delete;
~promise() {}
promise &operator=(promise &&other) noexcept {
if (&other != this) {
state_ = std::move(other.state_);
other.state_.reset();
}
return *this;
}
promise &operator=(const promise &other) = delete;
void swap(promise &other) noexcept {
std::swap(state_, other.state_);
}
std::future<R> get_future() { return std::future<R>(state_); }
void set_value(const R &value) { state_->set(value); }
void set_value(R &&value) { state_->set(std::move(value)); }
void set_value_at_thread_exit(const R &value);
void set_value_at_thread_exit(R &&value);
void set_exception(std::exception_ptr p);
void set_exception_at_thread_exit(std::exception_ptr p);
private:
state_type state_;
};
template <>
class promise<void> {
using state_type = std::shared_ptr<detail::shared_state<int>>;
public:
promise() : state_(new detail::shared_state<int>()) {}
promise(promise &&other) noexcept: state_(std::move(other.state_)) {
other.state_.reset();
}
promise(const promise &other) = delete;
~promise() {}
promise &operator=(promise &&other) noexcept {
if (&other != this) {
state_ = std::move(other.state_);
other.state_.reset();
}
return *this;
}
promise &operator=(const promise &other) = delete;
void swap(promise &other) noexcept {
std::swap(state_, other.state_);
}
std::future<void> get_future() { return std::future<void>(state_); }
void set_value() { state_->set(0); }
void set_value_at_thread_exit();
void set_exception(std::exception_ptr p);
void set_exception_at_thread_exit(std::exception_ptr p);
private:
state_type state_;
};
template <class R>
void swap(std::future<R> &lhs, std::future<R> &rhs) noexcept {
lhs.swap(rhs);
}
template <class R>
void swap(std::promise<R> &lhs, std::promise<R> &rhs) noexcept {
lhs.swap(rhs);
}
} // namespace std