diff --git a/src/liblwevent/200_eventloop.c b/src/liblwevent/200_eventloop.c index 2931303..aeaecc6 100644 --- a/src/liblwevent/200_eventloop.c +++ b/src/liblwevent/200_eventloop.c @@ -90,7 +90,7 @@ void lwevent_exit(void) int lwevent_loop(void) { while(!lwevent_loop_exit) { - if(lwevent_wait(-1) == -1) { + if(lwevent_wait(-1, 0) == -1) { if(errno == EINTR) continue; return -1; } @@ -125,7 +125,7 @@ static void _lwevent_invalid(struct lwevent* ev) * Wrapper around epoll_wait(2). For every returned event, we assume the object has an associated * struct lwevent in the struct epoll_event. */ -int lwevent_wait(int timeout_ms) +int lwevent_wait(int timeout_ms, void (*before_loop)(void)) { int ret, i, revents; struct lwevent* ev; @@ -137,6 +137,8 @@ int lwevent_wait(int timeout_ms) ret = epoll_wait(lwevent_epoll_fd, _lw_revents, _lw_revents_size, timeout_ms); if(ret == -1) return -1; + if(before_loop) before_loop(); + /* process returned events */ for(i = 0; i < ret; ++i) { ev = (struct lwevent*)(_lw_revents[i].data.ptr); diff --git a/src/liblwevent/200_eventloop.h b/src/liblwevent/200_eventloop.h index 917ce11..95b8029 100644 --- a/src/liblwevent/200_eventloop.h +++ b/src/liblwevent/200_eventloop.h @@ -73,6 +73,8 @@ int lwevent_loop(void); \param timeout_ms A timeout, in milliseconds. This function will return as soon as any events occur, but will return after \a timeout_ms milliseconds have elapsed. +\param before_loop If specified, a function which is called after \c epoll_wait(2) but before + processing events. May be 0. \returns Number of events processed. \retval 0 on timeout. \retval -1 on error (system call failed; see \a errno). @@ -85,12 +87,16 @@ objects that are closed during the callbacks. \a timeout_ms may be specified as a negative number to disable timeouts and wait forever, or as 0 to return immediately even if no events are available. A positive number is a duration in milliseconds. +\a before_loop may be used to run a function before looping over any returned events. Some programs +may wish to record the time at the top of the event loop, for example, to avoid polling for it +multiple times while processing events. + \note If a signal is received while in \c epoll_wait(2), this function will return -1 and \a errno will be set to \c EINTR. This is not a permanent error condition. lwevent_loop() will catch this condition and continue to loop. */ -int lwevent_wait(int timeout_ms); +int lwevent_wait(int timeout_ms, void (*before_loop)(void));