/* * Wrapper for poll(s, timeout). * Auto restarts with adjusted timeout if interrupted by * signal other than our wakeup signal. */ int NET_Timeout(int s, long timeout) { long prevtime = 0, newtime; struct timeval t; fdEntry_t *fdEntry = getFdEntry(s); /* * Check that fd hasn't been closed. */ if (fdEntry == NULL) { errno = EBADF; return -1; } /* * Pick up current time as may need to adjust timeout */ if (timeout > 0) { gettimeofday(&t, NULL); prevtime = t.tv_sec * 1000 + t.tv_usec / 1000; } for(;;) { struct pollfd pfd; int rv; threadEntry_t self; /* * Poll the fd. If interrupted by our wakeup signal * errno will be set to EBADF. */ pfd.fd = s; pfd.events = POLLIN | POLLERR; startOp(fdEntry, &self); rv = poll(&pfd, 1, timeout); endOp(fdEntry, &self); /* * If interrupted then adjust timeout. If timeout * has expired return 0 (indicating timeout expired). */ if (rv < 0 && errno == EINTR) { if (timeout > 0) { gettimeofday(&t, NULL); newtime = t.tv_sec * 1000 + t.tv_usec / 1000; timeout -= newtime - prevtime; if (timeout <= 0) { return 0; } prevtime = newtime; } } else { return rv; } } }
/* * Wrapper for select(s, timeout). We are using select() on Mac OS due to Bug 7131399. * Auto restarts with adjusted timeout if interrupted by * signal other than our wakeup signal. */ int NET_Timeout(int s, long timeout) { long prevtime = 0, newtime; struct timeval t, *tp = &t; fd_set fds; fd_set* fdsp = NULL; int allocated = 0; threadEntry_t self; fdEntry_t *fdEntry = getFdEntry(s); /* * Check that fd hasn't been closed. */ if (fdEntry == NULL) { errno = EBADF; return -1; } /* * Pick up current time as may need to adjust timeout */ if (timeout > 0) { /* Timed */ struct timeval now; gettimeofday(&now, NULL); prevtime = now.tv_sec * 1000 + now.tv_usec / 1000; t.tv_sec = timeout / 1000; t.tv_usec = (timeout % 1000) * 1000; } else if (timeout < 0) { /* Blocking */ tp = 0; } else { /* Poll */ t.tv_sec = 0; t.tv_usec = 0; } if (s < FD_SETSIZE) { fdsp = &fds; FD_ZERO(fdsp); } else { int length = (howmany(s+1, NFDBITS)) * sizeof(int); fdsp = (fd_set *) calloc(1, length); if (fdsp == NULL) { return -1; // errno will be set to ENOMEM } allocated = 1; } FD_SET(s, fdsp); for(;;) { int rv; /* * call select on the fd. If interrupted by our wakeup signal * errno will be set to EBADF. */ startOp(fdEntry, &self); rv = select(s+1, fdsp, 0, 0, tp); endOp(fdEntry, &self); /* * If interrupted then adjust timeout. If timeout * has expired return 0 (indicating timeout expired). */ if (rv < 0 && errno == EINTR) { if (timeout > 0) { struct timeval now; gettimeofday(&now, NULL); newtime = now.tv_sec * 1000 + now.tv_usec / 1000; timeout -= newtime - prevtime; if (timeout <= 0) { if (allocated != 0) free(fdsp); return 0; } prevtime = newtime; t.tv_sec = timeout / 1000; t.tv_usec = (timeout % 1000) * 1000; } } else { if (allocated != 0) free(fdsp); return rv; } } }