robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
38986188ba
master
1#include <stdbool.h> 2#include <stdint.h> 3#include <uv.h> 4 5#include "nvim/event/defs.h" 6#include "nvim/event/loop.h" 7#include "nvim/event/multiqueue.h" 8#include "nvim/event/time.h" 9#include "nvim/types_defs.h" 10 11#include "event/time.c.generated.h" 12 13void time_watcher_init (Loop * loop ,TimeWatcher * watcher ,void * data ) 14FUNC_ATTR_NONNULL_ARG (1 )FUNC_ATTR_NONNULL_ARG (2 ) 15{ 16uv_timer_init (& loop -> uv ,& watcher -> uv ); 17watcher -> uv .data = watcher ; 18watcher -> data = data ; 19watcher -> events = loop -> fast_events ; 20watcher -> blockable = false; 21} 22 23void time_watcher_start (TimeWatcher * watcher ,time_cb cb ,uint64_t timeout ,uint64_t repeat ) 24FUNC_ATTR_NONNULL_ALL 25{ 26watcher -> cb = cb ; 27uv_timer_start (& watcher -> uv ,time_watcher_cb ,timeout ,repeat ); 28} 29 30void time_watcher_stop (TimeWatcher * watcher ) 31FUNC_ATTR_NONNULL_ALL 32{ 33uv_timer_stop (& watcher -> uv ); 34} 35 36void time_watcher_close (TimeWatcher * watcher ,time_cb cb ) 37FUNC_ATTR_NONNULL_ARG (1 ) 38{ 39watcher -> close_cb = cb ; 40uv_close ((uv_handle_t * )& watcher -> uv ,close_cb ); 41} 42 43static void time_event (void ** argv ) 44{ 45TimeWatcher * watcher = argv [0 ]; 46watcher -> cb (watcher ,watcher -> data ); 47} 48 49static void time_watcher_cb (uv_timer_t * handle ) 50FUNC_ATTR_NONNULL_ALL 51{ 52TimeWatcher * watcher = handle -> data ; 53if (watcher -> blockable && !multiqueue_empty (watcher -> events )) { 54// the timer blocked and there already is an unprocessed event waiting 55return ; 56 } 57CREATE_EVENT (watcher -> events ,time_event ,watcher ); 58} 59 60static void close_event (void ** argv ) 61{ 62TimeWatcher * watcher = argv [0 ]; 63watcher -> close_cb (watcher ,watcher -> data ); 64} 65 66static void close_cb (uv_handle_t * handle ) 67FUNC_ATTR_NONNULL_ALL 68{ 69TimeWatcher * watcher = handle -> data ; 70if (watcher -> close_cb ) { 71CREATE_EVENT (watcher -> events ,close_event ,watcher ); 72 } 73}