2022-05-17 03:56:25 +00:00
|
|
|
#include "async.h"
|
|
|
|
|
|
|
|
#include <array>
|
|
|
|
#include <atomic>
|
|
|
|
#include <chrono>
|
2022-06-19 07:32:09 +00:00
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
#include "lock.h"
|
2023-06-03 06:33:01 +00:00
|
|
|
#include "trace.h"
|
2022-05-17 03:56:25 +00:00
|
|
|
|
|
|
|
namespace async {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
using namespace std::literals::chrono_literals;
|
|
|
|
|
|
|
|
struct Stuff {
|
|
|
|
std::coroutine_handle<> h;
|
|
|
|
std::chrono::system_clock::time_point expiration;
|
|
|
|
|
|
|
|
Stuff* next;
|
|
|
|
};
|
|
|
|
|
2022-06-19 07:32:09 +00:00
|
|
|
struct Notification {
|
|
|
|
bool pending; // can only be true if stuff is nullptr
|
|
|
|
Stuff* stuff;
|
|
|
|
};
|
|
|
|
|
2023-06-03 06:33:01 +00:00
|
|
|
std::atomic<Stuff*> work = nullptr;
|
2022-06-19 07:32:09 +00:00
|
|
|
std::array<Notification, static_cast<size_t>(AwaitableType::kNumTypes)>
|
|
|
|
notifications = {};
|
2022-05-17 03:56:25 +00:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
void schedule(std::coroutine_handle<> h, int ms) {
|
2023-06-03 06:33:01 +00:00
|
|
|
InterruptLock lock;
|
2022-06-19 07:32:09 +00:00
|
|
|
TRACE(tracing::TraceEvent::kAsyncSchedule);
|
2022-05-17 03:56:25 +00:00
|
|
|
std::chrono::system_clock::time_point exp =
|
|
|
|
std::chrono::system_clock::now() + std::chrono::milliseconds(ms);
|
|
|
|
Stuff* news = new Stuff{
|
|
|
|
.h = h,
|
|
|
|
.expiration = exp,
|
|
|
|
};
|
|
|
|
|
|
|
|
Stuff* stuff = work;
|
|
|
|
|
|
|
|
if (!stuff || stuff->expiration > exp) {
|
|
|
|
news->next = stuff;
|
|
|
|
work = news;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Stuff* s = stuff;
|
|
|
|
while (s->next && s->next->expiration <= exp) {
|
|
|
|
s = s->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
news->next = s->next;
|
|
|
|
s->next = news;
|
|
|
|
}
|
|
|
|
|
2023-06-03 06:33:01 +00:00
|
|
|
void step() {
|
|
|
|
Stuff* stuff;
|
|
|
|
// ensure all previous side effects are visible
|
|
|
|
{
|
|
|
|
InterruptLock lock;
|
|
|
|
stuff = work;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (stuff == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto now = std::chrono::system_clock::now();
|
|
|
|
auto dt = stuff->expiration - now;
|
|
|
|
|
|
|
|
if (dt > 0ms) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int stuffinqueue = 0;
|
|
|
|
for (Stuff* s = stuff; s; s = s->next) stuffinqueue++;
|
|
|
|
|
|
|
|
TRACE(tracing::TraceEvent::kAsyncTask);
|
|
|
|
stuff->h();
|
|
|
|
TRACE(tracing::TraceEvent::kAsyncTaskDone);
|
|
|
|
|
|
|
|
if (stuff->h.done()) {
|
|
|
|
stuff->h.destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
InterruptLock lock;
|
|
|
|
work = stuff->next;
|
|
|
|
}
|
|
|
|
delete stuff;
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset() {
|
|
|
|
Stuff* stuff = work;
|
|
|
|
while (stuff) {
|
|
|
|
Stuff* byebye = stuff;
|
|
|
|
stuff = stuff->next;
|
|
|
|
|
|
|
|
delete byebye;
|
|
|
|
}
|
|
|
|
work = nullptr;
|
|
|
|
}
|
|
|
|
|
2022-06-19 07:32:09 +00:00
|
|
|
void main_loop(bool (*idle_function)()) {
|
2022-05-17 03:56:25 +00:00
|
|
|
while (1) {
|
|
|
|
if (idle_function != nullptr) {
|
2022-06-19 07:32:09 +00:00
|
|
|
if (idle_function()) {
|
2023-06-03 06:33:01 +00:00
|
|
|
reset();
|
2022-06-19 07:32:09 +00:00
|
|
|
break;
|
|
|
|
};
|
2022-05-17 03:56:25 +00:00
|
|
|
}
|
|
|
|
|
2023-06-03 06:33:01 +00:00
|
|
|
step();
|
2022-05-17 03:56:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void enqueue(std::coroutine_handle<> h, AwaitableType type) {
|
|
|
|
auto ttype = static_cast<size_t>(type);
|
2022-06-19 07:32:09 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
InterruptLock lock;
|
|
|
|
TRACE(tracing::TraceEvent::kAsyncEnqueue);
|
|
|
|
|
|
|
|
const bool was_notified =
|
|
|
|
std::exchange(notifications[ttype].pending, false);
|
|
|
|
if (was_notified) {
|
|
|
|
TRACE(tracing::TraceEvent::kAsyncAwaitWasNotified);
|
|
|
|
schedule(h);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Stuff* item = new Stuff{.h = h};
|
|
|
|
Stuff* stuff = notifications[ttype].stuff;
|
|
|
|
if (stuff == nullptr) {
|
|
|
|
notifications[ttype].stuff = item;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
while (stuff->next != nullptr) {
|
|
|
|
stuff = stuff->next;
|
|
|
|
}
|
|
|
|
stuff->next = item;
|
2022-05-17 03:56:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void resume(AwaitableType type) {
|
|
|
|
auto ttype = static_cast<size_t>(type);
|
2022-06-19 07:32:09 +00:00
|
|
|
Stuff* stuff = nullptr;
|
|
|
|
{
|
|
|
|
InterruptLock lock;
|
2022-05-17 03:56:25 +00:00
|
|
|
|
2022-06-19 07:32:09 +00:00
|
|
|
stuff = notifications[ttype].stuff;
|
|
|
|
if (stuff == nullptr) {
|
|
|
|
notifications[ttype].pending = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
notifications[ttype].stuff = stuff->next;
|
|
|
|
schedule(stuff->h);
|
|
|
|
}
|
2022-05-17 03:56:25 +00:00
|
|
|
delete stuff;
|
|
|
|
}
|
|
|
|
|
2023-06-07 07:59:54 +00:00
|
|
|
} // namespace async
|