time.c (1777B)
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 13 void time_watcher_init(Loop *loop, TimeWatcher *watcher, void *data) 14 FUNC_ATTR_NONNULL_ARG(1) FUNC_ATTR_NONNULL_ARG(2) 15 { 16 uv_timer_init(&loop->uv, &watcher->uv); 17 watcher->uv.data = watcher; 18 watcher->data = data; 19 watcher->events = loop->fast_events; 20 watcher->blockable = false; 21 } 22 23 void time_watcher_start(TimeWatcher *watcher, time_cb cb, uint64_t timeout, uint64_t repeat) 24 FUNC_ATTR_NONNULL_ALL 25 { 26 watcher->cb = cb; 27 uv_timer_start(&watcher->uv, time_watcher_cb, timeout, repeat); 28 } 29 30 void time_watcher_stop(TimeWatcher *watcher) 31 FUNC_ATTR_NONNULL_ALL 32 { 33 uv_timer_stop(&watcher->uv); 34 } 35 36 void time_watcher_close(TimeWatcher *watcher, time_cb cb) 37 FUNC_ATTR_NONNULL_ARG(1) 38 { 39 watcher->close_cb = cb; 40 uv_close((uv_handle_t *)&watcher->uv, close_cb); 41 } 42 43 static void time_event(void **argv) 44 { 45 TimeWatcher *watcher = argv[0]; 46 watcher->cb(watcher, watcher->data); 47 } 48 49 static void time_watcher_cb(uv_timer_t *handle) 50 FUNC_ATTR_NONNULL_ALL 51 { 52 TimeWatcher *watcher = handle->data; 53 if (watcher->blockable && !multiqueue_empty(watcher->events)) { 54 // the timer blocked and there already is an unprocessed event waiting 55 return; 56 } 57 CREATE_EVENT(watcher->events, time_event, watcher); 58 } 59 60 static void close_event(void **argv) 61 { 62 TimeWatcher *watcher = argv[0]; 63 watcher->close_cb(watcher, watcher->data); 64 } 65 66 static void close_cb(uv_handle_t *handle) 67 FUNC_ATTR_NONNULL_ALL 68 { 69 TimeWatcher *watcher = handle->data; 70 if (watcher->close_cb) { 71 CREATE_EVENT(watcher->events, close_event, watcher); 72 } 73 }