robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
64a301184e
master
1#include <stdbool.h> 2#include <stdint.h> 3#include <stdlib.h> 4#include <uv.h> 5 6#include "nvim/event/loop.h" 7#include "nvim/event/multiqueue.h" 8#include "nvim/log.h" 9#include "nvim/memory.h" 10#include "nvim/os/time.h" 11#include "nvim/types_defs.h" 12 13#include "event/loop.c.generated.h" 14 15void loop_init (Loop * loop ,void * data ) 16{ 17uv_loop_init (& loop -> uv ); 18loop -> recursive = 0 ; 19loop -> closing = false; 20loop -> uv .data = loop ; 21kv_init (loop -> children ); 22loop -> events = multiqueue_new (loop_on_put ,loop ); 23loop -> fast_events = multiqueue_new_child (loop -> events ); 24loop -> thread_events = multiqueue_new (NULL ,NULL ); 25uv_mutex_init (& loop -> mutex ); 26uv_async_init (& loop -> uv ,& loop -> async ,async_cb ); 27uv_signal_init (& loop -> uv ,& loop -> children_watcher ); 28uv_timer_init (& loop -> uv ,& loop -> children_kill_timer ); 29uv_timer_init (& loop -> uv ,& loop -> poll_timer ); 30uv_timer_init (& loop -> uv ,& loop -> exit_delay_timer ); 31loop -> poll_timer .data = xmalloc (sizeof (bool ));// "timeout expired" flag 32} 33 34/// Process `Loop.uv` events with a timeout. 35/// 36/// @param loop 37/// @param ms 0: non-blocking poll. 38/// > 0: timeout after `ms`. 39/// < 0: wait forever. 40/// @return true if `ms` > 0 and was reached 41static bool loop_uv_run (Loop * loop ,int64_t ms ) 42{ 43if (loop -> recursive ++ ) { 44abort ();// Should not re-enter uv_run 45 } 46 47uv_run_mode mode = UV_RUN_ONCE ; 48bool * timeout_expired = loop -> poll_timer .data ; 49* timeout_expired = false; 50 51if (ms > 0 ) { 52// This timer ensures UV_RUN_ONCE does not block indefinitely for I/O. 53uv_timer_start (& loop -> poll_timer ,timer_cb , (uint64_t )ms , (uint64_t )ms ); 54 }else if (ms == 0 ) { 55// For ms == 0, do a non-blocking event poll. 56mode = UV_RUN_NOWAIT ; 57 } 58 59uv_run (& loop -> uv ,mode ); 60 61if (ms > 0 ) { 62uv_timer_stop (& loop -> poll_timer ); 63 } 64 65loop -> recursive -- ;// Can re-enter uv_run now 66return * timeout_expired ; 67} 68 69/// Processes one `Loop.uv` event (at most). 70/// Processes all `Loop.fast_events` events. 71/// Does NOT process `Loop.events`, that is an application-specific decision. 72/// 73/// @param loop 74/// @param ms 0: non-blocking poll. 75/// > 0: timeout after `ms`. 76/// < 0: wait forever. 77/// @return true if `ms` > 0 and was reached 78bool loop_poll_events (Loop * loop ,int64_t ms ) 79{ 80bool timeout_expired = loop_uv_run (loop ,ms ); 81multiqueue_process_events (loop -> fast_events ); 82return timeout_expired ; 83} 84 85/// Schedules a fast event from another thread. 86/// 87/// @note Event is queued into `fast_events`, which is processed outside of the 88/// primary `events` queue by loop_poll_events(). For `main_loop`, that 89/// means `fast_events` is NOT processed in an "editor mode" 90/// (VimState.execute), so redraw and other side effects are likely to be 91/// skipped. 92/// @see loop_schedule_deferred 93void loop_schedule_fast (Loop * loop ,Event event ) 94{ 95uv_mutex_lock (& loop -> mutex ); 96multiqueue_put_event (loop -> thread_events ,event ); 97uv_async_send (& loop -> async ); 98uv_mutex_unlock (& loop -> mutex ); 99} 100 101/// Schedules an event from another thread. Unlike loop_schedule_fast(), the 102/// event is forwarded to `Loop.events`, instead of being processed immediately. 103/// 104/// @see loop_schedule_fast 105void loop_schedule_deferred (Loop * loop ,Event event ) 106{ 107Event * eventp = xmalloc (sizeof (* eventp )); 108* eventp = event ; 109loop_schedule_fast (loop ,event_create (loop_deferred_event ,loop ,eventp )); 110} 111static void loop_deferred_event (void ** argv ) 112{ 113Loop * loop = argv [0 ]; 114Event * eventp = argv [1 ]; 115multiqueue_put_event (loop -> events ,* eventp ); 116xfree (eventp ); 117} 118 119void loop_on_put (MultiQueue * queue ,void * data ) 120{ 121Loop * loop = data ; 122// Sometimes libuv will run pending callbacks (timer for example) before 123// blocking for a poll. If this happens and the callback pushes a event to one 124// of the queues, the event would only be processed after the poll 125// returns (user hits a key for example). To avoid this scenario, we call 126// uv_stop when a event is enqueued. 127// Only call uv_stop() when the loop is actually running, otherwise it will 128// instead make the next uv_run() stop immediately. 129if (loop -> recursive ) { 130uv_stop (& loop -> uv ); 131 } 132} 133 134#ifndef EXITFREE 135static void loop_walk_cb (uv_handle_t * handle ,void * arg ) 136{ 137if (!uv_is_closing (handle )) { 138uv_close (handle ,NULL ); 139 } 140} 141#endif 142 143/// Closes `loop` and its handles, and frees its structures. 144/// 145/// @param loop Loop to destroy 146/// @param wait Wait briefly for handles to deref 147/// 148/// @returns false if the loop could not be closed gracefully 149bool loop_close (Loop * loop ,bool wait ) 150{ 151bool rv = true; 152loop -> closing = true; 153uv_mutex_destroy (& loop -> mutex ); 154uv_close ((uv_handle_t * )& loop -> children_watcher ,NULL ); 155uv_close ((uv_handle_t * )& loop -> children_kill_timer ,NULL ); 156uv_close ((uv_handle_t * )& loop -> poll_timer ,timer_close_cb ); 157uv_close ((uv_handle_t * )& loop -> exit_delay_timer ,NULL ); 158uv_close ((uv_handle_t * )& loop -> async ,NULL ); 159uint64_t start = wait ?os_hrtime () :0 ; 160bool didstop = false; 161while (true) { 162// Run the loop to tickle close-callbacks (which should then free memory). 163// Use UV_RUN_NOWAIT to avoid a hang. #11820 164uv_run (& loop -> uv ,didstop ?UV_RUN_DEFAULT :UV_RUN_NOWAIT ); 165if ((uv_loop_close (& loop -> uv )!= UV_EBUSY )|| !wait ) { 166break ; 167 } 168uint64_t elapsed_s = (os_hrtime ()- start ) /NS_PER_SEC ; 169if (elapsed_s >=2 ) { 170// Some libuv resource was not correctly deref'd. Log and bail. 171rv = false; 172ELOG ("uv_loop_close() hang?" ); 173log_uv_handles (& loop -> uv ); 174break ; 175 } 176#ifdef EXITFREE 177 (void )didstop ; 178#else 179if (!didstop ) { 180// Loop won’t block for I/O after this. 181uv_stop (& loop -> uv ); 182// XXX: Close all (lua/luv!) handles. But loop_walk_cb() does not call 183// resource-specific close-callbacks, so this leaks memory... 184uv_walk (& loop -> uv ,loop_walk_cb ,NULL ); 185didstop = true; 186 } 187#endif 188 } 189multiqueue_free (loop -> fast_events ); 190multiqueue_free (loop -> thread_events ); 191multiqueue_free (loop -> events ); 192kv_destroy (loop -> children ); 193return rv ; 194} 195 196void loop_purge (Loop * loop ) 197{ 198uv_mutex_lock (& loop -> mutex ); 199multiqueue_purge_events (loop -> thread_events ); 200multiqueue_purge_events (loop -> fast_events ); 201uv_mutex_unlock (& loop -> mutex ); 202} 203 204size_t loop_size (Loop * loop ) 205{ 206uv_mutex_lock (& loop -> mutex ); 207size_t rv = multiqueue_size (loop -> thread_events ); 208uv_mutex_unlock (& loop -> mutex ); 209return rv ; 210} 211 212static void async_cb (uv_async_t * handle ) 213{ 214Loop * l = handle -> loop -> data ; 215uv_mutex_lock (& l -> mutex ); 216// Flush thread_events to fast_events for processing on main loop. 217multiqueue_move_events (l -> fast_events ,l -> thread_events ); 218uv_mutex_unlock (& l -> mutex ); 219} 220 221static void timer_cb (uv_timer_t * handle ) 222{ 223bool * timeout_expired = handle -> data ; 224* timeout_expired = true; 225} 226 227static void timer_close_cb (uv_handle_t * handle ) 228{ 229xfree (handle -> data ); 230}