X-Git-Url: http://git.archive.openwrt.org/?p=project%2Flibubox.git;a=blobdiff_plain;f=uloop.c;h=f60eb41055053d4e582d1347a4cdd594016727e4;hp=4be5f45b17c9acf052e07afc4360ac6f595ee526;hb=11079ba5829a4d14461612fe236d6584b53892c3;hpb=bf87f247d8cf83a88626e9bf4edfb8711c115a2c diff --git a/uloop.c b/uloop.c index 4be5f45..f60eb41 100644 --- a/uloop.c +++ b/uloop.c @@ -46,14 +46,17 @@ #endif #define ULOOP_MAX_EVENTS 10 -struct uloop_timeout *first_timeout; -static int poll_fd; +static struct uloop_timeout *first_timeout; +static int poll_fd = -1; bool uloop_cancelled = false; #ifdef USE_KQUEUE int uloop_init(void) { + if (poll_fd >= 0) + return 0; + poll_fd = kqueue(); if (poll_fd < 0) return -1; @@ -118,7 +121,7 @@ static void uloop_run_events(int timeout) if (timeout > 0) { ts.tv_sec = timeout / 1000; - ts.tv_nsec = timeout * 1000000; + ts.tv_nsec = (timeout % 1000) * 1000000; } nfds = kevent(poll_fd, NULL, 0, events, ARRAY_SIZE(events), timeout > 0 ? &ts : NULL); @@ -160,6 +163,9 @@ static void uloop_run_events(int timeout) int uloop_init(void) { + if (poll_fd >= 0) + return 0; + poll_fd = epoll_create(32); if (poll_fd < 0) return -1; @@ -368,11 +374,6 @@ static void uloop_process_timeouts(struct timeval *tv) } } -void uloop_end(void) -{ - uloop_cancelled = true; -} - void uloop_run(void) { struct timeval tv; @@ -382,11 +383,17 @@ void uloop_run(void) { gettimeofday(&tv, NULL); uloop_process_timeouts(&tv); + if (uloop_cancelled) + break; uloop_run_events(uloop_get_next_timeout(&tv)); } } void uloop_done(void) { + if (poll_fd < 0) + return; + close(poll_fd); + poll_fd = -1; }