-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcml.cpp
More file actions
316 lines (232 loc) · 6.19 KB
/
cml.cpp
File metadata and controls
316 lines (232 loc) · 6.19 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
#include <condition_variable>
#include <mutex>
#include <thread>
#include <vector>
#include "maybe.hpp"
namespace event {
// type-erasure class
template<class A>
class any {
struct base {
virtual ~base() {}
virtual maybe<A> poll() = 0;
virtual A wait() = 0;
};
template<class E>
struct derived: base {
E impl;
derived(E impl): impl(std::move(impl)) {}
maybe<A> poll() override { return impl.poll(); }
A wait() override { return impl.wait(); }
};
std::unique_ptr<base> impl;
public:
template<class E>
any(E e): impl(std::make_unique<derived<E>>(std::move(e))) {}
maybe<A> poll() { return impl->poll(); }
A wait() { return impl->wait(); }
};
// atomic queue
template<class T>
class queue {
struct list: std::unique_ptr<std::pair<T, list>> { };
list data;
std::mutex mutex;
using lock_type = std::unique_lock<std::mutex>;
public:
template<class Alt>
maybe<T> pop(Alt alt) {
const lock_type lock(mutex);
if(data) {
maybe<T> result = data->first;
data = std::move(data->second);
return result;
}
alt();
return {};
}
void push(T value) {
const lock_type lock(mutex);
data.reset(new std::pair<T, list>(std::move(value), std::move(data)));
}
};
// channel
template<class A>
struct channel {
enum {
WAITING,
CLAIMED,
SYNCHED
};
struct side {
A payload;
std::condition_variable resume;
std::atomic<std::size_t> state;
};
queue<side*> sendq, recvq;
};
template<class A>
static std::shared_ptr<channel<A>> make_channel() {
return std::make_shared<channel<A>>();
}
/*
- primitive: dequeue waiting with transition
- once it's been dequeued, nobody else can change its state
- optimistic phase:
- dequeue(W->S), then complete tx
- pessimistic:
- publish ourselves as waiting, now our state may change
- recheck
- try claim our side (W->C)
- success: now our state may no longer change
- dequeue(W->S) then:
- success:
- commit our side (C->S), unpublish ourselves
- complete tx
- else:
- rollback (C -> W)
- recheck
- else:
- S: somebody else completed us (hence dequeued us)
- complete tx
- C: somebody else is claiming us, retry (cannot happen?)
*/
template<class A>
class recv {
using channel_type = std::shared_ptr<channel<A>>;
channel_type chan;
public:
recv(recv&&) = default;
recv(channel_type chan): chan(std::move(chan)) { }
maybe<A> poll() {
// atomically try pop one waiting sender from the queue
if(auto some = chan->sendq.pop()) {
maybe<A> result = std::move(*some.get().source);
// complete the transaction
*some.get().done = true;
// resume sender
some.get().resume->notify_one();
return result;
}
return {};
}
A wait() {
side my;
my.state = WAITING;
std::mutex mutex;
// publish ourselves to the recv queue
chan->recvq.push(&my);
// recheck send queue to avoid starving
while(auto some = chan->sendq.pop()) {
// attempt to claim our side of transaction
std::size_t expected = WAITING;
if(my.state.compare_exchange_strong(expected, CLAIMED)) {
// now attempt to complete transaction
std::size_t expected = WAITING;
if(some.get()->state.compare_exchange_strong(expected, SYNCHED)) {
// success, complete transaction
my.result = std::move(some.get()->payload);
// resume other side
some.get()->resume.notify_one();
// TODO unpublish ourselves from the recvq, atomically
return my.result;
} else {
}
} else if(expected == SYNCHED) {
// some other sender completed our transaction, rollback this one and
// return
// put back other end
chand->sendq.push(some.get());
return my.result;
} else {
// some other sender is claiming our transaction, rollback
chand->sendq.push(some.get());
}
}
// wait
std::unique_lock<std::mutex> lock(mutex);
cv.wait(lock, [&] { return my.state == SYNCHED; });
return my.result;
}
};
struct unit {};
template<class A>
class send {
using channel_type = std::shared_ptr<channel<A>>;
channel_type chan;
A value;
std::mutex mutex;
std::condition_variable cv;
public:
send(send&&) = default;
send(channel_type chan, A value): chan(std::move(chan)), value(std::move(value)) { }
maybe<unit> poll() {
// atomically try pop one receiver from the queue
if(auto ready = chan->recvq.pop()) {
*ready.get().target = std::move(value);
// complete the transaction
*ready.get().done = true;
// resume receiver
ready.get().resume->notify_one();
return unit{};
}
return {};
}
unit wait() {
bool done = false;
// add ourselves to the send queue
chan->recvq.push({&value, &cv, &done});
// TODO recheck?
std::unique_lock<std::mutex> lock(mutex);
cv.wait(lock, [&] { return done; });
return {};
}
};
template<class Ev, class Func>
struct map_type {
Ev source;
const Func func;
auto poll() { return map(source.poll(), func); }
auto wait() { return func(source.wait()); }
};
template<class Ev, class Func>
static map_type<Ev, Func> map(Ev ev, Func func) {
return {std::move(ev), std::move(func)};
}
template<class A>
struct choose {
std::vector<any<A>> events;
maybe<A> poll() {
// TODO randomize
for(const auto& event: events) {
if(auto ready = event.poll()) {
return ready;
}
}
}
A wait() {
// ????
}
};
template<class Event>
static auto sync(Event&& event) {
if(auto value = event.poll()) {
return value.get();
}
return event.wait();
}
} // namespace event
#include <iostream>
int main(int, char**) {
using namespace event;
auto chan = make_channel<int>();
std::thread t1([=] {
std::cout << sync(recv<int>(chan)) << std::endl;
});
std::thread t2([=] {
sync(send<int>(chan, 14));
});
t1.join();
t2.join();
return 0;
}