1 | // Bridge for communication between a UI thread and nvim core. |
2 | // Used by the built-in TUI and libnvim-based UIs. |
3 | #ifndef NVIM_UI_BRIDGE_H |
4 | #define NVIM_UI_BRIDGE_H |
5 | |
6 | #include <uv.h> |
7 | |
8 | #include "nvim/ui.h" |
9 | #include "nvim/event/defs.h" |
10 | |
11 | typedef struct ui_bridge_data UIBridgeData; |
12 | typedef void(*ui_main_fn)(UIBridgeData *bridge, UI *ui); |
13 | struct ui_bridge_data { |
14 | UI bridge; // actual UI passed to ui_attach |
15 | UI *ui; // UI pointer that will have its callback called in |
16 | // another thread |
17 | event_scheduler scheduler; |
18 | uv_thread_t ui_thread; |
19 | ui_main_fn ui_main; |
20 | uv_mutex_t mutex; |
21 | uv_cond_t cond; |
22 | // When the UI thread is called, the main thread will suspend until |
23 | // the call returns. This flag is used as a condition for the main |
24 | // thread to continue. |
25 | bool ready; |
26 | // When a stop request is sent from the main thread, it must wait until the UI |
27 | // thread finishes handling all events. This flag is set by the UI thread as a |
28 | // signal that it will no longer send messages to the main thread. |
29 | bool stopped; |
30 | }; |
31 | |
32 | #define CONTINUE(b) \ |
33 | do { \ |
34 | UIBridgeData *d = (UIBridgeData *)b; \ |
35 | uv_mutex_lock(&d->mutex); \ |
36 | d->ready = true; \ |
37 | uv_cond_signal(&d->cond); \ |
38 | uv_mutex_unlock(&d->mutex); \ |
39 | } while (0) |
40 | |
41 | |
42 | #ifdef INCLUDE_GENERATED_DECLARATIONS |
43 | # include "ui_bridge.h.generated.h" |
44 | #endif |
45 | #endif // NVIM_UI_BRIDGE_H |
46 | |