aboutsummaryrefslogtreecommitdiff
path: root/src/libthread
diff options
context:
space:
mode:
Diffstat (limited to 'src/libthread')
-rw-r--r--src/libthread/fdwait.c88
1 files changed, 83 insertions, 5 deletions
diff --git a/src/libthread/fdwait.c b/src/libthread/fdwait.c
index 2f8581f1..170305eb 100644
--- a/src/libthread/fdwait.c
+++ b/src/libthread/fdwait.c
@@ -4,10 +4,87 @@
#include <thread.h>
#include <errno.h>
-#include <poll.h>
#include <unistd.h>
#include <fcntl.h>
+#define debugpoll 0
+
+#ifdef __APPLE__
+#include <sys/time.h>
+enum { POLLIN=1, POLLOUT=2, POLLERR=4 };
+struct pollfd
+{
+ int fd;
+ int events;
+ int revents;
+};
+
+int
+poll(struct pollfd *p, int np, int ms)
+{
+ int i, maxfd, n;
+ struct timeval tv, *tvp;
+ fd_set rfd, wfd, efd;
+
+ maxfd = -1;
+ FD_ZERO(&rfd);
+ FD_ZERO(&wfd);
+ FD_ZERO(&efd);
+ for(i=0; i<np; i++){
+ p[i].revents = 0;
+ if(p[i].fd == -1)
+ continue;
+ if(p[i].fd > maxfd)
+ maxfd = p[i].fd;
+ if(p[i].events & POLLIN)
+ FD_SET(p[i].fd, &rfd);
+ if(p[i].events & POLLOUT)
+ FD_SET(p[i].fd, &wfd);
+ FD_SET(p[i].fd, &efd);
+ }
+
+ if(ms != -1){
+ tv.tv_usec = (ms%1000)*1000;
+ tv.tv_sec = ms/1000;
+ tvp = &tv;
+ }else
+ tvp = nil;
+
+ if(debugpoll){
+ fprint(2, "select %d:", maxfd+1);
+ for(i=0; i<=maxfd; i++){
+ if(FD_ISSET(i, &rfd))
+ fprint(2, " r%d", i);
+ if(FD_ISSET(i, &wfd))
+ fprint(2, " w%d", i);
+ if(FD_ISSET(i, &efd))
+ fprint(2, " e%d", i);
+ }
+ fprint(2, "; tp=%p, t=%d.%d\n", tvp, tv.tv_sec, tv.tv_usec);
+ }
+
+ n = select(maxfd+1, &rfd, &wfd, &efd, tvp);
+
+ if(n <= 0)
+ return n;
+
+ for(i=0; i<np; i++){
+ if(p[i].fd == -1)
+ continue;
+ if(FD_ISSET(p[i].fd, &rfd))
+ p[i].revents |= POLLIN;
+ if(FD_ISSET(p[i].fd, &wfd))
+ p[i].revents |= POLLOUT;
+ if(FD_ISSET(p[i].fd, &efd))
+ p[i].revents |= POLLERR;
+ }
+ return n;
+}
+
+#else
+#include <poll.h>
+#endif
+
/*
* Poll file descriptors in an idle loop.
*/
@@ -34,21 +111,22 @@ pollidle(void *v)
uint now;
for(;; yield()){
- //fprint(2, "poll %d:", npoll);
+ if(debugpoll) fprint(2, "poll %d:", npoll);
for(i=0; i<npoll; i++){
- //fprint(2, " %d%c", pfd[i].fd, pfd[i].events==POLLIN ? 'r' : 'w');
+ if(debugpoll) fprint(2, " %d%c", pfd[i].fd, pfd[i].events==POLLIN ? 'r' : 'w');
pfd[i].revents = 0;
}
t = -1;
+ now = p9nsec()/1000000;
for(i=0; i<nsleep; i++){
- now = p9nsec()/1000000;
n = sleeptime[i] - now;
+ if(debugpoll) fprint(2, " s%d", n);
if(n < 0)
n = 0;
if(t == -1 || n < t)
t = n;
}
- //fprint(2, "\n");
+ if(debugpoll) fprint(2, "; t=%d\n", t);
n = poll(pfd, npoll, t);
//fprint(2, "poll ret %d:", n);