-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmagnesium.hpp
More file actions
459 lines (356 loc) · 11.5 KB
/
magnesium.hpp
File metadata and controls
459 lines (356 loc) · 11.5 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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/**
* @file magnesium.hpp
* @brief Interrupt-based preemptive multitasking.
* License: Public domain. The code is provided as is without any warranty.
*/
#ifndef MAGNESIUM_HPP
#define MAGNESIUM_HPP
#include <optional>
#include <array>
#include <coroutine>
#include "mg_port.h"
namespace magnesium {
template<class T> using option = std::optional<T>;
template<class T> class owner {
T* ptr;
void drop(T*);
public:
owner(T* pointer) noexcept : ptr(pointer) {}
owner(owner&& other) noexcept : ptr(other.ptr) {
other.ptr = nullptr;
}
~owner() {
if (ptr != nullptr) {
drop(ptr);
}
}
inline T* release() {
T* const temp = ptr;
ptr = nullptr;
return temp;
}
T* operator->() const {
return ptr; // TODO: assert ptr != nullptr
}
owner(std::nullptr_t) = delete;
owner(const owner&) = delete;
owner& operator=(const owner& other) = delete;
owner& operator=(owner&& other) = delete;
};
template <class T> class list;
template <class T = void> class node {
node<T>* next;
node<T>* prev;
protected:
~node() = default; // can't be destructed through 'delete node'
public:
friend class list<T>;
friend class message;
friend class actor;
node() = default; // the container and its items are non-copyable/movable.
node(const node&) = delete;
node& operator=(const node& other) = delete;
node& operator=(node&& other) = delete;
};
template <typename U = void> class list : public node<U> {
inline bool is_empty() const {
return (this->next == this);
}
public:
list() noexcept {
this->next = this->prev = this;
}
template<class T> inline void enqueue(owner<T>& object) {
node<U>* const link = static_cast<node<U>*>(object.release());
link->next = this;
link->prev = this->prev;
link->prev->next = link;
this->prev = link;
}
template<class T> inline option<owner<T>> dequeue() {
option<owner<T>> object = {};
if (!is_empty()) {
node<U>* const link = this->next;
link->prev->next = link->next;
link->next->prev = link->prev;
link->next = link->prev = nullptr;
object.emplace(owner(static_cast<T*>(link)));
}
return object;
}
};
class mutex {};
class locked_region {
mutex& lock;
public:
locked_region(mutex& object) noexcept : lock(object) {
mg_object_lock(&lock);
}
~locked_region() {
mg_object_unlock(&lock);
}
};
class queue_base {};
struct message : public node<> {
queue_base* parent;
};
struct future {
struct promise_type {
static void* allocate(std::size_t n);
future get_return_object() { return {}; }
std::suspend_never initial_suspend() { return {}; }
std::suspend_never final_suspend() noexcept { return {}; }
void unhandled_exception() {}
void* operator new(std::size_t n) { return allocate(n); }
};
};
template<class T> class queue;
template<class T> class message_pool;
class actor : public node<> {
message* mailbox = nullptr;
unsigned int timeout = 0;
std::coroutine_handle<> frame;
protected:
~actor() = default;
public:
const unsigned int vect;
const unsigned int prio;
virtual future run() = 0;
actor(unsigned int vect) noexcept :
vect(vect),
prio(pic_vect2prio(vect)) {}
template<class T> inline void set_message(owner<T>& msg) {
mailbox = msg.release();
}
template<class T> inline owner<T> take_message() {
message* temp = mailbox;
mailbox = nullptr;
return owner<T>(static_cast<T*>(temp));
}
inline void set_handle(std::coroutine_handle<> handle) {
frame = handle;
}
void call() {
frame();
}
template<class T> inline auto poll(queue<T>& q);
template<class T> inline auto get(message_pool<T>& q);
inline auto sleep(unsigned int delay);
friend class timer;
};
class scheduler {
mutex lock;
std::array<list<>, MG_PRIO_MAX> runqueue;
static scheduler context;
static option<owner<actor>> extract(list<>& runq) {
locked_region region(context.lock);
return runq.dequeue<actor>();
}
public:
static void activate(owner<actor>& target) {
locked_region region(context.lock);
pic_interrupt_request(target->vect);
context.runqueue[target->prio].enqueue(target);
}
static void schedule(unsigned int vect) {
const unsigned int prio = pic_vect2prio(vect);
while (option<owner<actor>> item = extract(context.runqueue[prio])) {
actor* active_actor = (*item).release();
active_actor->call();
}
}
};
template<class T> class queue : public queue_base {
list<> items;
int length = 0;
option<owner<actor>> push_internal(owner<T>& msg) {
locked_region region(lock);
const int queue_length = length++;
if (queue_length >= 0) {
items.enqueue(msg);
} else {
option<owner<actor>> item = items.dequeue<actor>();
owner<actor>& subscriber = *item;
subscriber->set_message(msg);
return item;
}
return std::nullopt;
}
option<owner<T>> pop_internal(actor& subscriber, std::coroutine_handle<> h) {
locked_region region(lock);
const int queue_length = length--;
if (queue_length <= 0) {
subscriber.set_handle(h);
auto subscr_owner = owner(&subscriber);
items.enqueue(subscr_owner);
} else {
return items.dequeue<T>();
}
return std::nullopt;
}
protected:
mutex lock;
option<owner<T>> try_pop() {
locked_region region(lock);
if (length > 0) {
--length;
return items.dequeue<T>();
}
return std::nullopt;
}
auto pop(actor& subscriber) {
struct awaitable {
actor& subscriber;
queue<T>& source;
awaitable(queue<T>& q, actor& a) noexcept :
subscriber(a),
source(q) {}
bool await_ready() const noexcept {
option<owner<T>> msg = source.try_pop();
if (msg) {
subscriber.set_message(*msg);
}
return (bool)msg;
}
bool await_suspend(std::coroutine_handle<> h) const noexcept {
option<owner<T>> msg = source.pop_internal(subscriber, h);
if (msg) {
subscriber.set_message(*msg);
}
return !(bool)msg;
}
owner<T> await_resume() const noexcept {
return subscriber.take_message<T>();
}
};
return awaitable(*this, subscriber);
}
public:
inline void push(owner<T>& msg) {
option<owner<actor>> subscriber = push_internal(msg);
if (subscriber) {
scheduler::activate(*subscriber);
}
}
friend class actor;
};
template<class T> class message_pool : public queue<T> {
T* const items_array;
const std::size_t array_length;
std::size_t offset;
inline option<owner<T>> try_pick_from_array() {
locked_region region(this->lock);
if (offset < array_length) {
T& item = items_array[offset++];
item.parent = this;
return owner(&item);
}
return std::nullopt;
}
protected:
auto get(actor& subscriber) {
option<owner<T>> msg = try_pick_from_array();
if (msg) {
this->push(*msg);
}
return this->pop(subscriber);
}
public:
template<unsigned int N> message_pool(T (&arr)[N]) noexcept :
items_array(&arr[0]),
array_length(sizeof(arr) / sizeof(arr[0])),
offset(0) {}
option<owner<T>> alloc() {
auto msg = try_pick_from_array();
if (!msg) {
return this->try_pop();
}
return msg;
}
friend class actor;
};
class timer {
mutex lock;
std::array<list<>, MG_TIMERQ_MAX> subscribers;
std::array<size_t, MG_TIMERQ_MAX> length;
unsigned int ticks;
static timer context;
static unsigned int diff_msb(unsigned int a, unsigned int b) {
const unsigned int width = sizeof(unsigned int) * 8;
const unsigned int i = width - mg_port_clz(a ^ b) - 1;
return i < MG_TIMERQ_MAX ? i : MG_TIMERQ_MAX - 1;
}
public:
static void subscribe(actor& subscriber, unsigned int delay) {
//TODO: assert(delay < INT32_MAX);
locked_region region(context.lock);
const unsigned int timeout = context.ticks + delay;
const unsigned q = diff_msb(context.ticks, timeout);
subscriber.timeout = timeout;
auto subscr_owner = owner(&subscriber);
context.subscribers[q].enqueue(subscr_owner);
++(context.length[q]);
}
static auto sleep(actor& subscriber, unsigned int delay) {
struct awaitable {
actor& subscriber;
const unsigned int delay;
awaitable(actor& a, unsigned int d) noexcept :
subscriber(a),
delay(d) {}
bool await_ready() const noexcept {
return false;
}
void await_suspend(std::coroutine_handle<> h) const noexcept {
subscriber.set_handle(h);
if (delay != 0) {
timer::subscribe(subscriber, delay);
} else {
auto subscr_owner = owner(&subscriber);
scheduler::activate(subscr_owner);
}
}
void await_resume() const noexcept {}
};
return awaitable(subscriber, delay);
}
static void tick() {
locked_region region(context.lock);
const auto prev_tick = context.ticks++;
const unsigned q = diff_msb(prev_tick, context.ticks);
const unsigned int len = context.length[q];
context.length[q] = 0;
for (unsigned int i = 0; i < len; ++i) {
option<owner<actor>> item = context.subscribers[q].dequeue<actor>();
const unsigned int timeout = (*item)->timeout;
if (timeout == context.ticks) {
scheduler::activate(*item);
} else {
const unsigned next = diff_msb(timeout, context.ticks);;
context.subscribers[next].enqueue(*item);
++(context.length[next]);
}
// TODO: interrupt window.
}
}
};
template<class T> inline auto actor::poll(queue<T>& q) {
return q.pop(*this);
}
template<class T> inline auto actor::get(message_pool<T>& p) {
return p.get(*this);
}
inline auto actor::sleep(unsigned int delay) {
return timer::sleep(*this, delay);
}
template<class T> void owner<T>::drop(T* ptr) {
message* m = static_cast<message*>(ptr);
queue<T>* q = static_cast<queue<T>*>(m->parent);
owner<T> msg = owner(ptr);
q->push(msg);
}
template<> void owner<actor>::drop(actor* a) {
//TODO: assert actor never dropped
}
};
#endif