robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
38986188ba
master
1#pragma once 2 3#include <stddef.h> // IWYU pragma: keep 4 5#include "nvim/event/defs.h" // IWYU pragma: keep 6#include "nvim/os/time.h" // IWYU pragma: keep 7 8#include "event/multiqueue.h.generated.h" 9 10#define multiqueue_put (q ,h , ...) \ 11 do { \ 12 multiqueue_put_event(q, event_create(h, __VA_ARGS__)); \ 13 } while (0) 14 15#define CREATE_EVENT (multiqueue ,handler , ...) \ 16 do { \ 17 if (multiqueue) { \ 18 multiqueue_put((multiqueue), (handler), __VA_ARGS__); \ 19 } else { \ 20 void *argv[] = { __VA_ARGS__ }; \ 21 (handler)(argv); \ 22 } \ 23 } while (0) 24 25// Poll for events until a condition or timeout 26#define LOOP_PROCESS_EVENTS_UNTIL (loop ,multiqueue ,timeout ,condition ) \ 27 do { \ 28 int64_t remaining = timeout; \ 29 uint64_t before = (remaining > 0) ? os_hrtime() : 0; \ 30 while (!(condition)) { \ 31 LOOP_PROCESS_EVENTS(loop, multiqueue, remaining); \ 32 if (remaining == 0) { \ 33 break; \ 34 } else if (remaining > 0) { \ 35 uint64_t now = os_hrtime(); \ 36 remaining -= (int64_t)((now - before) / 1000000); \ 37 before = now; \ 38 if (remaining <= 0) { \ 39 break; \ 40 } \ 41 } \ 42 } \ 43 } while (0) 44 45#define LOOP_PROCESS_EVENTS (loop ,multiqueue ,timeout ) \ 46 do { \ 47 if (multiqueue && !multiqueue_empty(multiqueue)) { \ 48 multiqueue_process_events(multiqueue); \ 49 } else { \ 50 loop_poll_events(loop, timeout); \ 51 } \ 52 } while (0)