From fe29cc06611fb5c13012e90c36cd3f992849acf8 Mon Sep 17 00:00:00 2001 From: Aleksy Grabowski Date: Sun, 14 Apr 2024 12:08:48 +0200 Subject: [PATCH] Just saving --- comultitask.c | 44 +++----------------------------------------- comultitask.h | 31 ++++++++++++++++++++----------- frob.h | 14 ++++++++------ main.c | 28 ++++------------------------ 4 files changed, 35 insertions(+), 82 deletions(-) diff --git a/comultitask.c b/comultitask.c index b183637..95692f7 100644 --- a/comultitask.c +++ b/comultitask.c @@ -8,6 +8,8 @@ #include #include +enum ioset { IOSET_READ, IOSET_WRITE, IOSET_OOB }; + struct coro_stuff { struct coro_stack stack; struct coro_context ctx; @@ -153,7 +155,7 @@ static void starter(struct sus_coroutine_reg* const reg) { sus_exit(); } -int sus_io_loop(struct sus_args_io_loop* const args) { +int sus_ioloop(struct sus_ioloop_args* const args) { io_wait_f* const iowait = get_io_wait(args->timeout); struct io_params iop = { .maxfd = 10, @@ -199,43 +201,3 @@ int sus_runall(const size_t length, struct sus_coroutine_reg (* const h)[length] coro_destroy(&s_end); return ret; } - -void insert(struct coro_context_ring** const cursor, struct coro_context* const ctx, const char* const name) { - assert(ctx); - assert(cursor); - struct coro_context_ring* const new = xmalloc(sizeof (struct coro_context_ring)); - memset(new, 0, sizeof *new); - if (*cursor) { - const struct coro_context_ring tmp = { - .name = name, - .ctx = ctx, - .next = (*cursor)->next, - .prev = *cursor - }; - memcpy(new, &tmp, sizeof tmp); - (*cursor)->next->prev = new; - (*cursor)->next = new; - *cursor = new; - } else { - const struct coro_context_ring tmp = { - .name = name, - .ctx = ctx, - .next = new, - .prev = new - }; - memcpy(new, &tmp, sizeof tmp); - } - *cursor = new; -} - -void shrink(struct coro_context_ring** const cursor) { - assert(cursor && *cursor); - if ((*cursor)->next == (*cursor)) { - *cursor = NULL; - } else { - (*cursor)->prev->next = (*cursor)->next; - (*cursor)->next->prev = (*cursor)->prev; - *cursor = (*cursor)->next; - } - // FIXME: Free memory! -} diff --git a/comultitask.h b/comultitask.h index 6432391..da03fe9 100644 --- a/comultitask.h +++ b/comultitask.h @@ -2,31 +2,40 @@ #include #include -#include -enum ioset { IOSET_READ, IOSET_WRITE, IOSET_OOB }; +#define sus_registration(Entry, ...) (struct sus_registation_form){\ + .name = #Entry,\ + .stack_size = 0,\ + .entry = (sus_entry)Entry,\ + .args = &(struct Entry ## _args){ __VA_ARGS__ }\ +} -typedef int (* sus_entry)(void*); +typedef int (* sus_entry)(const void*); -struct sus_coroutine_reg { +struct sus_registation_form { const char* const name; const size_t stack_size; int result; const sus_entry entry; - void* const args; + const void* const args; }; -struct sus_args_io_loop { - int s6_notification_fd; - time_t timeout; +struct sus_ioloop_args { + const time_t timeout; }; +/** Start and schedule all tasks. + * + * Currently uses round-robin scheduling scheme. In well-written program order + * of tasks in the input array shouldn't matter. + */ +int sus_runall(size_t s, struct sus_registation_form (* c)[s]) __attribute__((nonnull (2))); + ssize_t sus_read(int fd, void* data, size_t size) __attribute__((nonnull(2))); ssize_t sus_write(int fd, const void* data, size_t size) __attribute__((nonnull(2))); -int sus_io_loop(struct sus_args_io_loop* args) __attribute__((nonnull(1))); +int sus_ioloop(struct sus_ioloop_args* args) __attribute__((nonnull(1))); + void sus_lend(uint8_t ch, size_t size, void* data) __attribute__((nonnull(3))); ssize_t sus_borrow(uint8_t id, void** value) __attribute__((nonnull(2))); void sus_return(uint8_t id, const void* data, size_t size); void sus_disable(uint8_t id); - -int sus_runall(size_t s, struct sus_coroutine_reg (* c)[s]) __attribute__((nonnull (2))); diff --git a/frob.h b/frob.h index 0f4730d..b892912 100644 --- a/frob.h +++ b/frob.h @@ -287,19 +287,21 @@ struct frob_msg { static_assert(sizeof (struct frob_msg) % 16 == 0, "Message shall fit into 16-byte blocks, so output of od(1) will line up nicely"); #pragma once -struct args_frontend_foreign { +struct fsm_frontend_foreign_args { int cs; }; -struct args_frontend_internal { +struct fsm_frontend_internal_args { int cs; }; -struct args_frontend_timer { +struct fsm_frontend_timer_args { int cs; }; +struct fsm_wireformat_args {}; + int fsm_wireformat(void*); -int fsm_frontend_foreign(struct args_frontend_foreign*); -int fsm_frontend_internal(struct args_frontend_internal*); -int fsm_frontend_timer(struct args_frontend_timer*); +int fsm_frontend_foreign(struct fsm_frontend_foreign_args*); +int fsm_frontend_internal(struct fsm_frontend_internal_args*); +int fsm_frontend_timer(struct fsm_frontend_timer_args*); diff --git a/main.c b/main.c index e5d6c40..95ac1de 100644 --- a/main.c +++ b/main.c @@ -2,32 +2,12 @@ #include "utils.h" #include "comultitask.h" #include "frob.h" -#include -#include int main() { - struct sus_coroutine_reg tasks[] = { - { - .name = "wireformat", - .stack_size = 0, - .entry = fsm_wireformat, - .args = NULL - }, - { - .name = "frontend", - .stack_size = 0, - .entry = (sus_entry)fsm_frontend_foreign, - .args = &(struct args_frontend_foreign){} - }, - { - .name = "io_loop", - .stack_size = 0, - .entry = (sus_entry)sus_io_loop, - .args = &(struct sus_args_io_loop){ - .timeout = 3, - .s6_notification_fd = 1 - } - } + struct sus_registation_form tasks[] = { + sus_registration(fsm_wireformat), + sus_registration(fsm_frontend_foreign), + sus_registration(sus_ioloop, .timeout = 3) }; if (sus_runall(lengthof(tasks), &tasks) != 0) EXITF("Can't start");