From 49839c88a98d3798f7b18c58f54f26f36cacff38 Mon Sep 17 00:00:00 2001 From: Guangxiong Lin Date: Fri, 9 Dec 2022 16:46:49 +0800 Subject: Implement a simple thread pool and refactor Refactor --- eventloop.c | 84 ------------------------------------------------------------- 1 file changed, 84 deletions(-) delete mode 100644 eventloop.c (limited to 'eventloop.c') diff --git a/eventloop.c b/eventloop.c deleted file mode 100644 index ae4377c..0000000 --- a/eventloop.c +++ /dev/null @@ -1,84 +0,0 @@ -#include -#include - -#include "eventloop.h" -#include "util.h" -#include "constant.h" - -struct eventLoop *eventLoopNew() -{ - int epollfd = epoll_create1(0); - if (epollfd == -1) - return NULL; - - struct eventLoop *eventLoop = malloc(sizeof(*eventLoop)); - eventLoop->epollfd = epollfd; - eventLoop->size = EVENT_LOOP_MAX_EVENTS; - - return eventLoop; -} - -int eventLoopWait(struct eventLoop *el, int timeout) -{ - return epoll_wait(el->epollfd, el->events, el->size, timeout); -} - -struct tsocket *eventLoopGetSocket(struct eventLoop *el, int index) -{ - return (struct tsocket *)el->events[index].data.ptr; -} - -int eventLoopAdd(struct eventLoop *el, struct event *ev, int flag) -{ - struct epoll_event epev; - epev.events = flag; - epev.data.ptr = ev; - - return epoll_ctl(el->epollfd, EPOLL_CTL_ADD, ev->fd, &epev); -} - -struct event *eventLoopGet(struct eventLoop *el, int index) -{ - return (struct event *)el->events[index].data.ptr; -} - -void eventLoopLoop(struct eventLoop *el) -{ - int nevents; - struct event *ev; - - for (;;) { - nevents = eventLoopWait(el, -1); - if (nevents == -1) - panic("eventloop wait"); - - for (int i = 0; i < nevents; i++) { - ev = eventLoopGet(el, i); - if (ev->handle(ev->data) == -1) - eventLoopDel(el, ev); - } - } -} - -int eventLoopDel(struct eventLoop *el, struct event *ev) -{ - if (epoll_ctl(el->epollfd, EPOLL_CTL_DEL, ev->fd, NULL) == -1) - return ERROR; - - if (ev->delete) - ev->delete(ev->data); - free(ev); - - return OK; -} - -struct event *eventNew(void *data, int fd, void (*handle), void (*delete)) -{ - struct event *ev = malloc(sizeof(*ev)); - ev->data = data; - ev->handle = handle; - ev->fd = fd; - ev->delete = delete; - - return ev; -} -- cgit v1.2.3