diff options
author | Eliseo Martínez <eliseomarmol@gmail.com> | 2014-05-12 02:25:17 +0200 |
---|---|---|
committer | Eliseo Martínez <eliseomarmol@gmail.com> | 2014-05-15 20:46:01 +0200 |
commit | da51dc9cf202772f60bd2da975dbef257bd9237c (patch) | |
tree | 5c16b93238a153f55634e9323077f30c8133970c /src/nvim/os/time.c | |
parent | ffe61e5ba1721340ca51d56bae3ddaca415fb5bc (diff) | |
download | rneovim-da51dc9cf202772f60bd2da975dbef257bd9237c.tar.gz rneovim-da51dc9cf202772f60bd2da975dbef257bd9237c.tar.bz2 rneovim-da51dc9cf202772f60bd2da975dbef257bd9237c.zip |
Introduce nvim namespace: Move files.
Move files from src/ to src/nvim/.
- src/nvim/ becomes the new root dir for nvim executable sources.
- src/libnvim/ is planned to become root dir of the neovim library.
Diffstat (limited to 'src/nvim/os/time.c')
-rw-r--r-- | src/nvim/os/time.c | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/src/nvim/os/time.c b/src/nvim/os/time.c new file mode 100644 index 0000000000..1dc7ca68d4 --- /dev/null +++ b/src/nvim/os/time.c @@ -0,0 +1,86 @@ +#include <stdint.h> +#include <stdbool.h> +#include <sys/time.h> + +#include <uv.h> + +#include "os/time.h" +#include "vim.h" +#include "term.h" + +static uv_mutex_t delay_mutex; +static uv_cond_t delay_cond; + +static void microdelay(uint64_t ms); + +void time_init() +{ + uv_mutex_init(&delay_mutex); + uv_cond_init(&delay_cond); +} + +void os_delay(uint64_t milliseconds, bool ignoreinput) +{ + os_microdelay(milliseconds * 1000, ignoreinput); +} + +void os_microdelay(uint64_t microseconds, bool ignoreinput) +{ + int old_tmode; + + if (ignoreinput) { + // Go to cooked mode without echo, to allow SIGINT interrupting us + // here + old_tmode = curr_tmode; + + if (curr_tmode == TMODE_RAW) + settmode(TMODE_SLEEP); + + microdelay(microseconds); + + settmode(old_tmode); + } else { + microdelay(microseconds); + } +} + +static void microdelay(uint64_t microseconds) +{ + uint64_t hrtime; + int64_t ns = microseconds * 1000; // 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); +} + +struct tm *os_localtime_r(const time_t *clock, struct tm *result) +{ +#ifdef UNIX + // POSIX provides localtime_r() as a thread-safe version of localtime(). + return localtime_r(clock, result); +#else + // Windows version of localtime() is thread-safe. + // See http://msdn.microsoft.com/en-us/library/bf12f0hc%28VS.80%29.aspx + struct tm *local_time = localtime(clock); // NOLINT + *result = *local_time; +return result; +#endif +} + +struct tm *os_get_localtime(struct tm *result) +{ + struct timeval tv; + if (gettimeofday(&tv, NULL) < 0) { + return NULL; + } + + return os_localtime_r(&tv.tv_sec, result); +} |