From 1e77e6e1b28e8c7f842f6a24e05d44437ceb1474 Mon Sep 17 00:00:00 2001 From: Maria Matejka Date: Thu, 27 Jun 2024 08:37:05 +0200 Subject: [PATCH] Flush deferred calls directly after the loop finished its one run --- lib/defer.h | 2 +- lib/io-loop.h | 7 +++++-- sysdep/unix/io-loop.c | 8 ++++++-- sysdep/unix/io-loop.h | 1 + 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/lib/defer.h b/lib/defer.h index b53d1efa..69128ef6 100644 --- a/lib/defer.h +++ b/lib/defer.h @@ -39,7 +39,7 @@ static inline void defer_call(struct deferred_call *call, size_t actual_size) { /* If first, send the actual event to the local thread */ if (local_deferred.last == &local_deferred.first) - ev_send_this_thread(&local_deferred.e); + ev_send_defer(&local_deferred.e); /* Add to list */ a->next = NULL; diff --git a/lib/io-loop.h b/lib/io-loop.h index b893aa45..9f08420a 100644 --- a/lib/io-loop.h +++ b/lib/io-loop.h @@ -19,6 +19,9 @@ extern struct birdloop main_birdloop; /* Currently running birdloop */ extern _Thread_local struct birdloop *this_birdloop; +/* Lowest entered birdloop */ +extern _Thread_local struct birdloop *birdloop_current; + /* Check that the task has enough time to do a bit more */ bool task_still_in_limit(void); bool task_before_halftime(void); @@ -40,8 +43,8 @@ void birdloop_stop(struct birdloop *loop, void (*stopped)(void *data), void *dat void birdloop_stop_self(struct birdloop *loop, void (*stopped)(void *data), void *data); void birdloop_free(struct birdloop *loop); -/* Run this event in this thread's priority event list */ -void ev_send_this_thread(event *e); +/* Run this event in the running loop's priority event list to run asap */ +void ev_send_defer(event *e); /* Get birdloop's time heap */ struct timeloop *birdloop_time_loop(struct birdloop *loop); diff --git a/sysdep/unix/io-loop.c b/sysdep/unix/io-loop.c index c332b5df..d2a60562 100644 --- a/sysdep/unix/io-loop.c +++ b/sysdep/unix/io-loop.c @@ -1543,6 +1543,10 @@ birdloop_run(void *_loop) /* Process socket RX */ sockets_fire(loop, 1, 0); + /* Flush deferred events */ + while (ev_run_list(&loop->defer_list)) + repeat++; + /* Check end time */ } while (repeat && task_still_in_limit()); @@ -1750,10 +1754,10 @@ birdloop_yield(void) } void -ev_send_this_thread(event *e) +ev_send_defer(event *e) { if (this_thread == &main_thread) ev_send_loop(&main_birdloop, e); else - ev_send(&this_thread->priority_events, e); + ev_send(&this_birdloop->defer_list, e); } diff --git a/sysdep/unix/io-loop.h b/sysdep/unix/io-loop.h index 9c7c3718..f7fde411 100644 --- a/sysdep/unix/io-loop.h +++ b/sysdep/unix/io-loop.h @@ -48,6 +48,7 @@ struct birdloop struct timeloop time; event_list event_list; + event_list defer_list; list sock_list; struct birdsock *sock_active; int sock_num;