23 int pselect(
int n, fd_set* readfds, fd_set* writefds, fd_set* exceptfds,
24 const struct timespec* timeout,
const sigset_t* sigmask);
26 #include <sys/select.h>
30 #define NANOSECONDS_PER_SECOND 1000000000L
32 static const char* netio_str =
"netio";
63 if (!netio || !handler) {
89 if (!netio || !handler) {
92 for (lptr = &netio->
handlers; *lptr; lptr = &(*lptr)->
next) {
93 if ((*lptr)->handler == handler) {
114 timeval_to_timespec(
struct timespec* left,
const struct timeval* right)
116 left->tv_sec = right->tv_sec;
117 left->tv_nsec = 1000 * right->tv_usec;
126 timespec_compare(
const struct timespec* left,
127 const struct timespec* right)
129 if (left->tv_sec < right->tv_sec) {
131 }
else if (left->tv_sec > right->tv_sec) {
133 }
else if (left->tv_nsec < right->tv_nsec) {
135 }
else if (left->tv_nsec > right->tv_nsec) {
149 left->tv_sec += right->tv_sec;
150 left->tv_nsec += right->tv_nsec;
164 timespec_subtract(
struct timespec* left,
const struct timespec* right)
166 left->tv_sec -= right->tv_sec;
167 left->tv_nsec -= right->tv_nsec;
168 if (left->tv_nsec < 0L) {
180 const struct timespec*
183 struct timeval current_timeval;
186 if (gettimeofday(¤t_timeval, NULL) == -1) {
188 "gettimeofday() failed (%s)", netio_str,
206 const sigset_t* sigmask)
208 fd_set readfds, writefds, exceptfds;
210 int have_timeout = 0;
211 struct timespec minimum_timeout;
225 memcpy(&minimum_timeout, timeout,
sizeof(
struct timespec));
235 if (handler->
fd >= 0 && handler->
fd < (
int) FD_SETSIZE) {
236 if (handler->
fd > max_fd) {
237 max_fd = handler->
fd;
240 FD_SET(handler->
fd, &readfds);
243 FD_SET(handler->
fd, &writefds);
246 FD_SET(handler->
fd, &exceptfds);
251 struct timespec relative;
252 relative.tv_sec = handler->
timeout->tv_sec;
253 relative.tv_nsec = handler->
timeout->tv_nsec;
257 timespec_compare(&relative, &minimum_timeout) < 0) {
259 minimum_timeout.tv_sec = relative.tv_sec;
260 minimum_timeout.tv_nsec = relative.tv_nsec;
261 timeout_handler = handler;
266 if (have_timeout && minimum_timeout.tv_sec < 0) {
271 ods_log_debug(
"[%s] dispatch timeout event without checking for "
272 "other events", netio_str);
273 if (timeout_handler &&
275 timeout_handler->event_handler(netio, timeout_handler,
281 rc =
pselect(max_fd + 1, &readfds, &writefds, &exceptfds,
282 have_timeout ? &minimum_timeout : NULL, sigmask);
284 if(errno == EINVAL || errno == EACCES || errno == EBADF) {
297 "expired", netio_str);
302 if (timeout_handler &&
304 timeout_handler->event_handler(netio, timeout_handler,
315 for (l = netio->
handlers; l && rc; ) {
318 if (handler->
fd >= 0 && handler->
fd < (
int) FD_SETSIZE) {
320 if (FD_ISSET(handler->
fd, &readfds)) {
322 FD_CLR(handler->
fd, &readfds);
325 if (FD_ISSET(handler->
fd, &writefds)) {
327 FD_CLR(handler->
fd, &writefds);
330 if (FD_ISSET(handler->
fd, &exceptfds)) {
332 FD_CLR(handler->
fd, &exceptfds);