Skip to content

Commit

Permalink
Events arriving
Browse files Browse the repository at this point in the history
  • Loading branch information
hurufu committed Apr 29, 2024
1 parent 3ef26d5 commit 013b5a4
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 22 deletions.
4 changes: 2 additions & 2 deletions fsm/wireprotocol.rl
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ int fsm_wireformat(void*) {
int cs;
unsigned char* p = buf, * pe = p;
%% write init;
set_nonblocking(7);
while ((bytes = sio_read(7, buf, sizeof buf)) > 0) {
set_nonblocking(6);
while ((bytes = sio_read(6, buf, sizeof buf)) > 0) {
pe = (p = buf) + bytes;
LOGDXP(char tmp[4*bytes], "→ % 4zd %s", bytes, PRETTY(p, pe, tmp));
%% write exec;
Expand Down
4 changes: 3 additions & 1 deletion multitasking/contextring.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ void insert(struct coro_context_ring** const cursor, struct coro_context* const
*cursor = new;
}

void shrink(struct coro_context_ring** const cursor) {
struct coro_context* shrink(struct coro_context_ring** const cursor) {
assert(cursor && *cursor);
struct coro_context* const ret = (*cursor)->ctx;
if ((*cursor)->next == (*cursor)) {
*cursor = NULL;
} else {
Expand All @@ -40,4 +41,5 @@ void shrink(struct coro_context_ring** const cursor) {
*cursor = (*cursor)->next;
}
// FIXME: Free memory!
return ret;
}
2 changes: 1 addition & 1 deletion multitasking/contextring.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ struct coro_context_ring {
};

void insert(struct coro_context_ring** cursor, struct coro_context* ctx, const char* name) __attribute__((nonnull (1,2)));
void shrink(struct coro_context_ring** cursor) __attribute__((nonnull (1)));
struct coro_context* shrink(struct coro_context_ring** cursor) __attribute__((nonnull (1)));
77 changes: 59 additions & 18 deletions multitasking/sigio.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,72 @@
#include <assert.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>

#ifndef SIGINFO
# define SIGINFO SIGPWR
#endif

static struct coro_context_ring* s_current;
static struct coro_context_ring* s_current; // TODO: Renamce to s_active
static struct coro_context_ring* s_waiting;
static struct coro_context s_end;
static siginfo_t s_si;

static int suspend(void) {
if (s_current->visited < 100)
return errno = EDEADLK, -1;
s_current->visited++;
coro_transfer(s_current->ctx, &s_end);
return 0;
static char* events_tostring(const size_t size, char buf[static const size], const short e) {
FILE* const s = fmemopen(buf, size, "w");
xfprintf(s, "0x%04hx", e);
# ifdef _XOPEN_SOURCE
if (e & POLLRDNORM)
xfputs(" IN", s);
if (e & POLLWRNORM)
xfputs(" OUT", s);
if (e & POLLRDBAND)
xfputs(" INPRI", s);
if (e & POLLWRBAND)
xfputs(" OUTPRI", s);
# else
if (e & POLLIN)
xfputs(" IN", s);
if (e & POLLPRI)
xfputs(" PRI", s);
if (e & POLLOUT)
xfputs(" OUT", s);
# endif
# ifdef _GNU_SOURCE
if (e & POLLMSG)
xfputs(" MSG", s);
if (e & POLLREMOVE)
xfputs(" REMOVE", s);
if (e & POLLRDHUP)
xfputs(" RDHUP", s);
# endif
if (e & POLLERR)
xfputs(" ERR", s);
if (e & POLLHUP)
xfputs(" HUP", s);
if (e & POLLNVAL)
xfputs(" NVAL", s);
xfclose(s);
return buf;
}

static int suspend_poll(const int fd, const short nevents) {
LOGDX("fd: %d, nevents: %s", fd, (nevents == POLLIN ? "IN" : "OUT"));
LOGDXP(char tmp[32], "fd: %d, nevents: %s", fd, events_tostring(sizeof tmp, tmp, nevents));
if (is_fd_bad(fd))
return -1;
assert(fcntl(fd, F_GETFL) & O_NONBLOCK);
assert(fcntl(fd, F_GETFL) & O_ASYNC);
assert(fcntl(fd, F_GETOWN) == getpid());
assert(fcntl(fd, F_GETSIG) == 0);
assert(fcntl(fd, F_GETSIG) == SIGPOLL);
assert(nevents & (POLLIN | POLLOUT));
while (s_si.si_signo != SIGPOLL || s_si.si_fd != fd || s_si.si_band & nevents)
if (suspend() < 0)
return -1;
while (s_si.si_signo != SIGPOLL || s_si.si_fd != fd || s_si.si_band & nevents) {
if (s_current->visited > 100)
return errno = EDEADLK, -1;
const char* const name = s_current->name;
insert(&s_waiting, shrink(&s_current), name);
coro_transfer(s_waiting->ctx, &s_end);
}
s_si = (siginfo_t){};
return 0;
}

Expand Down Expand Up @@ -70,9 +107,9 @@ static void starter(struct sus_registation_form* const reg) {
sig_exit();
}


int sig_runall(const size_t length, struct sus_registation_form (* const h)[length]) {
assert(h);
close(STDIN_FILENO);
int ret = -1;
struct coro_stuff stuff[length] = {};
coro_create(&s_end, NULL, NULL, NULL, 0);
Expand All @@ -91,14 +128,18 @@ int sig_runall(const size_t length, struct sus_registation_form (* const h)[leng
sigaddset(&s, blocked_signals[i]);
xsigprocmask(SIG_BLOCK, &s, NULL);
const struct timespec timeout = { .tv_sec = 10, .tv_nsec = 0 };

// FIXME: Not working loop
while (1) {
do {
for (; s_current; s_current = s_current ? s_current->next : NULL)
coro_transfer(&s_end, s_current->ctx);
const int sig = xsigtimedwait(&s, &s_si, &timeout);
LOGIX("New signal (%02d): %s", sig, strsignal(sig));
}
if (s_si.si_signo == SIGPOLL)
LOGDXP(char tmp[16], "signal %02d: %s (fd: %d band: %s)",
sig, strsignal(sig), s_si.si_fd, events_tostring(sizeof tmp, tmp, s_si.si_band));
else
psiginfo(&s_si, NULL);
s_current = s_waiting;
s_waiting = NULL;
} while (s_current);
ret = 0;
end:
for (size_t i = 0; i < length; i++) {
Expand Down
3 changes: 3 additions & 0 deletions utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <execinfo.h>

void set_nonblocking(const int fd) {
Expand All @@ -18,6 +19,8 @@ void set_nonblocking(const int fd) {
EXITF("fcntl F_SETFL O_NONBLOCK | O_ASYNC");
if (fcntl(fd, F_SETOWN, getpid()) == -1)
EXITF("fcntl F_SETOWN");
if (fcntl(fd, F_SETSIG, SIGPOLL) == -1)
EXITF("fcntl F_SETSIG");
}

bool is_fd_bad(const int fd) {
Expand Down

0 comments on commit 013b5a4

Please sign in to comment.