mirror of
https://github.com/troglobit/libuev.git
synced 2026-11-17 19:31:18 +00:00
Return from uev_run() when UEV_NONBLOCK and no event is pending
uev_run() with only UEV_NONBLOCK never returns when there are watchers
but no events, it spins at full CPU load:
uev_run(ctx, UEV_NONBLOCK);
The loop polls epoll with a zero timeout and only stops on UEV_ONCE.
Leave the loop when a nonblocking poll finds no events.
This commit is contained in:
1 parent
09d9b6ba04
commit
28643fa599
5 files changed
+45
-5
No files matched your search
+2
-2
@@ -168,8 +168,8 @@ result = uev_run(&ctx, UEV_NONE);
|
|||||||
```
|
```
|
||||||
|
|
||||||
With `flags` set to `UEV_ONCE` the event loop returns as soon as it has
|
With `flags` set to `UEV_ONCE` the event loop returns as soon as it has
|
||||||
served the first event. If `flags` is set to `UEV_ONCE | UEV_NONBLOCK`
|
served the first event. With `UEV_NONBLOCK` the event loop returns as
|
||||||
the event loop returns immediately if no event is available.
|
soon as no event is available, also when combined with `UEV_ONCE`.
|
||||||
|
|
||||||
Timers are started by the first call to `uev_run()` and keep their
|
Timers are started by the first call to `uev_run()` and keep their
|
||||||
deadline between calls. Use `uev_timer_set()` to restart a timer.
|
deadline between calls. Use `uev_timer_set()` to restart a timer.
|
||||||
|
|||||||
@@ -290,9 +290,9 @@ int uev_exit(uev_ctx_t *ctx)
|
|||||||
*
|
*
|
||||||
* With @p flags set to ::UEV_ONCE the event loop returns after the first
|
* With @p flags set to ::UEV_ONCE the event loop returns after the first
|
||||||
* event has been served, useful for instance to set a timeout on a file
|
* event has been served, useful for instance to set a timeout on a file
|
||||||
* descriptor. If @p flags also has the ::UEV_NONBLOCK flag set the event
|
* descriptor. With the ::UEV_NONBLOCK flag set the event loop returns
|
||||||
* loop will return immediately if no event is pending, useful when run
|
* as soon as no event is pending, useful when run inside another event
|
||||||
* inside another event loop.
|
* loop.
|
||||||
*
|
*
|
||||||
* Timers are started by the first call and keep their deadline between
|
* Timers are started by the first call and keep their deadline between
|
||||||
* calls. Use uev_timer_set() to restart a timer.
|
* calls. Use uev_timer_set() to restart a timer.
|
||||||
@@ -369,6 +369,9 @@ int uev_run(uev_ctx_t *ctx, int flags)
|
|||||||
return -2;
|
return -2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!nfds && (flags & UEV_NONBLOCK))
|
||||||
|
break;
|
||||||
|
|
||||||
for (i = 0; ctx->running && i < nfds; i++) {
|
for (i = 0; ctx->running && i < nfds; i++) {
|
||||||
struct signalfd_siginfo fdsi;
|
struct signalfd_siginfo fdsi;
|
||||||
ssize_t sz = sizeof(fdsi);
|
ssize_t sz = sizeof(fdsi);
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ cronrun
|
|||||||
cronstart
|
cronstart
|
||||||
error
|
error
|
||||||
io
|
io
|
||||||
|
nonblock
|
||||||
resume
|
resume
|
||||||
signal
|
signal
|
||||||
stdin
|
stdin
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ TESTS += cronrun
|
|||||||
TESTS += cronstart
|
TESTS += cronstart
|
||||||
TESTS += error
|
TESTS += error
|
||||||
TESTS += io
|
TESTS += io
|
||||||
|
TESTS += nonblock
|
||||||
TESTS += resume
|
TESTS += resume
|
||||||
TESTS += signal
|
TESTS += signal
|
||||||
TESTS += stdin
|
TESTS += stdin
|
||||||
|
|||||||
@@ -0,0 +1,35 @@
|
|||||||
|
/* Verifies that UEV_NONBLOCK returns when no event is pending */
|
||||||
|
#include "check.h"
|
||||||
|
|
||||||
|
static int fired;
|
||||||
|
|
||||||
|
static void cb(uev_t *w, void *arg, int events)
|
||||||
|
{
|
||||||
|
fired++;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
uev_ctx_t ctx;
|
||||||
|
uev_t w;
|
||||||
|
|
||||||
|
fail_unless(uev_init(&ctx) == 0);
|
||||||
|
fail_unless(uev_event_init(&ctx, &w, cb, NULL) == 0);
|
||||||
|
|
||||||
|
alarm(2);
|
||||||
|
fail_unless(uev_run(&ctx, UEV_NONBLOCK) == 0);
|
||||||
|
fail_unless(fired == 0);
|
||||||
|
|
||||||
|
fail_unless(uev_event_post(&w) == 0);
|
||||||
|
fail_unless(uev_run(&ctx, UEV_NONBLOCK) == 0);
|
||||||
|
fail_unless(fired == 1);
|
||||||
|
|
||||||
|
return uev_exit(&ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Local Variables:
|
||||||
|
* indent-tabs-mode: t
|
||||||
|
* c-file-style: "linux"
|
||||||
|
* End:
|
||||||
|
*/
|
||||||
Reference in new issue
Block a user