unix,win: add uv_timer_get_due_in()

Co-authored-by: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Refs: https://github.com/nodejs/node-report/pull/73
Refs: https://github.com/libuv/libuv/pull/1255
Fixes: https://github.com/libuv/libuv/issues/2950
PR-URL: https://github.com/libuv/libuv/pull/2951
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
This commit is contained in:
Ulrik Strid 2017-03-16 12:06:49 -04:00 committed by cjihrig
parent 3ee60fa72a
commit 2a1b880f54
No known key found for this signature in database
GPG Key ID: 7434390BDBE9B9C5
4 changed files with 20 additions and 0 deletions

View File

@ -78,4 +78,11 @@ API
Get the timer repeat value.
.. c:function:: uint64_t uv_timer_get_due_in(const uv_timer_t* handle)
Get the timer due value or 0 if it has expired. The time is relative to
:c:func:`uv_now()`.
.. versionadded:: 1.40.0
.. seealso:: The :c:type:`uv_handle_t` API functions also apply.

View File

@ -871,6 +871,7 @@ UV_EXTERN int uv_timer_stop(uv_timer_t* handle);
UV_EXTERN int uv_timer_again(uv_timer_t* handle);
UV_EXTERN void uv_timer_set_repeat(uv_timer_t* handle, uint64_t repeat);
UV_EXTERN uint64_t uv_timer_get_repeat(const uv_timer_t* handle);
UV_EXTERN uint64_t uv_timer_get_due_in(const uv_timer_t* handle);
/*

View File

@ -130,6 +130,14 @@ uint64_t uv_timer_get_repeat(const uv_timer_t* handle) {
}
uint64_t uv_timer_get_due_in(const uv_timer_t* handle) {
if (handle->loop->time >= handle->timeout)
return 0;
return handle->timeout - handle->loop->time;
}
int uv__next_timeout(const uv_loop_t* loop) {
const struct heap_node* heap_node;
const uv_timer_t* handle;

View File

@ -161,6 +161,7 @@ TEST_IMPL(timer_init) {
ASSERT(0 == uv_timer_init(uv_default_loop(), &handle));
ASSERT(0 == uv_timer_get_repeat(&handle));
ASSERT_UINT64_LE(0, uv_timer_get_due_in(&handle));
ASSERT(0 == uv_is_active((uv_handle_t*) &handle));
MAKE_VALGRIND_HAPPY();
@ -232,6 +233,9 @@ TEST_IMPL(timer_huge_timeout) {
ASSERT(0 == uv_timer_start(&tiny_timer, tiny_timer_cb, 1, 0));
ASSERT(0 == uv_timer_start(&huge_timer1, tiny_timer_cb, 0xffffffffffffLL, 0));
ASSERT(0 == uv_timer_start(&huge_timer2, tiny_timer_cb, (uint64_t) -1, 0));
ASSERT_UINT64_EQ(1, uv_timer_get_due_in(&tiny_timer));
ASSERT_UINT64_EQ(281474976710655, uv_timer_get_due_in(&huge_timer1));
ASSERT_UINT64_LE(0, uv_timer_get_due_in(&huge_timer2));
ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT));
MAKE_VALGRIND_HAPPY();
return 0;