tests: attempt to fix memcached_dump() with data
[awesomized/libmemcached] / libmemcached / poll.cc
1 /* LibMemcached
2 * Copyright (C) 2013 Data Differential, http://datadifferential.com/
3 * Copyright (C) 2010 Brian Aker, Trond Norbye
4 * All rights reserved.
5 *
6 * Use and distribution licensed under the BSD license. See
7 * the COPYING file in the parent directory for full text.
8 *
9 * Summary: Implementation of poll by using select
10 *
11 */
12
13 #include "libmemcached/common.h"
14
15 #if defined(_WIN32)
16 #include "libmemcached/poll.h"
17
18 #include <sys/time.h>
19 #include <strings.h>
20
21 int poll(struct pollfd fds[], nfds_t nfds, int tmo)
22 {
23 fd_set readfds, writefds, errorfds;
24 FD_ZERO(&readfds);
25 FD_ZERO(&writefds);
26 FD_ZERO(&errorfds);
27
28 int maxfd= 0;
29
30 for (nfds_t x= 0; x < nfds; ++x)
31 {
32 if (fds[x].events & (POLLIN | POLLOUT))
33 {
34 #ifndef _WIN32
35 if (fds[x].fd > maxfd)
36 {
37 maxfd= fds[x].fd;
38 }
39 #endif
40 if (fds[x].events & POLLIN)
41 {
42 FD_SET(fds[x].fd, &readfds);
43 }
44 if (fds[x].events & POLLOUT)
45 {
46 FD_SET(fds[x].fd, &writefds);
47 }
48 }
49 }
50
51 struct timeval timeout= { .tv_sec = tmo / 1000,
52 .tv_usec= (tmo % 1000) * 1000 };
53 struct timeval *tp= &timeout;
54 if (tmo == -1)
55 {
56 tp= NULL;
57 }
58 int ret= select(maxfd + 1, &readfds, &writefds, &errorfds, tp);
59 if (ret <= 0)
60 {
61 return ret;
62 }
63
64 /* Iterate through all of them because I need to clear the revent map */
65 for (nfds_t x= 0; x < nfds; ++x)
66 {
67 fds[x].revents= 0;
68 if (FD_ISSET(fds[x].fd, &readfds))
69 {
70 fds[x].revents |= POLLIN;
71 }
72 if (FD_ISSET(fds[x].fd, &writefds))
73 {
74 fds[x].revents |= POLLOUT;
75 }
76 if (FD_ISSET(fds[x].fd, &errorfds))
77 {
78 fds[x].revents |= POLLERR;
79 }
80 }
81
82 return ret;
83 }
84
85 #endif // defined(_WIN32)