]> granicus.if.org Git - libevent/commitdiff
Notify event base if there are no more events, so it can exit without delay
authorAzat Khuzhin <a3at.mail@gmail.com>
Mon, 23 Apr 2018 21:59:11 +0000 (00:59 +0300)
committerAzat Khuzhin <a3at.mail@gmail.com>
Wed, 25 Apr 2018 22:14:49 +0000 (01:14 +0300)
Fixes: #623
event.c
test/regress.c

diff --git a/event.c b/event.c
index c55607076c07a4cc1be0697c255bf1f9600a1c29..caf766b5b1920007f5c289bc784c8dd6aeb63be8 100644 (file)
--- a/event.c
+++ b/event.c
@@ -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 */
index 88e9b25990ff9ca5a97cd9ffa26f38548532a797..6daa17ede42be4c1d958bb612a46fcdc512ec869 100644 (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