]> PatchyGit - patchyircd.git/commitdiff
netpoll: add epoll implementation
authorEgor <egor@patchysicp.xyz>
Sat, 21 Oct 2023 16:15:26 +0000 (19:15 +0300)
committerEgor <egor@patchysicp.xyz>
Sat, 21 Oct 2023 16:15:26 +0000 (19:15 +0300)
src/netpoll.c

index b06a42b..93b7cb1 100644 (file)
@@ -131,6 +131,73 @@ int net_poll_get(struct net_pollfd *out)
        *out = fds[lastptr-1];
        return ev;
 }
+#elif defined(NETPOLL_USE_EPOLL)
+#include <sys/epoll.h>
+static int epollfd = -1;
+static struct epoll_event events[256] = {0};
+static int nevents = 0;
+static int lastptr = 0;
+void net_poll_init(void)
+{
+       epollfd = epoll_create1(EPOLL_CLOEXEC);
+       if (epollfd == -1) {
+               perror("epoll_create");
+               abort();
+       }
+}
+void net_poll_fini(void)
+{
+       close(epollfd);
+       epollfd = -1;
+}
+void net_poll_add_impl(int i)
+{
+       struct epoll_event e = { .data.fd = fds[i].fd };
+       if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fds[i].fd, &e) == -1) {
+               perror("epoll_ctl(EPOLL_CTL_ADD)");
+               abort();
+       }
+}
+void net_poll_del_impl(int i)
+{
+       if (epoll_ctl(epollfd, EPOLL_CTL_DEL, fds[i].fd, 0) == -1) {
+               perror("epoll_ctl(EPOLL_CTL_DEL)");
+               abort();
+       }
+}
+void net_poll_reg_impl(int i, int ev)
+{
+       struct epoll_event e = { .data.fd = fds[i].fd };
+       e.events = (ev & NETPOLL_IN ? EPOLLIN : 0) | (ev & NETPOLL_OUT ? EPOLLOUT : 0);
+       if (epoll_ctl(epollfd, EPOLL_CTL_MOD, fds[i].fd, &e) == -1) {
+               perror("epoll_ctl(EPOLL_CTL_MOD)");
+               abort();
+       }
+}
+int net_poll_begin(void)
+{
+       int rv = epoll_wait(epollfd, events, sizeof(events)/sizeof(events[0]), -1);
+       lastptr = 0;
+       nevents = rv;
+       return rv;
+}
+int net_poll_get(struct net_pollfd *out)
+{
+       int ev = 0;
+       while (!ev) {
+               if (lastptr == nevents)
+                       return 0;
+               int pev = events[lastptr].events;
+               if (pev & EPOLLIN)
+                       ev |= NETPOLL_IN;
+               if (pev & EPOLLOUT)
+                       ev |= NETPOLL_OUT;
+               lastptr++;
+       }
+       /* FIXME: this is stupid */
+       *out = fds[search(events[lastptr-1].data.fd)];
+       return ev;
+}
 #else
 #error pick a netpoll implementation
 #endif