windows: further simplify the code for timers

- Remove the UV_HANDLE_ACTIVE flag. It's a duplicate from
  UV__HANDLE_ACTIVE, which was used solely on timers and loop watchers.

- Avoid duplicated code when running timers by stopping the handle and
  rearming it with the repeat time, thus having a single place where the
  timers are added and removed to and from the RB tree, respectively.
This commit is contained in:
Saúl Ibarra Corretgé 2014-09-05 19:18:39 +02:00
parent bbf6a144e1
commit 54ce3f6c8e
3 changed files with 7 additions and 28 deletions

View File

@ -65,7 +65,6 @@ extern UV_THREAD_LOCAL int uv__crt_assert_enabled;
/* Used by all handles. */
#define UV_HANDLE_CLOSED 0x00000002
#define UV_HANDLE_ENDGAME_QUEUED 0x00000004
#define UV_HANDLE_ACTIVE 0x00000010
/* uv-common.h: #define UV__HANDLE_CLOSING 0x00000001 */
/* uv-common.h: #define UV__HANDLE_ACTIVE 0x00000040 */

View File

@ -49,7 +49,7 @@ void uv_loop_watcher_endgame(uv_loop_t* loop, uv_handle_t* handle) {
\
assert(handle->type == UV_##NAME); \
\
if (handle->flags & UV_HANDLE_ACTIVE) \
if (uv__is_active(handle)) \
return 0; \
\
if (cb == NULL) \
@ -67,7 +67,6 @@ void uv_loop_watcher_endgame(uv_loop_t* loop, uv_handle_t* handle) {
loop->name##_handles = handle; \
\
handle->name##_cb = cb; \
handle->flags |= UV_HANDLE_ACTIVE; \
uv__handle_start(handle); \
\
return 0; \
@ -79,7 +78,7 @@ void uv_loop_watcher_endgame(uv_loop_t* loop, uv_handle_t* handle) {
\
assert(handle->type == UV_##NAME); \
\
if (!(handle->flags & UV_HANDLE_ACTIVE)) \
if (!uv__is_active(handle)) \
return 0; \
\
/* Update loop head if needed */ \
@ -99,7 +98,6 @@ void uv_loop_watcher_endgame(uv_loop_t* loop, uv_handle_t* handle) {
handle->name##_next->name##_prev = handle->name##_prev; \
} \
\
handle->flags &= ~UV_HANDLE_ACTIVE; \
uv__handle_stop(handle); \
\
return 0; \

View File

@ -122,14 +122,12 @@ int uv_timer_start(uv_timer_t* handle, uv_timer_cb timer_cb, uint64_t timeout,
if (timer_cb == NULL)
return UV_EINVAL;
if (handle->flags & UV_HANDLE_ACTIVE) {
RB_REMOVE(uv_timer_tree_s, &loop->timers, handle);
}
if (uv__is_active(handle))
uv_timer_stop(handle);
handle->timer_cb = timer_cb;
handle->due = get_clamped_due_time(loop->time, timeout);
handle->repeat = repeat;
handle->flags |= UV_HANDLE_ACTIVE;
uv__handle_start(handle);
/* start_id is the second index to be compared in uv__timer_cmp() */
@ -145,12 +143,10 @@ int uv_timer_start(uv_timer_t* handle, uv_timer_cb timer_cb, uint64_t timeout,
int uv_timer_stop(uv_timer_t* handle) {
uv_loop_t* loop = handle->loop;
if (!(handle->flags & UV_HANDLE_ACTIVE))
if (!uv__is_active(handle))
return 0;
RB_REMOVE(uv_timer_tree_s, &loop->timers, handle);
handle->flags &= ~UV_HANDLE_ACTIVE;
uv__handle_stop(handle);
return 0;
@ -225,23 +221,9 @@ void uv_process_timers(uv_loop_t* loop) {
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 = get_clamped_due_time(timer->due, timer->repeat);
if (timer->due < loop->time) {
timer->due = loop->time;
}
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;
uv__handle_stop(timer);
}
uv_timer_stop(timer);
uv_timer_again(timer);
timer->timer_cb((uv_timer_t*) timer);
}
}