Notify event base if there are no more events, so it can exit without delay

Fixes: #623
(cherry picked from commit 23c2914f6b430f2c2d74c267c13ffab3dda1b325)
This commit is contained in:
Azat Khuzhin 2018-04-24 00:59:11 +03:00 committed by Azat Khuzhin
parent 44fa5b19f7
commit d9d1c09e25
No known key found for this signature in database
GPG Key ID: B86086848EF8686D
2 changed files with 33 additions and 0 deletions

View File

@ -2847,6 +2847,10 @@ event_del_nolock_(struct event *ev, int blocking)
notify = 1;
res = 0;
}
/* If we do not have events, let's notify event base so it can
* exit without waiting */
if (!event_haveevents(base) && !N_ACTIVE_CALLBACKS(base))
notify = 1;
}
/* if we are not in the right thread, we need to wake up the loop */

View File

@ -1039,6 +1039,34 @@ test_del_wait(void)
end:
;
}
static void null_cb(evutil_socket_t fd, short what, void *arg) {}
static void* test_del_notify_thread(void *arg)
{
event_dispatch();
return NULL;
}
static void
test_del_notify(void)
{
struct event ev;
pthread_t thread;
test_ok = 1;
event_set(&ev, -1, EV_READ, null_cb, &ev);
event_add(&ev, NULL);
pthread_create(&thread, NULL, test_del_notify_thread, NULL);
{
struct timeval delay = { 0, 1000 };
evutil_usleep_(&delay);
}
event_del(&ev);
pthread_join(thread, NULL);
}
#endif
static void
@ -3452,6 +3480,7 @@ struct testcase_t main_testcases[] = {
#ifdef EVENT__HAVE_PTHREADS
/** TODO: support win32 */
LEGACY(del_wait, TT_ISOLATED|TT_NEED_THREADS),
LEGACY(del_notify, TT_ISOLATED|TT_NEED_THREADS),
#endif
END_OF_TESTCASES