2 * Copyright (C) 2010 Brian Aker, Trond Norbye
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
8 * Summary: Implementation of poll by using select
11 #include "mem_config.h"
13 #if defined(WIN32) || defined(__MINGW32__)
14 # include <winsock2.h>
15 # include <ws2tcpip.h>
21 #include "poll/poll.h"
23 int poll(struct pollfd fds
[], nfds_t nfds
, int tmo
)
25 fd_set readfds
, writefds
, errorfds
;
32 for (nfds_t x
= 0; x
< nfds
; ++x
)
34 if (fds
[x
].events
& (POLLIN
| POLLOUT
))
37 if (fds
[x
].fd
> maxfd
)
42 if (fds
[x
].events
& POLLIN
)
44 FD_SET(fds
[x
].fd
, &readfds
);
46 if (fds
[x
].events
& POLLOUT
)
48 FD_SET(fds
[x
].fd
, &writefds
);
53 struct timeval timeout
= { .tv_sec
= tmo
/ 1000,
54 .tv_usec
= (tmo
% 1000) * 1000 };
55 struct timeval
*tp
= &timeout
;
60 int ret
= select(maxfd
+ 1, &readfds
, &writefds
, &errorfds
, tp
);
66 /* Iterate through all of them because I need to clear the revent map */
67 for (nfds_t x
= 0; x
< nfds
; ++x
)
70 if (FD_ISSET(fds
[x
].fd
, &readfds
))
72 fds
[x
].revents
|= POLLIN
;
74 if (FD_ISSET(fds
[x
].fd
, &writefds
))
76 fds
[x
].revents
|= POLLOUT
;
78 if (FD_ISSET(fds
[x
].fd
, &errorfds
))
80 fds
[x
].revents
|= POLLERR
;