-
-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
887fe71
commit e692910
Showing
41 changed files
with
1,104 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#include <karm-async/async.h> | ||
#include <karm-sys/proc.h> | ||
|
||
namespace Karm::Async::_Embed { | ||
|
||
struct PosixLoop : public Loop { | ||
TimeStamp _now; | ||
|
||
PosixLoop(TimeStamp now) : _now(now) {} | ||
|
||
TimeStamp now() override { | ||
return _now; | ||
} | ||
|
||
Res<> wait(TimeStamp until) override { | ||
try$(Sys::sleepUntil(until)); | ||
_now = Sys::now(); | ||
return Ok(); | ||
} | ||
}; | ||
|
||
static Opt<PosixLoop> _loop; | ||
Loop &loop() { | ||
if (not _loop) { | ||
_loop.emplace(Sys::now()); | ||
} | ||
return *_loop; | ||
} | ||
|
||
} // namespace Karm::Async::_Embed |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#include <karm-async/_embed.h> | ||
|
||
namespace Karm::Async::_Embed { | ||
|
||
} // namespace Karm::Async::_Embed |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#pragma once | ||
|
||
namespace Karm::Async { | ||
struct Loop; | ||
namespace _Embed { | ||
Loop &loop(); | ||
} // namespace _Embed | ||
} // namespace Karm::Async |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
#include <karm-logger/logger.h> | ||
|
||
#include "async.h" | ||
|
||
namespace Karm::Async { | ||
|
||
/* --- Sink --- */ | ||
|
||
void Loop::_post(Sink &sink, Box<Event> event) { | ||
_queued.emplaceBack(sink, std::move(event)); | ||
} | ||
|
||
void Loop::_move(Sink &from, Sink &to) { | ||
logInfo("Moving sink from {p} to {p}", &from, &to); | ||
for (auto &q : _queued) { | ||
if (q.sink == &from) { | ||
q.sink = &to; | ||
} | ||
} | ||
|
||
for (auto &s : _sources) { | ||
if (s.sink == &from) { | ||
s.sink = &to; | ||
} | ||
} | ||
} | ||
|
||
void Loop::_dtor(Sink &sink) { | ||
for (auto &q : _queued) { | ||
if (q.sink == &sink) | ||
q.sink = nullptr; | ||
} | ||
|
||
for (auto &s : _sources) { | ||
if (s.sink == &sink) | ||
s.sink = nullptr; | ||
} | ||
} | ||
|
||
/* --- Source --- */ | ||
|
||
void Loop::_bind(Source &source, Sink &sink) { | ||
_sources.emplaceBack(source, sink); | ||
} | ||
|
||
void Loop::_move(Source &from, Source &to) { | ||
for (auto &s : _sources) { | ||
if (s.source == &from) | ||
s.source = &to; | ||
} | ||
} | ||
|
||
void Loop::_dtor(Source *source) { | ||
for (auto &s : _sources) { | ||
if (s.source == source) | ||
s.source = nullptr; | ||
} | ||
} | ||
|
||
/* --- Public --- */ | ||
|
||
void Loop::_collect() { | ||
for (usize i = 0; i < _sources.len(); i++) { | ||
auto [source, sink] = _sources[i]; | ||
if (not source or not sink) { | ||
_sources.removeAt(i--); | ||
} | ||
} | ||
} | ||
|
||
Res<TimeStamp> Loop::poll() { | ||
TimeStamp until = TimeStamp::endOfTime(); | ||
|
||
for (usize i = 0; i < _sources.len(); i++) { | ||
auto [source, sink] = _sources[i]; | ||
if (not source or not sink) | ||
continue; | ||
|
||
auto ts = try$(source->poll(*sink)); | ||
|
||
if (Op::lt(ts, until)) | ||
until = ts; | ||
} | ||
|
||
return Ok(until); | ||
} | ||
|
||
Res<usize> Loop::dispatch() { | ||
usize count = 0; | ||
for (usize i = 0; i < _queued.len(); i++) { | ||
count++; | ||
auto *target = _queued[i].sink; | ||
auto event = std::move(_queued[i].event); | ||
|
||
if (not target) | ||
continue; | ||
|
||
auto res = target->post(*event); | ||
if (not res) { | ||
_queued.removeRange(0, i); | ||
return res.none(); | ||
} | ||
} | ||
|
||
_queued.clear(); | ||
return Ok(count); | ||
} | ||
|
||
Res<> Loop::run() { | ||
while (true) { | ||
debug(" --- Loop ---"); | ||
usize count = 0; | ||
TimeStamp until = TimeStamp::endOfTime(); | ||
|
||
do { | ||
until = try$(poll()); | ||
count = try$(dispatch()); | ||
_collect(); | ||
} while (count); | ||
|
||
if (_sources.len() == 0) | ||
return Ok(); | ||
|
||
if (_ret) | ||
return *_ret; | ||
|
||
try$(wait(until)); | ||
} | ||
} | ||
|
||
} // namespace Karm::Async |
Oops, something went wrong.