arm: async changes
- more tracing - pending resume() notifications - idle function returns bool (useful for testing) may not build (more stuff coming in later commits)
This commit is contained in:
parent
77bc82d294
commit
15f2657254
96
arm/async.cc
96
arm/async.cc
@ -3,6 +3,23 @@
|
||||
#include <array>
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <utility>
|
||||
|
||||
#include "trace.h"
|
||||
|
||||
#ifdef __x86_64__
|
||||
#include <mutex>
|
||||
struct InterruptLock {
|
||||
static std::mutex m;
|
||||
InterruptLock() { m.lock(); }
|
||||
~InterruptLock() { m.unlock(); }
|
||||
};
|
||||
std::mutex InterruptLock::m;
|
||||
#else // __x86_64__
|
||||
#include "lock.h"
|
||||
|
||||
using tracing::trace;
|
||||
#endif // __x86_64__
|
||||
|
||||
namespace async {
|
||||
namespace {
|
||||
@ -16,13 +33,19 @@ struct Stuff {
|
||||
Stuff* next;
|
||||
};
|
||||
|
||||
struct Notification {
|
||||
bool pending; // can only be true if stuff is nullptr
|
||||
Stuff* stuff;
|
||||
};
|
||||
|
||||
std::atomic<Stuff*> work;
|
||||
std::array<std::atomic<Stuff*>, static_cast<size_t>(AwaitableType::kNumTypes)>
|
||||
queues;
|
||||
std::array<Notification, static_cast<size_t>(AwaitableType::kNumTypes)>
|
||||
notifications = {};
|
||||
|
||||
} // namespace
|
||||
|
||||
void schedule(std::coroutine_handle<> h, int ms) {
|
||||
TRACE(tracing::TraceEvent::kAsyncSchedule);
|
||||
std::chrono::system_clock::time_point exp =
|
||||
std::chrono::system_clock::now() + std::chrono::milliseconds(ms);
|
||||
Stuff* news = new Stuff{
|
||||
@ -47,10 +70,12 @@ void schedule(std::coroutine_handle<> h, int ms) {
|
||||
s->next = news;
|
||||
}
|
||||
|
||||
void main_loop(void (*idle_function)()) {
|
||||
void main_loop(bool (*idle_function)()) {
|
||||
while (1) {
|
||||
if (idle_function != nullptr) {
|
||||
idle_function();
|
||||
if (idle_function()) {
|
||||
break;
|
||||
};
|
||||
}
|
||||
Stuff* stuff = work;
|
||||
if (stuff == nullptr) {
|
||||
@ -64,38 +89,65 @@ void main_loop(void (*idle_function)()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
TRACE(tracing::TraceEvent::kAsyncTask);
|
||||
stuff->h();
|
||||
TRACE(tracing::TraceEvent::kAsyncTaskDone);
|
||||
|
||||
Stuff* oldstuff = stuff;
|
||||
work = stuff->next;
|
||||
if (stuff->h.done()) {
|
||||
stuff->h.destroy();
|
||||
}
|
||||
{
|
||||
InterruptLock lock;
|
||||
|
||||
delete oldstuff;
|
||||
work = stuff->next;
|
||||
}
|
||||
delete stuff;
|
||||
}
|
||||
}
|
||||
|
||||
void enqueue(std::coroutine_handle<> h, AwaitableType type) {
|
||||
Stuff* item = new Stuff{.h = h};
|
||||
auto ttype = static_cast<size_t>(type);
|
||||
Stuff* stuff = queues[ttype];
|
||||
if (stuff == nullptr) {
|
||||
queues[ttype] = item;
|
||||
return;
|
||||
|
||||
{
|
||||
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;
|
||||
}
|
||||
while (stuff->next != nullptr) {
|
||||
stuff = stuff->next;
|
||||
}
|
||||
stuff->next = item;
|
||||
}
|
||||
|
||||
void resume(AwaitableType type) {
|
||||
auto ttype = static_cast<size_t>(type);
|
||||
Stuff* stuff = queues[ttype];
|
||||
if (stuff == nullptr) {
|
||||
return;
|
||||
}
|
||||
Stuff* stuff = nullptr;
|
||||
{
|
||||
InterruptLock lock;
|
||||
|
||||
queues[ttype] = stuff->next;
|
||||
schedule(stuff->h);
|
||||
stuff = notifications[ttype].stuff;
|
||||
if (stuff == nullptr) {
|
||||
notifications[ttype].pending = true;
|
||||
return;
|
||||
}
|
||||
|
||||
notifications[ttype].stuff = stuff->next;
|
||||
schedule(stuff->h);
|
||||
}
|
||||
delete stuff;
|
||||
}
|
||||
|
||||
|
125
arm/async.h
125
arm/async.h
@ -1,18 +1,29 @@
|
||||
#pragma once
|
||||
|
||||
#include <chrono>
|
||||
#include <coroutine>
|
||||
#include <utility>
|
||||
|
||||
#include "trace.h"
|
||||
|
||||
#ifdef __clang__
|
||||
#include <experimental/coroutine>
|
||||
namespace std {
|
||||
using namespace experimental;
|
||||
}
|
||||
#else // __clang__
|
||||
#include <coroutine>
|
||||
#endif // __clang__
|
||||
|
||||
namespace async {
|
||||
|
||||
struct task_final_suspend {
|
||||
bool await_ready() noexcept(true) { return false; }
|
||||
void await_suspend(std::coroutine_handle<> h) noexcept(true) {
|
||||
if (parent) {
|
||||
TRACE(tracing::TraceEvent::kAsyncCallParent);
|
||||
parent();
|
||||
TRACE(tracing::TraceEvent::kAsyncCallParentDone);
|
||||
}
|
||||
h.destroy();
|
||||
}
|
||||
void await_resume() noexcept(true) {}
|
||||
|
||||
@ -20,35 +31,7 @@ struct task_final_suspend {
|
||||
};
|
||||
|
||||
template <typename T = void>
|
||||
struct task {
|
||||
struct promise_type;
|
||||
using handle_type = std::coroutine_handle<promise_type>;
|
||||
|
||||
struct promise_type {
|
||||
task get_return_object() {
|
||||
return {.h = handle_type::from_promise(*this)};
|
||||
}
|
||||
std::suspend_always initial_suspend() noexcept { return {}; }
|
||||
task_final_suspend final_suspend() noexcept {
|
||||
return {.parent = parent};
|
||||
}
|
||||
void return_value(T&& value) { ret_value = std::move(value); }
|
||||
void unhandled_exception() {}
|
||||
|
||||
T ret_value;
|
||||
std::coroutine_handle<> parent;
|
||||
};
|
||||
|
||||
// awaitable
|
||||
bool await_ready() { return h.done(); }
|
||||
void await_suspend(std::coroutine_handle<> ha) {
|
||||
h();
|
||||
h.promise().parent = ha;
|
||||
}
|
||||
T await_resume() { return std::move(h.promise().ret_value); }
|
||||
|
||||
std::coroutine_handle<promise_type> h;
|
||||
};
|
||||
struct task;
|
||||
|
||||
template <>
|
||||
struct task<void> {
|
||||
@ -64,22 +47,94 @@ struct task<void> {
|
||||
return {.parent = parent};
|
||||
}
|
||||
void return_void() {}
|
||||
void unhandled_exception() {}
|
||||
void unhandled_exception() {
|
||||
TRACE(tracing::TraceEvent::kAsyncException);
|
||||
}
|
||||
|
||||
std::coroutine_handle<> parent;
|
||||
};
|
||||
|
||||
// awaitable
|
||||
bool await_ready() { return h.done(); }
|
||||
void await_suspend(std::coroutine_handle<> ha) {
|
||||
h.promise().parent = ha;
|
||||
bool await_ready() {
|
||||
TRACE(tracing::TraceEvent::kAsyncCoAwait);
|
||||
h();
|
||||
if (h.done()) {
|
||||
TRACE(tracing::TraceEvent::kAsyncDestroy); h.destroy();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
void await_suspend(std::coroutine_handle<> ha) {
|
||||
TRACE(tracing::TraceEvent::kAsyncSuspend);
|
||||
h.promise().parent = ha;
|
||||
}
|
||||
void await_resume() {}
|
||||
|
||||
std::coroutine_handle<promise_type> h;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct task {
|
||||
struct promise_type;
|
||||
using handle_type = std::coroutine_handle<promise_type>;
|
||||
|
||||
struct promise_type {
|
||||
task get_return_object() {
|
||||
return {.h = handle_type::from_promise(*this)};
|
||||
}
|
||||
std::suspend_always initial_suspend() noexcept { return {}; }
|
||||
task_final_suspend final_suspend() noexcept {
|
||||
return {.parent = parent};
|
||||
}
|
||||
void return_value(T&& value) { ret_value = std::move(value); }
|
||||
void unhandled_exception() {
|
||||
TRACE(tracing::TraceEvent::kAsyncException);
|
||||
}
|
||||
template<std::convertible_to<T> From>
|
||||
task_final_suspend yield_value(From&& value) {
|
||||
ret_value = std::forward<From>(value);
|
||||
result_ready = true;
|
||||
return {.parent = parent};
|
||||
}
|
||||
|
||||
T ret_value;
|
||||
bool result_ready = false;
|
||||
std::coroutine_handle<> parent;
|
||||
};
|
||||
|
||||
// awaitable
|
||||
bool await_ready() {
|
||||
h.promise().parent = {};
|
||||
TRACE(tracing::TraceEvent::kAsyncCoAwait);
|
||||
h();
|
||||
if (h.promise().result_ready) {
|
||||
return true;
|
||||
}
|
||||
if (h.done()) {
|
||||
destroyed = true;
|
||||
ret_value = std::move(h.promise().ret_value);
|
||||
TRACE(tracing::TraceEvent::kAsyncDestroy); h.destroy();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
void await_suspend(std::coroutine_handle<> ha) {
|
||||
TRACE(tracing::TraceEvent::kAsyncSuspend);
|
||||
h.promise().parent = ha;
|
||||
}
|
||||
T await_resume() {
|
||||
if (!destroyed) {
|
||||
h.promise().result_ready = false;
|
||||
return std::move(h.promise().ret_value);
|
||||
}
|
||||
return std::move(ret_value);
|
||||
}
|
||||
|
||||
bool destroyed = false;
|
||||
T ret_value;
|
||||
std::coroutine_handle<promise_type> h;
|
||||
};
|
||||
|
||||
enum class AwaitableType {
|
||||
kUnknown = 0,
|
||||
kUartRx = 1,
|
||||
|
@ -60,6 +60,7 @@ int main() {
|
||||
if ((cnt++ % 100000) == 0) {
|
||||
ToggleLed(0);
|
||||
}
|
||||
return false;
|
||||
});
|
||||
// should never get here
|
||||
}
|
||||
|
53
arm/trace.cc
53
arm/trace.cc
@ -1,6 +1,7 @@
|
||||
#include "trace.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
|
||||
#include "itoa.h"
|
||||
#include "lock.h"
|
||||
@ -9,30 +10,62 @@
|
||||
namespace tracing {
|
||||
namespace {
|
||||
|
||||
struct Event {
|
||||
uint32_t timestamp;
|
||||
TraceEvent event;
|
||||
};
|
||||
|
||||
constexpr size_t kTraceBufferSize = 256;
|
||||
std::array<TraceEvent, kTraceBufferSize> buffer;
|
||||
std::array<Event, kTraceBufferSize> buffer;
|
||||
size_t write_ptr = 0;
|
||||
size_t size = 0;
|
||||
|
||||
} // namespace
|
||||
|
||||
void trace(int raw_event) { trace(static_cast<TraceEvent>(raw_event)); }
|
||||
|
||||
void trace(TraceEvent event) {
|
||||
InterruptLock lock;
|
||||
const std::chrono::system_clock::time_point now =
|
||||
std::chrono::system_clock::now();
|
||||
const uint32_t uptime_ticks = now.time_since_epoch().count();
|
||||
|
||||
buffer[write_ptr] = event;
|
||||
{
|
||||
InterruptLock lock;
|
||||
|
||||
write_ptr = (write_ptr + 1) % buffer.size();
|
||||
buffer[write_ptr] = {.timestamp = uptime_ticks, .event = event};
|
||||
|
||||
write_ptr = (write_ptr + 1) % buffer.size();
|
||||
size = std::min(size + 1, kTraceBufferSize);
|
||||
|
||||
#if TRACE_DUMP_WHEN_FULL
|
||||
if (size == kTraceBufferSize) {
|
||||
dump();
|
||||
}
|
||||
#endif // TRACE_DUMP_WHEN_FULL
|
||||
}
|
||||
}
|
||||
|
||||
void dump() {
|
||||
std::rotate(buffer.begin(), buffer.begin() + write_ptr, buffer.end());
|
||||
InterruptLock lock;
|
||||
|
||||
char number[] = "00000000\r\n";
|
||||
|
||||
for (TraceEvent event : buffer) {
|
||||
itoa(static_cast<int>(event), number);
|
||||
UartWriteCrash(number);
|
||||
if (size == kTraceBufferSize) {
|
||||
std::rotate(buffer.begin(), buffer.begin() + write_ptr, buffer.end());
|
||||
}
|
||||
|
||||
char number[] = "00000000";
|
||||
|
||||
UartWriteCrash("----\r\n");
|
||||
for (Event event : std::span{buffer}.subspan(0, size)) {
|
||||
itoa(static_cast<int>(event.timestamp), number);
|
||||
UartWriteCrash(number);
|
||||
UartWriteCrash(" ");
|
||||
itoa(static_cast<int>(event.event), number);
|
||||
UartWriteCrash(number);
|
||||
UartWriteCrash("\r\n");
|
||||
}
|
||||
UartWriteCrash("----\r\n");
|
||||
|
||||
size = 0;
|
||||
write_ptr = 0;
|
||||
}
|
||||
} // namespace tracing
|
||||
|
40
arm/trace.h
40
arm/trace.h
@ -1,14 +1,48 @@
|
||||
#pragma once
|
||||
|
||||
#define TRACE_DUMP_WHEN_FULL 0
|
||||
|
||||
#ifdef __x86_64__
|
||||
#include <cstdio>
|
||||
#define TRACE(x) printf(#x "\n")
|
||||
#else // __x86_64__
|
||||
#define TRACE(...) tracing::trace(__VA_ARGS__)
|
||||
#endif // __x86_64__
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace tracing {
|
||||
enum class TraceEvent : uint8_t {
|
||||
kUnknown = 0,
|
||||
|
||||
kUartIsr,
|
||||
kUartRxCb,
|
||||
kUartTxCb,
|
||||
kUartIsr = 1,
|
||||
kUartRxCb = 2,
|
||||
kUartTxCb = 3,
|
||||
|
||||
kUartSend = 10,
|
||||
kUartRecv = 11,
|
||||
kUartTxBufferFull = 12,
|
||||
kUartTxBufferNotFull = 13,
|
||||
|
||||
kUartWriteDone = 20,
|
||||
|
||||
kAsyncResume = 4,
|
||||
kAsyncEnqueue = 5,
|
||||
kAsyncTask = 6,
|
||||
kAsyncResumeSetPending = 7,
|
||||
kAsyncAwaitWasNotified = 8,
|
||||
kAsyncSchedule = 9,
|
||||
|
||||
kAsyncTaskDone = 14,
|
||||
kAsyncException = 15,
|
||||
kAsyncCallParent = 16,
|
||||
kAsyncCallParentDone = 17,
|
||||
kAsyncCoAwait = 18,
|
||||
kAsyncSuspend = 19,
|
||||
kAsyncDestroy = 21,
|
||||
|
||||
kAsyncGimmeWaiting = 22,
|
||||
kAsyncGimmeResume = 23,
|
||||
};
|
||||
|
||||
void trace(TraceEvent event);
|
||||
|
Loading…
Reference in New Issue
Block a user