X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=src%2Fsimple-watch.c;h=a7b4e4218024aed1c1194d5c231e0b12230a86cb;hb=aa30c333011fc09b057c0468151b73ba6828b25e;hp=77da9f84e0e3a099e71ee0f56b27bc13cd475a27;hpb=b9cafcae35680b33ba1e0d6da08a355c5b62faab;p=catta diff --git a/src/simple-watch.c b/src/simple-watch.c index 77da9f8..a7b4e42 100644 --- a/src/simple-watch.c +++ b/src/simple-watch.c @@ -33,6 +33,7 @@ #include #include #include +#include #include "fdutil.h" // catta_set_nonblock #include "internal.h" // closesocket @@ -100,7 +101,7 @@ void catta_simple_poll_wakeup(CattaSimplePoll *s) { char c = 'W'; assert(s); - write(s->wakeup_pipe[1], &c, sizeof(c)); + (void)writepipe(s->wakeup_pipe[1], &c, sizeof(c)); s->wakeup_issued = 1; } @@ -112,9 +113,15 @@ static void clear_wakeup(CattaSimplePoll *s) { s->wakeup_issued = 0; - for(;;) - if (read(s->wakeup_pipe[0], &c, sizeof(c)) != sizeof(c)) + printf("(clear wake-up"); fflush(stdout); // XXX + for(;;) { + int n; + ioctl(s->wakeup_pipe[0], FIONREAD, &n); + printf(" %d", n); fflush(stdout); // XXX + if (readpipe(s->wakeup_pipe[0], c, sizeof(c)) != sizeof(c)) break; + } + printf(")\n"); // XXX } static CattaWatch* watch_new(const CattaPoll *api, int fd, CattaWatchEvent event, CattaWatchCallback callback, void *userdata) { @@ -309,13 +316,18 @@ CattaSimplePoll *catta_simple_poll_new(void) { if (!(s = catta_new(CattaSimplePoll, 1))) return NULL; + winsock_init(); // on Windows, pipe uses sockets; no-op on other platforms if (pipe(s->wakeup_pipe) < 0) { - catta_free(s); - return NULL; + catta_log_error(__FILE__": pipe() failed: %s", errnostrsocket()); + goto fail; } - catta_set_nonblock(s->wakeup_pipe[0]); - catta_set_nonblock(s->wakeup_pipe[1]); + if (catta_set_nonblock(s->wakeup_pipe[0]) < 0 || + catta_set_nonblock(s->wakeup_pipe[1]) < 0) + { + catta_log_error(__FILE__": O_NONBLOCK failed: %s", errnostrsocket()); + goto fail; + } s->api.userdata = s; @@ -350,6 +362,11 @@ CattaSimplePoll *catta_simple_poll_new(void) { CATTA_LLIST_HEAD_INIT(CattaTimeout, s->timeouts); return s; + +fail: + catta_free(s); + winsock_exit(); + return NULL; } void catta_simple_poll_free(CattaSimplePoll *s) { @@ -362,12 +379,13 @@ void catta_simple_poll_free(CattaSimplePoll *s) { catta_free(s->pollfds); if (s->wakeup_pipe[0] >= 0) - closesocket(s->wakeup_pipe[0]); + closepipe(s->wakeup_pipe[0]); if (s->wakeup_pipe[1] >= 0) - closesocket(s->wakeup_pipe[1]); + closepipe(s->wakeup_pipe[1]); catta_free(s); + winsock_exit(); // match the winsock_init in catta_simple_poll_new } static int rebuild(CattaSimplePoll *s) { @@ -512,12 +530,20 @@ int catta_simple_poll_run(CattaSimplePoll *s) { for (;;) { errno = 0; + // XXX debug + { + printf("(poll %d...", s->n_pollfds); + fflush(stdout); + } if (s->poll_func(s->pollfds, s->n_pollfds, s->prepared_timeout, s->poll_func_userdata) < 0) { - if (errno == EINTR) + if (errno == EINTR) { + printf(" interrupted)\n"); // XXX continue; + } s->state = STATE_FAILURE; + printf(" FAIL)\n"); // XXX return -1; } @@ -540,7 +566,20 @@ int catta_simple_poll_dispatch(CattaSimplePoll *s) { assert(s->state == STATE_RAN); s->state = STATE_DISPATCHING; - /* We execute only on callback in every iteration */ + // XXX debug + { + int i, nready=0, nwatches=0; + for(w=s->watches; w; w=w->watches_next) + nwatches++; + for(i=0; in_pollfds; i++) + if(s->pollfds[i].revents) + nready++; + if(nready > 0 && s->pollfds[i].revents) + printf(" wake-up,"); + printf(" %d ready, %d watches)\n", nready, nwatches); + } + + /* We execute only one callback in every iteration */ /* Check whether the wakeup time has been reached now */ if ((next_timeout = find_next_timeout(s))) {