diff options
author | Thiago de Arruda <tpadilha84@gmail.com> | 2014-03-24 19:11:51 -0300 |
---|---|---|
committer | Thiago de Arruda <tpadilha84@gmail.com> | 2014-03-24 19:25:07 -0300 |
commit | 32f118a47f108e4dc26fbea6afce9648ba610079 (patch) | |
tree | d0be52bddc14a617daf9313f6767093ac7a58483 /src/os/time.c | |
parent | ed42c808b61dd745e76d19c6ffb2612496a779a1 (diff) | |
download | rneovim-32f118a47f108e4dc26fbea6afce9648ba610079.tar.gz rneovim-32f118a47f108e4dc26fbea6afce9648ba610079.tar.bz2 rneovim-32f118a47f108e4dc26fbea6afce9648ba610079.zip |
Implement `mch_delay` on top of libuv
Needed to temporarily move two static variables from os_unix.c to 'globals.h'
as those are shared by other functions still in os_unix.
Diffstat (limited to 'src/os/time.c')
-rw-r--r-- | src/os/time.c | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/src/os/time.c b/src/os/time.c new file mode 100644 index 0000000000..bb86806af3 --- /dev/null +++ b/src/os/time.c @@ -0,0 +1,58 @@ +#include <stdint.h> +#include <stdbool.h> + +#include <uv.h> + +#include "vim.h" +#include "term.h" + +static uv_mutex_t delay_mutex; +static uv_cond_t delay_cond; + +static void delay(uint64_t ms); + +void time_init() +{ + uv_mutex_init(&delay_mutex); + uv_cond_init(&delay_cond); +} + +void mch_delay(uint64_t ms, bool ignoreinput) +{ + int old_tmode; + + if (ignoreinput) { + /* Go to cooked mode without echo, to allow SIGINT interrupting us + * here. But we don't want QUIT to kill us (CTRL-\ used in a + * shell may produce SIGQUIT). */ + in_mch_delay = true; + old_tmode = curr_tmode; + + if (curr_tmode == TMODE_RAW) + settmode(TMODE_SLEEP); + + delay(ms); + + settmode(old_tmode); + in_mch_delay = false; + } else { + delay(ms); + } +} + +static void delay(uint64_t ms) +{ + uint64_t hrtime; + int64_t ns = ms * 1000000; /* convert to nanoseconds */ + + uv_mutex_lock(&delay_mutex); + + while (ns > 0) { + hrtime = uv_hrtime(); + if (uv_cond_timedwait(&delay_cond, &delay_mutex, ns) == UV_ETIMEDOUT) + break; + ns -= uv_hrtime() - hrtime; + } + + uv_mutex_unlock(&delay_mutex); +} |