#define ULOOP_MAX_EVENTS 10
static struct uloop_timeout *first_timeout;
-static int poll_fd;
+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;
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);
int uloop_init(void)
{
+ if (poll_fd >= 0)
+ return 0;
+
poll_fd = epoll_create(32);
if (poll_fd < 0)
return -1;
{
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;
}