diff options
Diffstat (limited to 'src/libthread/fdwait.c')
-rw-r--r-- | src/libthread/fdwait.c | 159 |
1 files changed, 11 insertions, 148 deletions
diff --git a/src/libthread/fdwait.c b/src/libthread/fdwait.c index a46ea456..d3983419 100644 --- a/src/libthread/fdwait.c +++ b/src/libthread/fdwait.c @@ -7,164 +7,25 @@ #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) +void +fdwait() { - 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. - */ - -typedef struct Poll Poll; - -struct Poll -{ - Channel *c; /* for sending back */ -}; - -static Channel *sleepchan[64]; -static int sleeptime[64]; -static int nsleep; - -static struct pollfd pfd[64]; -static struct Poll polls[64]; -static int npoll; - -static void -pollidle(void *v) -{ - int i, n, t; - uint now; - - for(;; yield()){ - if(debugpoll) fprint(2, "poll %d:", npoll); - for(i=0; i<npoll; i++){ - 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++){ - n = sleeptime[i] - now; - if(debugpoll) fprint(2, " s%d", n); - if(n < 0) - n = 0; - if(t == -1 || n < t) - t = n; - } - if(debugpoll) fprint(2, "; t=%d\n", t); - - n = poll(pfd, npoll, t); - //fprint(2, "poll ret %d:", n); - now = p9nsec()/1000000; - for(i=0; i<nsleep; i++){ - if((int)(sleeptime[i] - now) < 0){ - nbsendul(sleepchan[i], 0); - nsleep--; - sleepchan[i] = sleepchan[nsleep]; - sleeptime[i] = sleeptime[nsleep]; - i--; - } - } - - if(n <= 0) - continue; - for(i=0; i<npoll; i++) - if(pfd[i].fd != -1 && pfd[i].revents){ - //fprint(2, " %d", pfd[i].fd); - pfd[i].fd = -1; - pfd[i].events = 0; - pfd[i].revents = 0; - nbsendul(polls[i].c, 1); - //fprint(2, " x%d", pfd[i].fd); - } - //fprint(2, "\n"); - } + if(mode=='w') + FD_SET(&wfd, fd); + else + FD_SET(&rfd, fd); + FD_SET(&efd, fd); + select(fd+1, &rfd, &wfd, &efd, nil); } void threadfdwaitsetup(void) { - static int setup = 0; - - if(!setup){ - setup = 1; - proccreate(pollidle, nil, 16384); - } } void @@ -199,6 +60,8 @@ _threadfdwait(int fd, int rw, ulong pc) polls[i].c = &s.c; if(0) fprint(2, "%s [%3d] fdwait %d %c list *0x%lux\n", argv0, threadid(), fd, rw, pc); + if(pollpid) + postnote(PNPROC, pollpid, "interrupt"); recvul(&s.c); } |