-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpromise.cpp
186 lines (164 loc) · 3.9 KB
/
promise.cpp
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
#include <stdexcept>
#include "promise.h"
namespace kaiu {
using namespace std;
/*** Monads ***/
namespace detail {
function<void()> combine_finalizers(const function<void()> f1, const function<void()> f2)
{
if (f1 == nullptr) {
return f2;
} else if (f2 == nullptr) {
return f1;
} else {
return [f1, f2] () {
try {
f1();
} catch (...) {
f2();
throw;
}
f2();
};
}
}
}
/*** PromiseStateBase ***/
#if defined(SAFE_PROMISES)
PromiseStateBase::~PromiseStateBase() noexcept(false)
{
/*
* Don't throw if stack is being unwound, it'll prevent catch blocks from
* being run and will generally ruin your debugging experience.
*/
if (std::uncaught_exception()) {
return;
}
if (callbacks_assigned && state != promise_state::completed) {
/* Bound but not completed */
throw logic_error("Promise destructor called on bound but uncompleted promise");
}
}
#endif
void PromiseStateBase::reject(exception_ptr error)
{
auto lock = get_lock();
set_error(lock, error);
set_state(lock, promise_state::rejected);
}
void PromiseStateBase::reject(const string& error)
{
reject(make_exception_ptr(runtime_error(error)));
}
void PromiseStateBase::set_callbacks(ensure_locked lock, function<void(ensure_locked)> resolve, function<void(ensure_locked)> reject)
{
#if defined(SAFE_PROMISES)
if (callbacks_assigned) {
throw logic_error("Attempted to double-bind to promise");
}
if (!resolve || !reject) {
throw logic_error("Attempted to bind null callback");
}
#endif
on_resolve = resolve;
on_reject = reject;
callbacks_assigned = true;
update_state(lock);
}
void PromiseStateBase::set_state(ensure_locked lock, const promise_state next_state)
{
#if defined(SAFE_PROMISES)
/* Validate transition */
switch (next_state) {
case promise_state::pending:
throw logic_error("Cannot explicitly mark a promise as pending");
case promise_state::rejected:
case promise_state::resolved:
if (state == promise_state::pending) {
make_immortal(lock);
break;
}
if (state == next_state) {
break;
}
throw logic_error("Cannot resolve/reject promise: it is already resolved/rejected");
case promise_state::completed:
if (state == promise_state::rejected || state == promise_state::resolved) {
break;
}
throw logic_error("Cannot mark promise as completed: promise has not been resolved/rejected");
default:
throw logic_error("Invalid state");
}
#endif
/* Apply transition */
state = next_state;
update_state(lock);
}
void PromiseStateBase::update_state(ensure_locked lock)
{
/*
* After unlock_self(), this instance may be destroyed. Hence, we must not
* do anything that accesses *this after calling unlock_self().
* Consequently, any method which calls update_state(...) or set_state()
* must not access *this after that call.
*/
switch (state) {
case promise_state::pending:
break;
case promise_state::rejected:
if (callbacks_assigned) {
auto callback = on_reject;
callback(lock);
set_state(lock, promise_state::completed);
}
break;
case promise_state::resolved:
if (callbacks_assigned) {
auto callback = on_resolve;
callback(lock);
set_state(lock, promise_state::completed);
}
break;
case promise_state::completed:
on_resolve = nullptr;
on_reject = nullptr;
make_mortal(lock);
break;
}
}
void PromiseStateBase::set_error(ensure_locked lock, exception_ptr error)
{
this->error = error;
}
exception_ptr PromiseStateBase::get_error(ensure_locked) const
{
return error;
}
void PromiseStateBase::set_terminator(ensure_locked lock)
{
set_callbacks(lock,
[] (ensure_locked) {},
[this] (ensure_locked) {
if (error) {
rethrow_exception(error);
}
});
}
void PromiseStateBase::finish()
{
auto lock = get_lock();
set_terminator(lock);
}
/*** Utils ***/
namespace promise {
/*
* Promise factory creator - for when the parameter is statically known to be
* nullptr
*/
nullptr_t factory(nullptr_t)
{
return nullptr;
}
}
}