Merge pull request #21596 from bfredl/nasleep

refactor(sleep): simplify rube goldberg implementation of :sleep
This commit is contained in:
bfredl 2022-12-30 20:39:42 +01:00 committed by GitHub
commit 6a45360de9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 7 additions and 11 deletions

View File

@ -43,7 +43,7 @@ void loop_init(Loop *loop, void *data)
/// @param once true: process at most one `Loop.uv` event.
/// false: process until `ms` timeout (only has effect if `ms` > 0).
/// @return true if `ms` > 0 and was reached
bool loop_uv_run(Loop *loop, int ms, bool once)
bool loop_uv_run(Loop *loop, int64_t ms, bool once)
{
if (loop->recursive++) {
abort(); // Should not re-enter uv_run
@ -82,7 +82,7 @@ bool loop_uv_run(Loop *loop, int ms, bool once)
/// > 0: timeout after `ms`.
/// < 0: wait forever.
/// @return true if `ms` > 0 and was reached
bool loop_poll_events(Loop *loop, int ms)
bool loop_poll_events(Loop *loop, int64_t ms)
{
bool timeout_expired = loop_uv_run(loop, ms, true);
multiqueue_process_events(loop->fast_events);

View File

@ -58,7 +58,7 @@ typedef struct loop {
// Poll for events until a condition or timeout
#define LOOP_PROCESS_EVENTS_UNTIL(loop, multiqueue, timeout, condition) \
do { \
int remaining = timeout; \
int64_t remaining = timeout; \
uint64_t before = (remaining > 0) ? os_hrtime() : 0; \
while (!(condition)) { \
LOOP_PROCESS_EVENTS(loop, multiqueue, remaining); \
@ -66,7 +66,7 @@ typedef struct loop {
break; \
} else if (remaining > 0) { \
uint64_t now = os_hrtime(); \
remaining -= (int)((now - before) / 1000000); \
remaining -= (int64_t)((now - before) / 1000000); \
before = now; \
if (remaining <= 0) { \
break; \

View File

@ -5658,15 +5658,11 @@ static void ex_sleep(exarg_T *eap)
do_sleep(len);
}
/// Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
/// Sleep for "msec" milliseconds, but return early on CTRL-C.
void do_sleep(long msec)
{
ui_flush(); // flush before waiting
for (long left = msec; !got_int && left > 0; left -= 1000L) {
int next = left > 1000L ? 1000 : (int)left;
LOOP_PROCESS_EVENTS_UNTIL(&main_loop, main_loop.events, (int)next, got_int);
os_breakcheck();
}
LOOP_PROCESS_EVENTS_UNTIL(&main_loop, main_loop.events, msec, got_int);
// If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
// input buffer, otherwise a following call to input() fails.

View File

@ -1747,7 +1747,7 @@ static void pad(void *ctx, size_t delay, int scale FUNC_ATTR_UNUSED, int force)
}
flush_buf(ui);
loop_uv_run(data->loop, (int)(delay / 10), false);
loop_uv_run(data->loop, (int64_t)(delay / 10), false);
}
static void unibi_set_if_empty(unibi_term *ut, enum unibi_string str, const char *val)