diff options
author | Justin M. Keyes <justinkz@gmail.com> | 2017-09-03 14:31:38 +0200 |
---|---|---|
committer | Justin M. Keyes <justinkz@gmail.com> | 2017-09-05 15:01:07 +0200 |
commit | c00a33ed19c1372bf5880e2f32adf37c9bea165b (patch) | |
tree | 95c0ae33cd746b9c7d4acd8af9b22311057c477c | |
parent | ff32bacb2e9781f5589eea1056e193897b0f964e (diff) | |
download | rneovim-c00a33ed19c1372bf5880e2f32adf37c9bea165b.tar.gz rneovim-c00a33ed19c1372bf5880e2f32adf37c9bea165b.tar.bz2 rneovim-c00a33ed19c1372bf5880e2f32adf37c9bea165b.zip |
eventloop: loop_schedule_deferred()
Generalize the "schedule schedule" technique.
-rw-r--r-- | src/nvim/aucmd.c | 9 | ||||
-rw-r--r-- | src/nvim/event/loop.c | 18 |
2 files changed, 20 insertions, 7 deletions
diff --git a/src/nvim/aucmd.c b/src/nvim/aucmd.c index a69881431e..fc421116ea 100644 --- a/src/nvim/aucmd.c +++ b/src/nvim/aucmd.c @@ -5,7 +5,6 @@ #include "nvim/fileio.h" #include "nvim/vim.h" #include "nvim/main.h" -#include "nvim/screen.h" #include "nvim/ui.h" #ifdef INCLUDE_GENERATED_DECLARATIONS @@ -18,16 +17,12 @@ static void focusgained_event(void **argv) do_autocmd_focusgained(*gainedp); xfree(gainedp); } -static void schedule_event(void **argv) -{ - bool *gainedp = argv[0]; - multiqueue_put(main_loop.events, focusgained_event, 1, gainedp); -} void aucmd_schedule_focusgained(bool gained) { bool *gainedp = xmalloc(sizeof(*gainedp)); *gainedp = gained; - loop_schedule(&main_loop, event_create(schedule_event, 1, gainedp)); + loop_schedule_deferred(&main_loop, + event_create(focusgained_event, 1, gainedp)); } static void do_autocmd_focusgained(bool gained) diff --git a/src/nvim/event/loop.c b/src/nvim/event/loop.c index cc9088f776..570db6dfc3 100644 --- a/src/nvim/event/loop.c +++ b/src/nvim/event/loop.c @@ -74,6 +74,24 @@ void loop_schedule(Loop *loop, Event event) uv_mutex_unlock(&loop->mutex); } +/// Schedules an event from another thread. Unlike loop_schedule(), the event +/// is forwarded to `Loop.events`, instead of being processed immediately. +/// +/// @see loop_schedule +void loop_schedule_deferred(Loop *loop, Event event) +{ + Event *eventp = xmalloc(sizeof(*eventp)); + *eventp = event; + loop_schedule(loop, event_create(loop_deferred_event, 2, loop, eventp)); +} +static void loop_deferred_event(void **argv) +{ + Loop *loop = argv[0]; + Event *eventp = argv[1]; + multiqueue_put_event(loop->events, *eventp); + xfree(eventp); +} + void loop_on_put(MultiQueue *queue, void *data) { Loop *loop = data; |