chore: rename event loop states

blocking
Jef Roosens 2024-02-14 12:07:14 +01:00
parent 64601b5f21
commit 1b8ba305b5
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
5 changed files with 31 additions and 31 deletions

View File

@ -12,11 +12,11 @@
#define LNM_QUEUE_MULTIPLIER 8 #define LNM_QUEUE_MULTIPLIER 8
typedef enum lnm_loop_state { typedef enum lnm_loop_state {
lnm_loop_state_req = 0, lnm_loop_state_req_io = 0,
lnm_loop_state_res, lnm_loop_state_res_io,
lnm_loop_state_end, lnm_loop_state_end,
lnm_loop_state_req_blocking, lnm_loop_state_req_work,
lnm_loop_state_res_blocking, lnm_loop_state_res_work,
} lnm_loop_state; } lnm_loop_state;
/** /**

View File

@ -133,8 +133,8 @@ void lnm_http_loop_process_steps(lnm_http_conn *conn) {
while ((ctx->cur_step != NULL) && (step != ctx->cur_step)) { while ((ctx->cur_step != NULL) && (step != ctx->cur_step)) {
step = ctx->cur_step; step = ctx->cur_step;
if (step->blocking && (conn->state != lnm_loop_state_req_blocking)) { if (step->blocking && (conn->state != lnm_loop_state_req_work)) {
conn->state = lnm_loop_state_req_blocking; conn->state = lnm_loop_state_req_work;
break; break;
} }
@ -145,7 +145,7 @@ void lnm_http_loop_process_steps(lnm_http_conn *conn) {
break; break;
case lnm_http_step_err_io_needed: case lnm_http_step_err_io_needed:
// Ensure steps that require more I/O are executed on the event loop // Ensure steps that require more I/O are executed on the event loop
conn->state = lnm_loop_state_req; conn->state = lnm_loop_state_req_io;
break; break;
case lnm_http_step_err_close: case lnm_http_step_err_close:
conn->state = lnm_loop_state_end; conn->state = lnm_loop_state_end;
@ -157,7 +157,7 @@ void lnm_http_loop_process_steps(lnm_http_conn *conn) {
} }
if (ctx->cur_step == NULL) { if (ctx->cur_step == NULL) {
conn->state = lnm_loop_state_res; conn->state = lnm_loop_state_res_io;
ctx->state = lnm_http_loop_state_add_headers; ctx->state = lnm_http_loop_state_add_headers;
} }
} }
@ -334,23 +334,23 @@ void (*process_fns[])(lnm_http_conn *conn) = {
lnm_loop_state state_map[] = { lnm_loop_state state_map[] = {
// parse_req // parse_req
lnm_loop_state_req, lnm_loop_state_req_io,
// route // route
lnm_loop_state_req, lnm_loop_state_req_io,
// parse_headers // parse_headers
lnm_loop_state_req, lnm_loop_state_req_io,
// steps // steps
lnm_loop_state_req, lnm_loop_state_req_io,
// add_headers // add_headers
lnm_loop_state_req, lnm_loop_state_req_io,
// write_status_line // write_status_line
lnm_loop_state_res, lnm_loop_state_res_io,
// write_headers // write_headers
lnm_loop_state_res, lnm_loop_state_res_io,
// write_body // write_body
lnm_loop_state_res, lnm_loop_state_res_io,
// finish // finish
lnm_loop_state_res, lnm_loop_state_res_io,
}; };
void lnm_http_loop_process(lnm_http_conn *conn) { void lnm_http_loop_process(lnm_http_conn *conn) {
@ -378,7 +378,7 @@ void lnm_http_loop_process(lnm_http_conn *conn) {
// We move the request to a dedicated buffer if the read buffer needs to be // We move the request to a dedicated buffer if the read buffer needs to be
// reused // reused
if ((conn->state == lnm_loop_state_req) && (conn->state == loop_state) && if ((conn->state == lnm_loop_state_req_io) && (conn->state == loop_state) &&
(!ctx->req.buf.owned) && (ctx->req.buf.len > 0)) { (!ctx->req.buf.owned) && (ctx->req.buf.len > 0)) {
char *buf = malloc(ctx->req.buf.len); char *buf = malloc(ctx->req.buf.len);

View File

@ -55,7 +55,7 @@ lnm_err lnm_loop_accept(lnm_loop *l) {
LNM_RES2(lnm_loop_conn_init(&conn, l), close(conn_fd)); LNM_RES2(lnm_loop_conn_init(&conn, l), close(conn_fd));
conn->fd = conn_fd; conn->fd = conn_fd;
conn->state = lnm_loop_state_req; conn->state = lnm_loop_state_req_io;
struct epoll_event event = {.data.ptr = conn, struct epoll_event event = {.data.ptr = conn,
.events = EPOLLIN | EPOLLET | EPOLLONESHOT}; .events = EPOLLIN | EPOLLET | EPOLLONESHOT};
@ -132,17 +132,17 @@ lnm_err lnm_loop_setup(lnm_loop *l, uint16_t port) {
void lnm_loop_conn_schedule(lnm_loop *l, lnm_loop_conn *conn) { void lnm_loop_conn_schedule(lnm_loop *l, lnm_loop_conn *conn) {
switch (conn->state) { switch (conn->state) {
// IO states get rescheduled in the epoll loop // IO states get rescheduled in the epoll loop
case lnm_loop_state_req: case lnm_loop_state_req_io:
case lnm_loop_state_res: { case lnm_loop_state_res_io: {
struct epoll_event event = { struct epoll_event event = {
.data.ptr = conn, .data.ptr = conn,
.events = (conn->state == lnm_loop_state_req ? EPOLLIN : EPOLLOUT) | .events = (conn->state == lnm_loop_state_req_io ? EPOLLIN : EPOLLOUT) |
EPOLLET | EPOLLONESHOT}; EPOLLET | EPOLLONESHOT};
epoll_ctl(l->epoll_fd, EPOLL_CTL_MOD, conn->fd, &event); epoll_ctl(l->epoll_fd, EPOLL_CTL_MOD, conn->fd, &event);
} break; } break;
case lnm_loop_state_req_blocking: case lnm_loop_state_req_work:
case lnm_loop_state_res_blocking: case lnm_loop_state_res_work:
lnm_loop_queue_push(l->wq, conn); lnm_loop_queue_push(l->wq, conn);
break; break;
case lnm_loop_state_end: { case lnm_loop_state_end: {

View File

@ -1,7 +1,7 @@
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
#include <unistd.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <unistd.h>
#include "lnm/loop.h" #include "lnm/loop.h"
#include "lnm/loop_internal.h" #include "lnm/loop_internal.h"
@ -34,7 +34,7 @@ void lnm_loop_conn_io_req(lnm_loop *l, lnm_loop_conn *conn) {
conn->r.size += res; conn->r.size += res;
l->data_read(conn); l->data_read(conn);
} while (conn->state == lnm_loop_state_req); } while (conn->state == lnm_loop_state_req_io);
} }
void lnm_loop_conn_io_res(lnm_loop *l, lnm_loop_conn *conn) { void lnm_loop_conn_io_res(lnm_loop *l, lnm_loop_conn *conn) {
@ -64,15 +64,15 @@ void lnm_loop_conn_io_res(lnm_loop *l, lnm_loop_conn *conn) {
// writer function more space to work with // writer function more space to work with
memmove(conn->w.buf, &conn->w.buf[res], conn->w.size - res); memmove(conn->w.buf, &conn->w.buf[res], conn->w.size - res);
conn->w.size -= res; conn->w.size -= res;
} while (conn->state == lnm_loop_state_res); } while (conn->state == lnm_loop_state_res_io);
} }
void lnm_loop_conn_io(lnm_loop *l, lnm_loop_conn *conn) { void lnm_loop_conn_io(lnm_loop *l, lnm_loop_conn *conn) {
switch (conn->state) { switch (conn->state) {
case lnm_loop_state_req: case lnm_loop_state_req_io:
lnm_loop_conn_io_req(l, conn); lnm_loop_conn_io_req(l, conn);
break; break;
case lnm_loop_state_res: case lnm_loop_state_res_io:
lnm_loop_conn_io_res(l, conn); lnm_loop_conn_io_res(l, conn);
break; break;
default:; default:;

View File

@ -87,10 +87,10 @@ void lnm_loop_worker_run(void *arg) {
lnm_ldebug("loop", "worker %i processing fd %i", thread_id, conn->fd); lnm_ldebug("loop", "worker %i processing fd %i", thread_id, conn->fd);
switch (conn->state) { switch (conn->state) {
case lnm_loop_state_req_blocking: case lnm_loop_state_req_work:
l->data_read(conn); l->data_read(conn);
break; break;
case lnm_loop_state_res_blocking: case lnm_loop_state_res_work:
l->data_write(conn); l->data_write(conn);
// Other states shouldn't even end up here, so we ignore them // Other states shouldn't even end up here, so we ignore them
default:; default:;