X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=src%2Fp9y%2Fpoll.c;fp=src%2Fp9y%2Fpoll.c;h=b745c8be8dade22402cf127b7e533f3527c0f6a1;hb=2f289c64f625962d945ec3bee80f36bc5c61ee35;hp=0000000000000000000000000000000000000000;hpb=4b584c02c679edd005cd2e542d2ff1d9dcb312b9;p=awesomized%2Flibmemcached diff --git a/src/p9y/poll.c b/src/p9y/poll.c new file mode 100644 index 00000000..b745c8be --- /dev/null +++ b/src/p9y/poll.c @@ -0,0 +1,56 @@ +#include "poll.hpp" + +#if defined P9Y_NEED_POLL + +int poll(struct pollfd fds[], nfds_t nfds, int tmo) { + fd_set readfds, writefds, errorfds; + FD_ZERO(&readfds); + FD_ZERO(&writefds); + FD_ZERO(&errorfds); + + int maxfd = 0; + + for (nfds_t x = 0; x < nfds; ++x) { + if (fds[x].events & (POLLIN | POLLOUT)) { +# ifndef _WIN32 + if (fds[x].fd > maxfd) { + maxfd = fds[x].fd; + } +# endif + if (fds[x].events & POLLIN) { + FD_SET(fds[x].fd, &readfds); + } + if (fds[x].events & POLLOUT) { + FD_SET(fds[x].fd, &writefds); + } + } + } + + struct timeval timeout = {tmo / 1000, (tmo % 1000) * 1000}; + struct timeval *tp = &timeout; + if (tmo == -1) { + tp = NULL; + } + int ret = select(maxfd + 1, &readfds, &writefds, &errorfds, tp); + if (ret <= 0) { + return ret; + } + + /* Iterate through all of them because I need to clear the revent map */ + for (nfds_t x = 0; x < nfds; ++x) { + fds[x].revents = 0; + if (FD_ISSET(fds[x].fd, &readfds)) { + fds[x].revents |= POLLIN; + } + if (FD_ISSET(fds[x].fd, &writefds)) { + fds[x].revents |= POLLOUT; + } + if (FD_ISSET(fds[x].fd, &errorfds)) { + fds[x].revents |= POLLERR; + } + } + + return ret; +} + +#endif // P9Y_NEED_POLL