-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_stuff.cpp
More file actions
45 lines (39 loc) · 1.11 KB
/
task_stuff.cpp
File metadata and controls
45 lines (39 loc) · 1.11 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
#include "task_stuff.h"
namespace TaskStuff
{
void Promise<void>::SetDone()
{
if (_value_set_)
{
throw FutureError(FutureErrorCode::PromiseAlreadySatisfied, "Promise value already set!");
}
if (!_state_)
{
throw FutureError(FutureErrorCode::NoState, "Promise has no state!");
}
std::unique_lock lck(_state_->_mtx_value_);
_value_set_ = true;
// If a continuation function is set, call it with the value
if (_state_->_continuation_)
{
_state_->_continuation_->Call();
}
else if (_state_->_chained_promise_)
{
_state_->_chained_promise_->SetDone();
}
else // Otherwise set the value in the state normally
{
_state_->_value_.emplace();
_state_->_cv_value_.notify_all();
}
}
Future<void>& Future<void>::operator=(Future<void>&& other) noexcept
{
if (_state_)
_state_->_release();
_state_ = other._state_;
other._state_ = nullptr;
return *this;
}
}