2 * Copyright (C) 2013 Data Differential, http://datadifferential.com/
3 * Copyright (C) 2010 Brian Aker, Trond Norbye
6 * Use and distribution licensed under the BSD license. See
7 * the COPYING file in the parent directory for full text.
9 * Summary: Implementation of poll by using select
13 #include "libmemcached/common.h"
16 #include "libmemcached/poll.h"
21 int poll(struct pollfd fds
[], nfds_t nfds
, int tmo
)
23 fd_set readfds
, writefds
, errorfds
;
30 for (nfds_t x
= 0; x
< nfds
; ++x
)
32 if (fds
[x
].events
& (POLLIN
| POLLOUT
))
35 if (fds
[x
].fd
> maxfd
)
40 if (fds
[x
].events
& POLLIN
)
42 FD_SET(fds
[x
].fd
, &readfds
);
44 if (fds
[x
].events
& POLLOUT
)
46 FD_SET(fds
[x
].fd
, &writefds
);
51 struct timeval timeout
= { .tv_sec
= tmo
/ 1000,
52 .tv_usec
= (tmo
% 1000) * 1000 };
53 struct timeval
*tp
= &timeout
;
58 int ret
= select(maxfd
+ 1, &readfds
, &writefds
, &errorfds
, tp
);
64 /* Iterate through all of them because I need to clear the revent map */
65 for (nfds_t x
= 0; x
< nfds
; ++x
)
68 if (FD_ISSET(fds
[x
].fd
, &readfds
))
70 fds
[x
].revents
|= POLLIN
;
72 if (FD_ISSET(fds
[x
].fd
, &writefds
))
74 fds
[x
].revents
|= POLLOUT
;
76 if (FD_ISSET(fds
[x
].fd
, &errorfds
))
78 fds
[x
].revents
|= POLLERR
;
85 #endif // defined(_WIN32)