aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/event/loop.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/nvim/event/loop.h')
-rw-r--r--src/nvim/event/loop.h55
1 files changed, 55 insertions, 0 deletions
diff --git a/src/nvim/event/loop.h b/src/nvim/event/loop.h
new file mode 100644
index 0000000000..e5a890dddb
--- /dev/null
+++ b/src/nvim/event/loop.h
@@ -0,0 +1,55 @@
+#ifndef NVIM_EVENT_LOOP_H
+#define NVIM_EVENT_LOOP_H
+
+#include <stdint.h>
+
+#include <uv.h>
+
+#include "nvim/lib/klist.h"
+#include "nvim/os/time.h"
+
+typedef struct event Event;
+typedef void (*event_handler)(Event event);
+
+struct event {
+ void *data;
+ event_handler handler;
+};
+
+typedef void * WatcherPtr;
+
+#define _noop(x)
+KLIST_INIT(WatcherPtr, WatcherPtr, _noop)
+KLIST_INIT(Event, Event, _noop)
+
+typedef struct loop {
+ uv_loop_t uv;
+ klist_t(Event) *deferred_events, *immediate_events;
+ int deferred_events_allowed;
+} Loop;
+
+// Poll for events until a condition or timeout
+#define LOOP_POLL_EVENTS_UNTIL(loop, timeout, condition) \
+ do { \
+ int remaining = timeout; \
+ uint64_t before = (remaining > 0) ? os_hrtime() : 0; \
+ while (!(condition)) { \
+ loop_poll_events(loop, remaining); \
+ if (remaining == 0) { \
+ break; \
+ } else if (remaining > 0) { \
+ uint64_t now = os_hrtime(); \
+ remaining -= (int) ((now - before) / 1000000); \
+ before = now; \
+ if (remaining <= 0) { \
+ break; \
+ } \
+ } \
+ } \
+ } while (0)
+
+#ifdef INCLUDE_GENERATED_DECLARATIONS
+# include "event/loop.h.generated.h"
+#endif
+
+#endif // NVIM_EVENT_LOOP_H