-
-
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
691be45
commit 1dd0348
Showing
15 changed files
with
505 additions
and
144 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#pragma once | ||
|
||
#include "loop.h" | ||
|
||
namespace Karm::Async::_Embed { | ||
|
||
EventLoop &eventLoop(); | ||
|
||
} // 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 | ||
|
||
#include "_embed.h" | ||
|
||
#include "defer.h" | ||
#include "funcs.h" | ||
#include "loop.h" | ||
#include "timer.h" |
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,58 @@ | ||
#pragma once | ||
|
||
#include <karm-base/func.h> | ||
|
||
#include "async.h" | ||
#include "funcs.h" | ||
|
||
namespace Karm::Async { | ||
|
||
struct Defer : public Sink { | ||
Func<Res<>()> _func; | ||
|
||
struct Event : public BaseEvent<Event> { | ||
Strong<Sink> sink; | ||
Event(Strong<Sink> sink) : sink{std::move(sink)} {} | ||
}; | ||
|
||
Res<> handle(Async::Event &) override { | ||
return _func(); | ||
} | ||
}; | ||
|
||
static inline void defer(Strong<Sink> sink) { | ||
post<Defer::Event>(*sink, sink); | ||
} | ||
|
||
static inline Strong<Defer> defer(Func<Res<>()> func) { | ||
auto sink = makeStrong<Defer>(func); | ||
defer(sink); | ||
return sink; | ||
} | ||
|
||
static inline auto defer() { | ||
struct Sink : public Async::Sink { | ||
Coro<> _coro; | ||
|
||
Res<> handle(Async::Event &) override { | ||
_coro.resume(); | ||
return Ok(); | ||
} | ||
}; | ||
|
||
struct Await { | ||
TimeStamp _stamp; | ||
|
||
constexpr bool await_ready() const { return false; } | ||
|
||
void await_suspend(Coro<> coro) { | ||
defer(makeStrong<Sink>(coro)); | ||
} | ||
|
||
void await_resume() const {} | ||
}; | ||
|
||
return Await{}; | ||
} | ||
|
||
} // 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,56 @@ | ||
#pragma once | ||
|
||
#include <karm-meta/id.h> | ||
|
||
namespace Karm::Async { | ||
|
||
struct Event { | ||
bool _accepted{false}; | ||
|
||
virtual ~Event() = default; | ||
|
||
virtual Meta::Type<> type() const = 0; | ||
|
||
void accept() { | ||
_accepted = true; | ||
} | ||
|
||
template <typename T> | ||
T &unwrap() { | ||
return static_cast<T &>(*this); | ||
} | ||
|
||
template <typename T> | ||
T const &unwrap() const { | ||
return static_cast<T const &>(*this); | ||
} | ||
|
||
template <typename T> | ||
T const *is() const { | ||
return type() == Meta::typeOf<T>() ? &unwrap<T>() : nullptr; | ||
} | ||
|
||
template <typename T> | ||
T *is() { | ||
return type() == Meta::typeOf<T>() ? &unwrap<T>() : nullptr; | ||
} | ||
|
||
template <typename T> | ||
Event &handle(auto callback) { | ||
if (auto *e = is<T>()) { | ||
_accepted = _accepted or callback(*e); | ||
} | ||
return *this; | ||
} | ||
}; | ||
|
||
template <typename Crtp> | ||
struct BaseEvent : public Event { | ||
Meta::Type<Crtp> _type = Meta::typeOf<Crtp>(); | ||
|
||
Meta::Type<> type() const override { | ||
return _type; | ||
} | ||
}; | ||
|
||
} // 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,26 @@ | ||
#pragma once | ||
|
||
#include "_embed.h" | ||
|
||
namespace Karm::Async { | ||
|
||
inline Res<> run() { | ||
return _Embed::eventLoop().run(); | ||
} | ||
|
||
inline void quit(Res<> res = Ok()) { | ||
_Embed::eventLoop().quit(std::move(res)); | ||
} | ||
|
||
inline TimeStamp now() { | ||
return _Embed::eventLoop().now(); | ||
} | ||
|
||
template <typename T, typename... Args> | ||
inline void post(Sink &sink, Args &&...args) { | ||
_Embed::eventLoop() | ||
.queue() | ||
.post(sink, makeBox<T>(std::forward<Args>(args)...)); | ||
} | ||
|
||
} // namespace Karm::Async |
Oops, something went wrong.