diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2023-06-13 01:50:24 +0200 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2023-06-13 01:50:24 +0200 |
commit | 5b58c9fd2880911aa926259d97a396e97f3d4313 (patch) | |
tree | fefbc7a72447f2f12adbe0e16d6f597cf05e3fdf /src | |
parent | use signalfd to stop on SIGTERM (diff) | |
download | cimple-5b58c9fd2880911aa926259d97a396e97f3d4313.tar.gz cimple-5b58c9fd2880911aa926259d97a396e97f3d4313.zip |
event_loop: hide the API details
Diffstat (limited to '')
-rw-r--r-- | src/command.c | 12 | ||||
-rw-r--r-- | src/command.h | 2 | ||||
-rw-r--r-- | src/event_loop.c | 30 | ||||
-rw-r--r-- | src/event_loop.h | 14 | ||||
-rw-r--r-- | src/signal.c | 6 | ||||
-rw-r--r-- | src/signal.h | 2 | ||||
-rw-r--r-- | src/tcp_server.c | 12 |
7 files changed, 34 insertions, 44 deletions
diff --git a/src/command.c b/src/command.c index 6306f7e..f629fde 100644 --- a/src/command.c +++ b/src/command.c @@ -222,14 +222,8 @@ free_ctx: return ret; } -int cmd_dispatcher_add_to_event_loop(const struct cmd_dispatcher *dispatcher, - struct event_loop *loop, int fd) +int cmd_dispatcher_add_to_event_loop(struct cmd_dispatcher *dispatcher, struct event_loop *loop, + int fd) { - struct event_fd entry = { - .fd = fd, - .events = POLLIN, - .handler = cmd_dispatcher_handle_event, - .arg = (void *)dispatcher, - }; - return event_loop_add(loop, &entry); + return event_loop_add(loop, fd, POLLIN, cmd_dispatcher_handle_event, dispatcher); } diff --git a/src/command.h b/src/command.h index 3d13abd..4ef245f 100644 --- a/src/command.h +++ b/src/command.h @@ -29,7 +29,7 @@ void cmd_dispatcher_destroy(struct cmd_dispatcher *); int cmd_dispatcher_handle(const struct cmd_dispatcher *, const struct msg *command, struct msg **response); -int cmd_dispatcher_add_to_event_loop(const struct cmd_dispatcher *, struct event_loop *, int fd); +int cmd_dispatcher_add_to_event_loop(struct cmd_dispatcher *, struct event_loop *, int fd); struct cmd_conn_ctx { int fd; diff --git a/src/event_loop.c b/src/event_loop.c index 8d91480..1400235 100644 --- a/src/event_loop.c +++ b/src/event_loop.c @@ -17,14 +17,28 @@ SIMPLEQ_HEAD(event_fd_queue, event_fd); -static struct event_fd *event_fd_copy(const struct event_fd *src) +struct event_fd { + int fd; + short events; + event_handler handler; + void *arg; + + SIMPLEQ_ENTRY(event_fd) entries; +}; + +static struct event_fd *event_fd_create(int fd, short events, event_handler handler, void *arg) { - struct event_fd *res = malloc(sizeof(*src)); + struct event_fd *res = calloc(1, sizeof(struct event_fd)); if (!res) { log_errno("malloc"); return NULL; } - *res = *src; + + res->fd = fd; + res->events = events; + res->handler = handler; + res->arg = arg; + return res; } @@ -76,16 +90,16 @@ void event_loop_destroy(struct event_loop *loop) free(loop); } -int event_loop_add(struct event_loop *loop, const struct event_fd *entry) +int event_loop_add(struct event_loop *loop, int fd, short events, event_handler handler, void *arg) { - log("Adding descriptor %d to event loop\n", entry->fd); + log("Adding descriptor %d to event loop\n", fd); - struct event_fd *copied = event_fd_copy(entry); - if (!copied) + struct event_fd *entry = event_fd_create(fd, events, handler, arg); + if (!entry) return -1; nfds_t nfds = loop->nfds + 1; - SIMPLEQ_INSERT_TAIL(&loop->entries, copied, entries); + SIMPLEQ_INSERT_TAIL(&loop->entries, entry, entries); loop->nfds = nfds; return 0; diff --git a/src/event_loop.h b/src/event_loop.h index eb5ff05..5273cc6 100644 --- a/src/event_loop.h +++ b/src/event_loop.h @@ -9,7 +9,6 @@ #define __EVENT_LOOP_H__ #include <poll.h> -#include <sys/queue.h> struct event_loop; @@ -20,17 +19,8 @@ int event_loop_run(struct event_loop *); #define EVENT_LOOP_REMOVE 1 -typedef int (*event_loop_handler)(struct event_loop *, int fd, short revents, void *arg); +typedef int (*event_handler)(struct event_loop *, int fd, short revents, void *arg); -struct event_fd { - int fd; - short events; - event_loop_handler handler; - void *arg; - - SIMPLEQ_ENTRY(event_fd) entries; -}; - -int event_loop_add(struct event_loop *, const struct event_fd *); +int event_loop_add(struct event_loop *, int fd, short events, event_handler, void *arg); #endif diff --git a/src/signal.c b/src/signal.c index 9f788b2..2c80c85 100644 --- a/src/signal.c +++ b/src/signal.c @@ -131,9 +131,7 @@ void signalfd_destroy(int fd) file_close(fd); } -int signalfd_add_to_event_loop(int fd, struct event_loop *loop, event_loop_handler handler, - void *arg) +int signalfd_add_to_event_loop(int fd, struct event_loop *loop, event_handler handler, void *arg) { - struct event_fd entry = {.fd = fd, .events = POLLIN, .handler = handler, .arg = arg}; - return event_loop_add(loop, &entry); + return event_loop_add(loop, fd, POLLIN, handler, arg); } diff --git a/src/signal.h b/src/signal.h index e3f5897..d2b7a9c 100644 --- a/src/signal.h +++ b/src/signal.h @@ -23,7 +23,7 @@ int signal_restore(const sigset_t *new); int signalfd_create(const sigset_t *); void signalfd_destroy(int fd); -int signalfd_add_to_event_loop(int fd, struct event_loop *, event_loop_handler handler, void *arg); +int signalfd_add_to_event_loop(int fd, struct event_loop *, event_handler handler, void *arg); int signalfd_listen_for_stops(void); diff --git a/src/tcp_server.c b/src/tcp_server.c index a6263f7..ba310a8 100644 --- a/src/tcp_server.c +++ b/src/tcp_server.c @@ -143,8 +143,8 @@ close_conn: return ret; } -static int tcp_server_event_loop_handler(UNUSED struct event_loop *loop, UNUSED int fd, - UNUSED short revents, void *_server) +static int tcp_server_event_handler(UNUSED struct event_loop *loop, UNUSED int fd, + UNUSED short revents, void *_server) { struct tcp_server *server = (struct tcp_server *)_server; return tcp_server_accept(server); @@ -152,11 +152,5 @@ static int tcp_server_event_loop_handler(UNUSED struct event_loop *loop, UNUSED int tcp_server_add_to_event_loop(struct tcp_server *server, struct event_loop *loop) { - struct event_fd entry = { - .fd = server->fd, - .events = POLLIN, - .handler = tcp_server_event_loop_handler, - .arg = server, - }; - return event_loop_add(loop, &entry); + return event_loop_add(loop, server->fd, POLLIN, tcp_server_event_handler, server); } |