b8324caa98876c93a8e1e38119913a301b501a36
[m6w6/libmemcached] / src / libmemcached / poll.cc
1 /*
2 +--------------------------------------------------------------------+
3 | libmemcached - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020 Michael Wallner <mike@php.net> |
13 +--------------------------------------------------------------------+
14 */
15
16 #include "libmemcached/common.h"
17
18 #if defined(_WIN32)
19 # include "libmemcached/poll.h"
20
21 # include <sys/time.h>
22 # include <strings.h>
23
24 int poll(struct pollfd fds[], nfds_t nfds, int tmo) {
25 fd_set readfds, writefds, errorfds;
26 FD_ZERO(&readfds);
27 FD_ZERO(&writefds);
28 FD_ZERO(&errorfds);
29
30 int maxfd = 0;
31
32 for (nfds_t x = 0; x < nfds; ++x) {
33 if (fds[x].events & (POLLIN | POLLOUT)) {
34 # ifndef _WIN32
35 if (fds[x].fd > maxfd) {
36 maxfd = fds[x].fd;
37 }
38 # endif
39 if (fds[x].events & POLLIN) {
40 FD_SET(fds[x].fd, &readfds);
41 }
42 if (fds[x].events & POLLOUT) {
43 FD_SET(fds[x].fd, &writefds);
44 }
45 }
46 }
47
48 struct timeval timeout = {.tv_sec = tmo / 1000, .tv_usec = (tmo % 1000) * 1000};
49 struct timeval *tp = &timeout;
50 if (tmo == -1) {
51 tp = NULL;
52 }
53 int ret = select(maxfd + 1, &readfds, &writefds, &errorfds, tp);
54 if (ret <= 0) {
55 return ret;
56 }
57
58 /* Iterate through all of them because I need to clear the revent map */
59 for (nfds_t x = 0; x < nfds; ++x) {
60 fds[x].revents = 0;
61 if (FD_ISSET(fds[x].fd, &readfds)) {
62 fds[x].revents |= POLLIN;
63 }
64 if (FD_ISSET(fds[x].fd, &writefds)) {
65 fds[x].revents |= POLLOUT;
66 }
67 if (FD_ISSET(fds[x].fd, &errorfds)) {
68 fds[x].revents |= POLLERR;
69 }
70 }
71
72 return ret;
73 }
74
75 #endif // defined(_WIN32)