|
| 1 | +#include <karm-logger/logger.h> |
| 2 | + |
| 3 | +#include "async.h" |
| 4 | + |
| 5 | +namespace Karm::Async { |
| 6 | + |
| 7 | +/* --- Sink --- */ |
| 8 | + |
| 9 | +void Loop::_post(Sink &sink, Box<Event> event) { |
| 10 | + _queued.emplaceBack(sink, std::move(event)); |
| 11 | +} |
| 12 | + |
| 13 | +void Loop::_move(Sink &from, Sink &to) { |
| 14 | + logInfo("Moving sink from {p} to {p}", &from, &to); |
| 15 | + for (auto &q : _queued) { |
| 16 | + if (q.sink == &from) { |
| 17 | + q.sink = &to; |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + for (auto &s : _sources) { |
| 22 | + if (s.sink == &from) { |
| 23 | + s.sink = &to; |
| 24 | + } |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +void Loop::_dtor(Sink &sink) { |
| 29 | + for (auto &q : _queued) { |
| 30 | + if (q.sink == &sink) |
| 31 | + q.sink = nullptr; |
| 32 | + } |
| 33 | + |
| 34 | + for (auto &s : _sources) { |
| 35 | + if (s.sink == &sink) |
| 36 | + s.sink = nullptr; |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +/* --- Source --- */ |
| 41 | + |
| 42 | +void Loop::_bind(Source &source, Sink &sink) { |
| 43 | + _sources.emplaceBack(source, sink); |
| 44 | +} |
| 45 | + |
| 46 | +void Loop::_move(Source &from, Source &to) { |
| 47 | + for (auto &s : _sources) { |
| 48 | + if (s.source == &from) |
| 49 | + s.source = &to; |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +void Loop::_dtor(Source *source) { |
| 54 | + for (auto &s : _sources) { |
| 55 | + if (s.source == source) |
| 56 | + s.source = nullptr; |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +/* --- Public --- */ |
| 61 | + |
| 62 | +void Loop::_collect() { |
| 63 | + for (usize i = 0; i < _sources.len(); i++) { |
| 64 | + auto [source, sink] = _sources[i]; |
| 65 | + if (not source or not sink) { |
| 66 | + _sources.removeAt(i--); |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +Res<TimeStamp> Loop::poll() { |
| 72 | + TimeStamp until = TimeStamp::endOfTime(); |
| 73 | + |
| 74 | + for (usize i = 0; i < _sources.len(); i++) { |
| 75 | + auto [source, sink] = _sources[i]; |
| 76 | + if (not source or not sink) |
| 77 | + continue; |
| 78 | + |
| 79 | + auto ts = try$(source->poll(*sink)); |
| 80 | + |
| 81 | + if (Op::lt(ts, until)) |
| 82 | + until = ts; |
| 83 | + } |
| 84 | + |
| 85 | + return Ok(until); |
| 86 | +} |
| 87 | + |
| 88 | +Res<usize> Loop::dispatch() { |
| 89 | + usize count = 0; |
| 90 | + for (usize i = 0; i < _queued.len(); i++) { |
| 91 | + count++; |
| 92 | + auto *target = _queued[i].sink; |
| 93 | + auto event = std::move(_queued[i].event); |
| 94 | + |
| 95 | + if (not target) |
| 96 | + continue; |
| 97 | + |
| 98 | + auto res = target->post(*event); |
| 99 | + if (not res) { |
| 100 | + _queued.removeRange(0, i); |
| 101 | + return res.none(); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + _queued.clear(); |
| 106 | + return Ok(count); |
| 107 | +} |
| 108 | + |
| 109 | +Res<> Loop::run() { |
| 110 | + while (true) { |
| 111 | + debug(" --- Loop ---"); |
| 112 | + usize count = 0; |
| 113 | + TimeStamp until = TimeStamp::endOfTime(); |
| 114 | + |
| 115 | + do { |
| 116 | + until = try$(poll()); |
| 117 | + count = try$(dispatch()); |
| 118 | + _collect(); |
| 119 | + } while (count); |
| 120 | + |
| 121 | + if (_sources.len() == 0) |
| 122 | + return Ok(); |
| 123 | + |
| 124 | + if (_ret) |
| 125 | + return *_ret; |
| 126 | + |
| 127 | + try$(wait(until)); |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +} // namespace Karm::Async |
0 commit comments