Async working (in concept)
This commit is contained in:
75
src/async.c
75
src/async.c
@ -1,12 +1,18 @@
|
||||
/**
|
||||
* Sesimos - secure, simple, modern web server
|
||||
* @brief Async handler
|
||||
* @file src/async.c
|
||||
* @author Lorenz Stechauner
|
||||
* @date 2022-12-28
|
||||
*/
|
||||
|
||||
|
||||
#include "async.h"
|
||||
#include "logger.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <poll.h>
|
||||
#include <signal.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <memory.h>
|
||||
|
||||
typedef struct {
|
||||
int fd;
|
||||
@ -23,21 +29,31 @@ typedef struct {
|
||||
evt_listen_t q[256];
|
||||
} listen_queue_t;
|
||||
|
||||
static listen_queue_t listen1, listen2, *listen = &listen1;
|
||||
static volatile sig_atomic_t alive = 1;
|
||||
|
||||
static listen_queue_t listen1;
|
||||
static listen_queue_t listen2;
|
||||
listen_queue_t *listen = &listen1;
|
||||
static int async_add_to_queue(evt_listen_t *evt) {
|
||||
// TODO locking
|
||||
memcpy(&listen->q[listen->n++], evt, sizeof(*evt));
|
||||
return 0;
|
||||
}
|
||||
|
||||
volatile sig_atomic_t alive = 1;
|
||||
|
||||
int async(int fd, int events, int flags, void (*cb)(void *), void *arg, void (*err_cb)(void *), void *err_arg) {
|
||||
return -1;
|
||||
int async(int fd, short events, int flags, void cb(void *), void *arg, void err_cb(void *), void *err_arg) {
|
||||
evt_listen_t evt = {
|
||||
.fd = fd,
|
||||
.events = events,
|
||||
.flags = flags,
|
||||
.cb = cb,
|
||||
.arg = arg,
|
||||
.err_cb = err_cb,
|
||||
.err_arg = err_arg,
|
||||
};
|
||||
return async_add_to_queue(&evt);
|
||||
}
|
||||
|
||||
void async_thread(void) {
|
||||
|
||||
int num_fds = 0;
|
||||
struct pollfd fds[256];
|
||||
int num_fds;
|
||||
struct pollfd fds[256]; // TODO dynamic
|
||||
|
||||
// main event loop
|
||||
while (alive) {
|
||||
@ -45,17 +61,15 @@ void async_thread(void) {
|
||||
listen_queue_t *l = listen;
|
||||
listen = (listen == &listen1) ? &listen2 : &listen1;
|
||||
|
||||
// fill fds with newly added
|
||||
for (int i = 0; i < l->n; i++, num_fds++) {
|
||||
fds[num_fds].fd = l->q[i].fd;
|
||||
fds[num_fds].events = l->q[i].events;
|
||||
// fill fds with newly added queue entries
|
||||
for (num_fds = 0; num_fds < l->n; num_fds++) {
|
||||
fds[num_fds].fd = l->q[num_fds].fd;
|
||||
fds[num_fds].events = l->q[num_fds].events;
|
||||
}
|
||||
|
||||
int ready_fds = poll(fds, num_fds, -1);
|
||||
if (ready_fds < 0) {
|
||||
if (poll(fds, num_fds, -1) < 0) {
|
||||
if (errno == EINTR) {
|
||||
// interrupt
|
||||
continue;
|
||||
} else {
|
||||
// other error
|
||||
critical("Unable to poll for events");
|
||||
@ -64,7 +78,26 @@ void async_thread(void) {
|
||||
}
|
||||
|
||||
for (int i = 0; i < num_fds; i++) {
|
||||
// TODO
|
||||
evt_listen_t *e = &l->q[i];
|
||||
if (fds[i].revents & e->events) {
|
||||
// specified event(s) occurred
|
||||
e->cb(e->arg);
|
||||
|
||||
if (e->flags & ASYNC_KEEP)
|
||||
async_add_to_queue(e);
|
||||
} else if (fds[i].revents & (POLLERR | POLLHUP | POLLNVAL)) {
|
||||
// error occurred
|
||||
e->err_cb(e->err_arg);
|
||||
} else {
|
||||
// no event occurred
|
||||
async_add_to_queue(e);
|
||||
}
|
||||
|
||||
// reset errno to prevent strange behaviour
|
||||
errno = 0;
|
||||
}
|
||||
|
||||
// reset size of queue
|
||||
l->n = 0;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user