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
15 int poll(struct pollfd fds
[], nfds_t nfds
, int tmo
)
17 fd_set readfds
, writefds
, errorfds
;
24 for (nfds_t x
= 0; x
< nfds
; ++x
)
26 if (fds
[x
].events
& (POLLIN
| POLLOUT
))
29 if (fds
[x
].fd
> maxfd
)
34 if (fds
[x
].events
& POLLIN
)
36 FD_SET(fds
[x
].fd
, &readfds
);
38 if (fds
[x
].events
& POLLOUT
)
40 FD_SET(fds
[x
].fd
, &writefds
);
45 struct timeval timeout
= { .tv_sec
= tmo
/ 1000,
46 .tv_usec
= (tmo
% 1000) * 1000 };
47 struct timeval
*tp
= &timeout
;
52 int ret
= select(maxfd
+ 1, &readfds
, &writefds
, &errorfds
, tp
);
58 /* Iterate through all of them because I need to clear the revent map */
59 for (nfds_t x
= 0; x
< nfds
; ++x
)
62 if (FD_ISSET(fds
[x
].fd
, &readfds
))
64 fds
[x
].revents
|= POLLIN
;
66 if (FD_ISSET(fds
[x
].fd
, &writefds
))
68 fds
[x
].revents
|= POLLOUT
;
70 if (FD_ISSET(fds
[x
].fd
, &errorfds
))
72 fds
[x
].revents
|= POLLERR
;