Skip to content

Commit

Permalink
Just saving
Browse files Browse the repository at this point in the history
  • Loading branch information
hurufu committed Apr 14, 2024
1 parent b3a185e commit fe29cc0
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 82 deletions.
44 changes: 3 additions & 41 deletions comultitask.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <unistd.h>
#include <stdbool.h>

enum ioset { IOSET_READ, IOSET_WRITE, IOSET_OOB };

struct coro_stuff {
struct coro_stack stack;
struct coro_context ctx;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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!
}
31 changes: 20 additions & 11 deletions comultitask.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,40 @@

#include <stddef.h>
#include <stdint.h>
#include <sys/types.h>

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)));
14 changes: 8 additions & 6 deletions frob.h
Original file line number Diff line number Diff line change
Expand Up @@ -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*);
28 changes: 4 additions & 24 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,12 @@
#include "utils.h"
#include "comultitask.h"
#include "frob.h"
#include <time.h>
#include <unistd.h>

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");
Expand Down

0 comments on commit fe29cc0

Please sign in to comment.