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:
Paul Mathieu 2022-06-19 09:32:09 +02:00
parent 77bc82d294
commit 15f2657254
5 changed files with 245 additions and 70 deletions

View File

@ -3,6 +3,23 @@
#include <array> #include <array>
#include <atomic> #include <atomic>
#include <chrono> #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 async {
namespace { namespace {
@ -16,13 +33,19 @@ struct Stuff {
Stuff* next; Stuff* next;
}; };
struct Notification {
bool pending; // can only be true if stuff is nullptr
Stuff* stuff;
};
std::atomic<Stuff*> work; std::atomic<Stuff*> work;
std::array<std::atomic<Stuff*>, static_cast<size_t>(AwaitableType::kNumTypes)> std::array<Notification, static_cast<size_t>(AwaitableType::kNumTypes)>
queues; notifications = {};
} // namespace } // namespace
void schedule(std::coroutine_handle<> h, int ms) { void schedule(std::coroutine_handle<> h, int ms) {
TRACE(tracing::TraceEvent::kAsyncSchedule);
std::chrono::system_clock::time_point exp = std::chrono::system_clock::time_point exp =
std::chrono::system_clock::now() + std::chrono::milliseconds(ms); std::chrono::system_clock::now() + std::chrono::milliseconds(ms);
Stuff* news = new Stuff{ Stuff* news = new Stuff{
@ -47,10 +70,12 @@ void schedule(std::coroutine_handle<> h, int ms) {
s->next = news; s->next = news;
} }
void main_loop(void (*idle_function)()) { void main_loop(bool (*idle_function)()) {
while (1) { while (1) {
if (idle_function != nullptr) { if (idle_function != nullptr) {
idle_function(); if (idle_function()) {
break;
};
} }
Stuff* stuff = work; Stuff* stuff = work;
if (stuff == nullptr) { if (stuff == nullptr) {
@ -64,38 +89,65 @@ void main_loop(void (*idle_function)()) {
continue; continue;
} }
TRACE(tracing::TraceEvent::kAsyncTask);
stuff->h(); stuff->h();
TRACE(tracing::TraceEvent::kAsyncTaskDone);
if (stuff->h.done()) {
stuff->h.destroy();
}
{
InterruptLock lock;
Stuff* oldstuff = stuff;
work = stuff->next; work = stuff->next;
}
delete oldstuff; delete stuff;
} }
} }
void enqueue(std::coroutine_handle<> h, AwaitableType type) { void enqueue(std::coroutine_handle<> h, AwaitableType type) {
Stuff* item = new Stuff{.h = h};
auto ttype = static_cast<size_t>(type); auto ttype = static_cast<size_t>(type);
Stuff* stuff = queues[ttype];
{
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) { if (stuff == nullptr) {
queues[ttype] = item; notifications[ttype].stuff = item;
return; return;
} }
while (stuff->next != nullptr) { while (stuff->next != nullptr) {
stuff = stuff->next; stuff = stuff->next;
} }
stuff->next = item; stuff->next = item;
}
} }
void resume(AwaitableType type) { void resume(AwaitableType type) {
auto ttype = static_cast<size_t>(type); auto ttype = static_cast<size_t>(type);
Stuff* stuff = queues[ttype]; Stuff* stuff = nullptr;
{
InterruptLock lock;
stuff = notifications[ttype].stuff;
if (stuff == nullptr) { if (stuff == nullptr) {
notifications[ttype].pending = true;
return; return;
} }
queues[ttype] = stuff->next; notifications[ttype].stuff = stuff->next;
schedule(stuff->h); schedule(stuff->h);
}
delete stuff; delete stuff;
} }

View File

@ -1,18 +1,29 @@
#pragma once #pragma once
#include <chrono> #include <chrono>
#include <coroutine>
#include <utility> #include <utility>
#include "trace.h"
#ifdef __clang__
#include <experimental/coroutine>
namespace std {
using namespace experimental;
}
#else // __clang__
#include <coroutine>
#endif // __clang__
namespace async { namespace async {
struct task_final_suspend { struct task_final_suspend {
bool await_ready() noexcept(true) { return false; } bool await_ready() noexcept(true) { return false; }
void await_suspend(std::coroutine_handle<> h) noexcept(true) { void await_suspend(std::coroutine_handle<> h) noexcept(true) {
if (parent) { if (parent) {
TRACE(tracing::TraceEvent::kAsyncCallParent);
parent(); parent();
TRACE(tracing::TraceEvent::kAsyncCallParentDone);
} }
h.destroy();
} }
void await_resume() noexcept(true) {} void await_resume() noexcept(true) {}
@ -20,35 +31,7 @@ struct task_final_suspend {
}; };
template <typename T = void> template <typename T = void>
struct task { 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;
};
template <> template <>
struct task<void> { struct task<void> {
@ -64,22 +47,94 @@ struct task<void> {
return {.parent = parent}; return {.parent = parent};
} }
void return_void() {} void return_void() {}
void unhandled_exception() {} void unhandled_exception() {
TRACE(tracing::TraceEvent::kAsyncException);
}
std::coroutine_handle<> parent; std::coroutine_handle<> parent;
}; };
// awaitable // awaitable
bool await_ready() { return h.done(); } bool await_ready() {
void await_suspend(std::coroutine_handle<> ha) { TRACE(tracing::TraceEvent::kAsyncCoAwait);
h.promise().parent = ha;
h(); 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() {} void await_resume() {}
std::coroutine_handle<promise_type> h; 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 { enum class AwaitableType {
kUnknown = 0, kUnknown = 0,
kUartRx = 1, kUartRx = 1,

View File

@ -60,6 +60,7 @@ int main() {
if ((cnt++ % 100000) == 0) { if ((cnt++ % 100000) == 0) {
ToggleLed(0); ToggleLed(0);
} }
return false;
}); });
// should never get here // should never get here
} }

View File

@ -1,6 +1,7 @@
#include "trace.h" #include "trace.h"
#include <algorithm> #include <algorithm>
#include <chrono>
#include "itoa.h" #include "itoa.h"
#include "lock.h" #include "lock.h"
@ -9,30 +10,62 @@
namespace tracing { namespace tracing {
namespace { namespace {
struct Event {
uint32_t timestamp;
TraceEvent event;
};
constexpr size_t kTraceBufferSize = 256; constexpr size_t kTraceBufferSize = 256;
std::array<TraceEvent, kTraceBufferSize> buffer; std::array<Event, kTraceBufferSize> buffer;
size_t write_ptr = 0; size_t write_ptr = 0;
size_t size = 0;
} // namespace } // namespace
void trace(int raw_event) { trace(static_cast<TraceEvent>(raw_event)); } void trace(int raw_event) { trace(static_cast<TraceEvent>(raw_event)); }
void trace(TraceEvent event) { void trace(TraceEvent event) {
const std::chrono::system_clock::time_point now =
std::chrono::system_clock::now();
const uint32_t uptime_ticks = now.time_since_epoch().count();
{
InterruptLock lock; InterruptLock lock;
buffer[write_ptr] = event; buffer[write_ptr] = {.timestamp = uptime_ticks, .event = event};
write_ptr = (write_ptr + 1) % buffer.size(); 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() { void dump() {
InterruptLock lock;
if (size == kTraceBufferSize) {
std::rotate(buffer.begin(), buffer.begin() + write_ptr, buffer.end()); std::rotate(buffer.begin(), buffer.begin() + write_ptr, buffer.end());
char number[] = "00000000\r\n";
for (TraceEvent event : buffer) {
itoa(static_cast<int>(event), number);
UartWriteCrash(number);
} }
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 } // namespace tracing

View File

@ -1,14 +1,48 @@
#pragma once #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> #include <cstdint>
namespace tracing { namespace tracing {
enum class TraceEvent : uint8_t { enum class TraceEvent : uint8_t {
kUnknown = 0, kUnknown = 0,
kUartIsr, kUartIsr = 1,
kUartRxCb, kUartRxCb = 2,
kUartTxCb, 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); void trace(TraceEvent event);