-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpromise_stream.cpp
195 lines (177 loc) · 4.71 KB
/
promise_stream.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
187
188
189
190
191
192
193
194
195
#include "promise_stream.h"
namespace kaiu {
using namespace std;
#if defined(SAFE_PROMISE_STREAMS)
PromiseStreamStateBase::~PromiseStreamStateBase() noexcept(false)
{
if (data_callback_assigned) {
if (state != stream_state::completed) {
throw logic_error("Promise stream destructor called on bound but uncompleted promise stream");
}
} else {
/* Not completed and not end of chain (should be a "warning" really) */
if (state != stream_state::pending && state != stream_state::completed) {
throw logic_error("Unterminted promise chain (forgot ->discard?)");
}
}
}
#endif
void PromiseStreamStateBase::set_state(ensure_locked lock, stream_state next_state)
{
#if defined(SAFE_PROMISE_STREAMS)
/* Validate transition */
switch (next_state) {
case stream_state::pending:
throw logic_error("Invalid state transition");
case stream_state::streaming1:
if (state == stream_state::pending) {
break;
}
throw logic_error("Invalid state transition");
case stream_state::streaming2:
if (state == stream_state::streaming1) {
break;
}
throw logic_error("Invalid state transition");
case stream_state::streaming3:
if (state == stream_state::streaming2) {
break;
}
throw logic_error("Invalid state transition");
case stream_state::completed:
if (state == stream_state::pending || state == stream_state::streaming3) {
break;
}
throw logic_error("Invalid state transition");
default:
throw logic_error("Invalid state");
}
#endif
/* Apply transition */
state = next_state;
update_state(lock);
}
void PromiseStreamStateBase::update_state(ensure_locked lock)
{
switch (state) {
case stream_state::pending:
if (stream_has_been_written_to) {
set_state(lock, stream_state::streaming1);
} else if (result != stream_result::pending) {
set_state(lock, stream_state::completed);
}
break;
case stream_state::streaming1:
if (result != stream_result::pending) {
set_state(lock, stream_state::streaming2);
}
break;
case stream_state::streaming2:
if (buffer_is_empty) {
set_state(lock, stream_state::streaming3);
}
break;
case stream_state::streaming3:
if (!consumer_is_running) {
set_state(lock, stream_state::completed);
}
break;
case stream_state::completed:
if (result != stream_result::pending) {
decltype(completer) callback{nullptr};
swap(callback, completer);
callback(lock);
make_mortal(lock);
}
break;
}
}
auto PromiseStreamStateBase::get_state(ensure_locked) const -> stream_state
{
return state;
}
void PromiseStreamStateBase::set_stream_has_been_written_to(ensure_locked lock)
{
#if defined(SAFE_PROMISE_STREAMS)
if (result == stream_result::resolved || result == stream_result::rejected) {
throw logic_error("Data written to stream after it has been completed");
}
#endif
stream_has_been_written_to = true;
buffer_is_empty = false;
update_state(lock);
}
void PromiseStreamStateBase::set_stream_result(ensure_locked lock, stream_result result_, function<void(ensure_locked)> completer_)
{
if (result == stream_result::consumer_failed) {
/*
* If consumer throws, that rejection overrides any result set by the
* producer
*/
return;
} else if (result != stream_result::pending) {
/*
* Throw on multiple resolutions by producer
*/
throw logic_error("Attempted to resolve promise stream multiple times");
}
completer = completer_;
result = result_;
update_state(lock);
}
void PromiseStreamStateBase::set_buffer_is_empty(ensure_locked lock)
{
if (buffer_is_empty) {
return;
}
/* Unset by set_stream_has_been_written_to */
buffer_is_empty = true;
update_state(lock);
}
void PromiseStreamStateBase::set_consumer_is_running(ensure_locked lock,
bool value)
{
#if defined(SAFE_PROMISE_STREAMS)
if (consumer_is_running == value) {
throw logic_error("set_consumer_is_running: concurrent consumers detected");
}
#endif
consumer_is_running = value;
update_state(lock);
}
bool PromiseStreamStateBase::get_consumer_is_running(ensure_locked) const
{
return consumer_is_running;
}
void PromiseStreamStateBase::set_action(ensure_locked, StreamAction action)
{
this->action = action;
}
StreamAction PromiseStreamStateBase::get_action(ensure_locked) const
{
return action;
}
void PromiseStreamStateBase::set_data_callback_assigned(ensure_locked lock)
{
#if defined(SAFE_PROMISE_STREAMS)
if (data_callback_assigned) {
throw logic_error("Data callback bound multiple times");
}
#endif
data_callback_assigned = true;
if (get_state(lock) != stream_state::completed) {
make_immortal(lock);
}
}
/* Public functions */
bool PromiseStreamStateBase::is_stopping() const
{
auto lock = get_lock();
return get_action(lock) == StreamAction::Stop;
}
StreamAction PromiseStreamStateBase::data_action() const
{
auto lock = get_lock();
return get_action(lock);
}
}