ext/noderb_extension/libuv/src/win/timer.c in noderb-0.0.4 vs ext/noderb_extension/libuv/src/win/timer.c in noderb-0.0.6

- old
+ new

@@ -34,16 +34,16 @@ static int64_t uv_ticks_per_msec_ = 0; static uint64_t uv_hrtime_frequency_ = 0; static char uv_hrtime_initialized_ = 0; -void uv_update_time() { +void uv_update_time(uv_loop_t* loop) { DWORD ticks = GetTickCount(); /* The assumption is made that LARGE_INTEGER.QuadPart has the same type */ - /* LOOP->time, which happens to be. Is there any way to assert this? */ - LARGE_INTEGER* time = (LARGE_INTEGER*) &LOOP->time; + /* loop->time, which happens to be. Is there any way to assert this? */ + LARGE_INTEGER* time = (LARGE_INTEGER*) &loop->time; /* If the timer has wrapped, add 1 to it's high-order dword. */ /* uv_poll must make sure that the timer can never overflow more than */ /* once between two subsequent uv_update_time calls. */ if (ticks < time->LowPart) { @@ -51,40 +51,40 @@ } time->LowPart = ticks; } -int64_t uv_now() { - return LOOP->time; +int64_t uv_now(uv_loop_t* loop) { + return loop->time; } - +/* TODO: thread safety */ uint64_t uv_hrtime(void) { LARGE_INTEGER counter; /* When called for the first time, obtain the high-resolution clock */ /* frequency. */ if (!uv_hrtime_initialized_) { uv_hrtime_initialized_ = 1; if (!QueryPerformanceFrequency(&counter)) { uv_hrtime_frequency_ = 0; - uv_set_sys_error(GetLastError()); + /* uv_set_sys_error(loop, GetLastError()); */ return 0; } uv_hrtime_frequency_ = counter.QuadPart; } /* If the performance frequency is zero, there's no support. */ if (!uv_hrtime_frequency_) { - uv_set_sys_error(ERROR_NOT_SUPPORTED); + /* uv_set_sys_error(loop, ERROR_NOT_SUPPORTED); */ return 0; } if (!QueryPerformanceCounter(&counter)) { - uv_set_sys_error(GetLastError()); + /* uv_set_sys_error(loop, GetLastError()); */ return 0; } /* Because we have no guarantee about the order of magniture of the */ /* performance counter frequency, and there may not be much headroom to */ @@ -109,86 +109,93 @@ RB_GENERATE_STATIC(uv_timer_tree_s, uv_timer_s, tree_entry, uv_timer_compare); -int uv_timer_init(uv_timer_t* handle) { - uv_counters()->handle_init++; - uv_counters()->timer_init++; +int uv_timer_init(uv_loop_t* loop, uv_timer_t* handle) { + loop->counters.handle_init++; + loop->counters.timer_init++; handle->type = UV_TIMER; + handle->loop = loop; handle->flags = 0; - handle->error = uv_ok_; handle->timer_cb = NULL; handle->repeat = 0; - uv_ref(); + uv_ref(loop); return 0; } -void uv_timer_endgame(uv_timer_t* handle) { +void uv_timer_endgame(uv_loop_t* loop, uv_timer_t* handle) { if (handle->flags & UV_HANDLE_CLOSING) { assert(!(handle->flags & UV_HANDLE_CLOSED)); handle->flags |= UV_HANDLE_CLOSED; if (handle->close_cb) { handle->close_cb((uv_handle_t*)handle); } - uv_unref(); + uv_unref(loop); } } -int uv_timer_start(uv_timer_t* handle, uv_timer_cb timer_cb, int64_t timeout, int64_t repeat) { +int uv_timer_start(uv_timer_t* handle, uv_timer_cb timer_cb, int64_t timeout, + int64_t repeat) { + uv_loop_t* loop = handle->loop; + if (handle->flags & UV_HANDLE_ACTIVE) { - RB_REMOVE(uv_timer_tree_s, &LOOP->timers, handle); + RB_REMOVE(uv_timer_tree_s, &loop->timers, handle); } handle->timer_cb = timer_cb; - handle->due = LOOP->time + timeout; + handle->due = loop->time + timeout; handle->repeat = repeat; handle->flags |= UV_HANDLE_ACTIVE; - if (RB_INSERT(uv_timer_tree_s, &LOOP->timers, handle) != NULL) { + if (RB_INSERT(uv_timer_tree_s, &loop->timers, handle) != NULL) { uv_fatal_error(ERROR_INVALID_DATA, "RB_INSERT"); } return 0; } int uv_timer_stop(uv_timer_t* handle) { + uv_loop_t* loop = handle->loop; + if (!(handle->flags & UV_HANDLE_ACTIVE)) return 0; - RB_REMOVE(uv_timer_tree_s, &LOOP->timers, handle); + RB_REMOVE(uv_timer_tree_s, &loop->timers, handle); handle->flags &= ~UV_HANDLE_ACTIVE; return 0; } int uv_timer_again(uv_timer_t* handle) { + uv_loop_t* loop = handle->loop; + /* If timer_cb is NULL that means that the timer was never started. */ if (!handle->timer_cb) { - uv_set_sys_error(ERROR_INVALID_DATA); + uv_set_sys_error(loop, ERROR_INVALID_DATA); return -1; } if (handle->flags & UV_HANDLE_ACTIVE) { - RB_REMOVE(uv_timer_tree_s, &LOOP->timers, handle); + RB_REMOVE(uv_timer_tree_s, &loop->timers, handle); handle->flags &= ~UV_HANDLE_ACTIVE; } if (handle->repeat) { - handle->due = LOOP->time + handle->repeat; + handle->due = loop->time + handle->repeat; - if (RB_INSERT(uv_timer_tree_s, &LOOP->timers, handle) != NULL) { + if (RB_INSERT(uv_timer_tree_s, &loop->timers, handle) != NULL) { uv_fatal_error(ERROR_INVALID_DATA, "RB_INSERT"); } handle->flags |= UV_HANDLE_ACTIVE; } @@ -207,20 +214,20 @@ assert(handle->type == UV_TIMER); return handle->repeat; } -DWORD uv_get_poll_timeout() { +DWORD uv_get_poll_timeout(uv_loop_t* loop) { uv_timer_t* timer; int64_t delta; /* Check if there are any running timers */ - timer = RB_MIN(uv_timer_tree_s, &LOOP->timers); + timer = RB_MIN(uv_timer_tree_s, &loop->timers); if (timer) { - uv_update_time(); + uv_update_time(loop); - delta = timer->due - LOOP->time; + delta = timer->due - loop->time; if (delta >= UINT_MAX >> 1) { /* A timeout value of UINT_MAX means infinite, so that's no good. But */ /* more importantly, there's always the risk that GetTickCount wraps. */ /* uv_update_time can detect this, but we must make sure that the */ /* tick counter never overflows twice between two subsequent */ @@ -239,25 +246,25 @@ return INFINITE; } } -void uv_process_timers() { +void uv_process_timers(uv_loop_t* loop) { uv_timer_t* timer; /* Call timer callbacks */ - for (timer = RB_MIN(uv_timer_tree_s, &LOOP->timers); - timer != NULL && timer->due <= LOOP->time; - timer = RB_MIN(uv_timer_tree_s, &LOOP->timers)) { - RB_REMOVE(uv_timer_tree_s, &LOOP->timers, timer); + for (timer = RB_MIN(uv_timer_tree_s, &loop->timers); + timer != NULL && timer->due <= loop->time; + timer = RB_MIN(uv_timer_tree_s, &loop->timers)) { + RB_REMOVE(uv_timer_tree_s, &loop->timers, timer); if (timer->repeat != 0) { /* If it is a repeating timer, reschedule with repeat timeout. */ timer->due += timer->repeat; - if (timer->due < LOOP->time) { - timer->due = LOOP->time; + if (timer->due < loop->time) { + timer->due = loop->time; } - if (RB_INSERT(uv_timer_tree_s, &LOOP->timers, timer) != NULL) { + if (RB_INSERT(uv_timer_tree_s, &loop->timers, timer) != NULL) { uv_fatal_error(ERROR_INVALID_DATA, "RB_INSERT"); } } else { /* If non-repeating, mark the timer as inactive. */ timer->flags &= ~UV_HANDLE_ACTIVE;