fix MinGW
[m6w6/libmemcached] / src / p9y / poll.c
1 #include "poll.hpp"
2
3 #if defined P9Y_NEED_POLL
4
5 int poll(struct pollfd fds[], nfds_t nfds, int tmo) {
6 fd_set readfds, writefds, errorfds;
7 FD_ZERO(&readfds);
8 FD_ZERO(&writefds);
9 FD_ZERO(&errorfds);
10
11 int maxfd = 0;
12
13 for (nfds_t x = 0; x < nfds; ++x) {
14 if (fds[x].events & (POLLIN | POLLOUT)) {
15 # ifndef _WIN32
16 if (fds[x].fd > maxfd) {
17 maxfd = fds[x].fd;
18 }
19 # endif
20 if (fds[x].events & POLLIN) {
21 FD_SET(fds[x].fd, &readfds);
22 }
23 if (fds[x].events & POLLOUT) {
24 FD_SET(fds[x].fd, &writefds);
25 }
26 }
27 }
28
29 struct timeval timeout = {tmo / 1000, (tmo % 1000) * 1000};
30 struct timeval *tp = &timeout;
31 if (tmo == -1) {
32 tp = NULL;
33 }
34 int ret = select(maxfd + 1, &readfds, &writefds, &errorfds, tp);
35 if (ret <= 0) {
36 return ret;
37 }
38
39 /* Iterate through all of them because I need to clear the revent map */
40 for (nfds_t x = 0; x < nfds; ++x) {
41 fds[x].revents = 0;
42 if (FD_ISSET(fds[x].fd, &readfds)) {
43 fds[x].revents |= POLLIN;
44 }
45 if (FD_ISSET(fds[x].fd, &writefds)) {
46 fds[x].revents |= POLLOUT;
47 }
48 if (FD_ISSET(fds[x].fd, &errorfds)) {
49 fds[x].revents |= POLLERR;
50 }
51 }
52
53 return ret;
54 }
55
56 #endif // P9Y_NEED_POLL