]> PatchyGit - patchyircd.git/commitdiff
netpoll: add poll implementation
authorEgor <egor@patchysicp.xyz>
Sat, 21 Oct 2023 15:49:27 +0000 (18:49 +0300)
committerEgor <egor@patchysicp.xyz>
Sat, 21 Oct 2023 15:49:27 +0000 (18:49 +0300)
config.example.mk
src/netpoll.c
src/netpoll.h

index 84b508b..3a75b9b 100644 (file)
@@ -1,7 +1,7 @@
 HEADERS+=config.mk
 CC=clang
-CFLAGS=-Wall -Wextra -DHAS_TLS
+CFLAGS=-Wall -Wextra -DHAS_TLS -DNETPOLL_USE_POLL
 LDLIBS=-ltls
 CC.w32=i686-w64-mingw32-gcc
-CFLAGS.w32=-Wall -Wextra
+CFLAGS.w32=-Wall -Wextra -DNETPOLL_USE_SELECT
 LDLIBS.w32=-lws2_32
index 88355d0..b06a42b 100644 (file)
@@ -81,6 +81,56 @@ int net_poll_get(struct net_pollfd *out)
        *out = fds[lastptr-1];
        return ev;
 }
+#elif defined(NETPOLL_USE_POLL)
+static struct pollfd pollfds[NETPOLL_MAXFDS] = {0};
+static int lastptr = 0;
+void net_poll_init(void)
+{
+}
+void net_poll_fini(void)
+{
+}
+void net_poll_add_impl(int i)
+{
+       memmove(pollfds+i+1, pollfds+i, sizeof(pollfds[0])*(nfds-1-i));
+       pollfds[i].fd = fds[i].fd;
+       pollfds[i].events = 0;
+       pollfds[i].revents = 0;
+       if (lastptr >= i)
+               lastptr++;
+}
+void net_poll_del_impl(int i)
+{
+       memmove(pollfds+i, pollfds+i+1, sizeof(pollfds[0])*(nfds-(i+1)));
+       if (lastptr > i)
+               lastptr--;
+}
+void net_poll_reg_impl(int i, int ev)
+{
+       pollfds[i].events = (ev & NETPOLL_IN ? POLLIN : 0) | (ev & NETPOLL_OUT ? POLLOUT : 0);
+}
+int net_poll_begin(void)
+{
+       int rv = poll(pollfds, nfds, -1);
+       lastptr = 0;
+       return rv;
+}
+int net_poll_get(struct net_pollfd *out)
+{
+       int ev = 0;
+       while (!ev) {
+               if (lastptr == nfds)
+                       return 0;
+               int pev = pollfds[lastptr].revents;
+               if (pev & POLLIN)
+                       ev |= NETPOLL_IN;
+               if (pev & POLLOUT)
+                       ev |= NETPOLL_OUT;
+               lastptr++;
+       }
+       *out = fds[lastptr-1];
+       return ev;
+}
 #else
 #error pick a netpoll implementation
 #endif
index d4f1f43..46346ff 100644 (file)
@@ -1,7 +1,6 @@
 #ifndef NETPOLL_H_
 #define NETPOLL_H_
 #include "netsock.h"
-#define NETPOLL_USE_SELECT
 struct net_pollfd {
        void *assoc;
        SOCKET fd;