arm: add host uart driver tests

All host tests currently pass
Some async refactors may not work well on device, will try later
This commit is contained in:
2023-06-02 23:33:01 -07:00
parent 1f2f08e525
commit f274749050
14 changed files with 494 additions and 97 deletions

View File

@@ -5,21 +5,8 @@
#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__
#include "trace.h"
namespace async {
namespace {
@@ -38,13 +25,14 @@ struct Notification {
Stuff* stuff;
};
std::atomic<Stuff*> work;
std::atomic<Stuff*> work = nullptr;
std::array<Notification, static_cast<size_t>(AwaitableType::kNumTypes)>
notifications = {};
} // namespace
void schedule(std::coroutine_handle<> h, int ms) {
InterruptLock lock;
TRACE(tracing::TraceEvent::kAsyncSchedule);
std::chrono::system_clock::time_point exp =
std::chrono::system_clock::now() + std::chrono::milliseconds(ms);
@@ -70,38 +58,64 @@ void schedule(std::coroutine_handle<> h, int ms) {
s->next = news;
}
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;
}
void main_loop(bool (*idle_function)()) {
while (1) {
if (idle_function != nullptr) {
if (idle_function()) {
reset();
break;
};
}
Stuff* stuff = work;
if (stuff == nullptr) {
continue; // busyloop
}
auto now = std::chrono::system_clock::now();
auto dt = stuff->expiration - now;
if (dt > 0ms) {
continue;
}
TRACE(tracing::TraceEvent::kAsyncTask);
stuff->h();
TRACE(tracing::TraceEvent::kAsyncTaskDone);
if (stuff->h.done()) {
stuff->h.destroy();
}
{
InterruptLock lock;
work = stuff->next;
}
delete stuff;
step();
}
}