-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.cpp
More file actions
75 lines (56 loc) · 1.8 KB
/
Copy pathcore.cpp
File metadata and controls
75 lines (56 loc) · 1.8 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
#include "core.h"
bool CoreBase::hasCallback() const noexcept {
constexpr auto allowed = State::OnlyCallback | State::Done | State::Empty;
auto const state = state_.load(std::memory_order_acquire);
return State() != (state & allowed);
}
bool CoreBase::hasResult() const noexcept {
constexpr auto allowed = State::OnlyResult | State::Done;
auto core = this;
auto state = core->state_.load(std::memory_order_acquire);
return State() != (state & allowed);
}
bool CoreBase::ready() const noexcept {
return hasResult();
}
void CoreBase::setResult_() {
assert(!hasResult());
auto state = state_.load(std::memory_order_acquire);
switch (state) {
case State::Start:
if (state_.compare_exchange_strong(state, State::OnlyResult, std::memory_order_release, std::memory_order_acquire)){
return;
}
case State::OnlyCallback:
state_.store(State::Done, std::memory_order_relaxed);
doCallback(state);
return;
case State::OnlyResult:
case State::Done:
case State::Empty:
default:
throw std::logic_error("setResult unexpected state");
}
}
void CoreBase::setCallback_(Callback&& callback) {
assert(!hasCallback());
::new (&callback_) Callback(std::move(callback));
auto state = state_.load(std::memory_order_acquire);
State nextState = State::OnlyCallback;
if (state == State::Start) {
if (state_.compare_exchange_strong(state, nextState, std::memory_order_release, std::memory_order_acquire)){
return;
}
}
if (state == State::OnlyResult) {
state_.store(State::Done, std::memory_order_relaxed);
doCallback(state);
return;
}
throw std::logic_error("setCallback unexpected state");
}
void CoreBase::doCallback(State priorState) {
assert(state_ == State::Done);
callback_(*this);
callback_.~Callback();
}