diff options
author | Thiago de Arruda <tpadilha84@gmail.com> | 2014-10-20 10:39:54 -0300 |
---|---|---|
committer | Thiago de Arruda <tpadilha84@gmail.com> | 2014-10-21 11:05:49 -0300 |
commit | b527ac752fd5ebcc74c06306e7009e2b98e4ee01 (patch) | |
tree | 0785b3f76cb371967ad0b28446611d1a1af60a96 /src/nvim/os/event.h | |
parent | 264e0d872c598062be2b2a118d38c89a6ed5a023 (diff) | |
download | rneovim-b527ac752fd5ebcc74c06306e7009e2b98e4ee01.tar.gz rneovim-b527ac752fd5ebcc74c06306e7009e2b98e4ee01.tar.bz2 rneovim-b527ac752fd5ebcc74c06306e7009e2b98e4ee01.zip |
event: Extract event_poll loops to `event_poll_until` macro
A pattern that is becoming common across the project is to poll for events until
a certain condition is true, optionally passing a timeout. To address this
scenario, the event_poll_until macro was created and the job/channel/input
modules were refactored to use it on their blocking functions.
Diffstat (limited to 'src/nvim/os/event.h')
-rw-r--r-- | src/nvim/os/event.h | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/src/nvim/os/event.h b/src/nvim/os/event.h index 29e304adc8..f8139e978d 100644 --- a/src/nvim/os/event.h +++ b/src/nvim/os/event.h @@ -6,6 +6,27 @@ #include "nvim/os/event_defs.h" #include "nvim/os/job_defs.h" +#include "nvim/os/time.h" + +// Poll for events until a condition is true or a timeout has passed +#define event_poll_until(timeout, condition) \ + do { \ + int remaining = timeout; \ + uint64_t before = (remaining > 0) ? os_hrtime() : 0; \ + while (!(condition)) { \ + event_poll(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 "os/event.h.generated.h" |