Skip to content

Commit 54ce3f6

Browse files
committed
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.
1 parent bbf6a14 commit 54ce3f6

File tree

3 files changed

+7
-28
lines changed

3 files changed

+7
-28
lines changed

src/win/internal.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ extern UV_THREAD_LOCAL int uv__crt_assert_enabled;
6565
/* Used by all handles. */
6666
#define UV_HANDLE_CLOSED 0x00000002
6767
#define UV_HANDLE_ENDGAME_QUEUED 0x00000004
68-
#define UV_HANDLE_ACTIVE 0x00000010
6968

7069
/* uv-common.h: #define UV__HANDLE_CLOSING 0x00000001 */
7170
/* uv-common.h: #define UV__HANDLE_ACTIVE 0x00000040 */

src/win/loop-watcher.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ void uv_loop_watcher_endgame(uv_loop_t* loop, uv_handle_t* handle) {
4949
\
5050
assert(handle->type == UV_##NAME); \
5151
\
52-
if (handle->flags & UV_HANDLE_ACTIVE) \
52+
if (uv__is_active(handle)) \
5353
return 0; \
5454
\
5555
if (cb == NULL) \
@@ -67,7 +67,6 @@ void uv_loop_watcher_endgame(uv_loop_t* loop, uv_handle_t* handle) {
6767
loop->name##_handles = handle; \
6868
\
6969
handle->name##_cb = cb; \
70-
handle->flags |= UV_HANDLE_ACTIVE; \
7170
uv__handle_start(handle); \
7271
\
7372
return 0; \
@@ -79,7 +78,7 @@ void uv_loop_watcher_endgame(uv_loop_t* loop, uv_handle_t* handle) {
7978
\
8079
assert(handle->type == UV_##NAME); \
8180
\
82-
if (!(handle->flags & UV_HANDLE_ACTIVE)) \
81+
if (!uv__is_active(handle)) \
8382
return 0; \
8483
\
8584
/* Update loop head if needed */ \
@@ -99,7 +98,6 @@ void uv_loop_watcher_endgame(uv_loop_t* loop, uv_handle_t* handle) {
9998
handle->name##_next->name##_prev = handle->name##_prev; \
10099
} \
101100
\
102-
handle->flags &= ~UV_HANDLE_ACTIVE; \
103101
uv__handle_stop(handle); \
104102
\
105103
return 0; \

src/win/timer.c

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,12 @@ int uv_timer_start(uv_timer_t* handle, uv_timer_cb timer_cb, uint64_t timeout,
122122
if (timer_cb == NULL)
123123
return UV_EINVAL;
124124

125-
if (handle->flags & UV_HANDLE_ACTIVE) {
126-
RB_REMOVE(uv_timer_tree_s, &loop->timers, handle);
127-
}
125+
if (uv__is_active(handle))
126+
uv_timer_stop(handle);
128127

129128
handle->timer_cb = timer_cb;
130129
handle->due = get_clamped_due_time(loop->time, timeout);
131130
handle->repeat = repeat;
132-
handle->flags |= UV_HANDLE_ACTIVE;
133131
uv__handle_start(handle);
134132

135133
/* 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,
145143
int uv_timer_stop(uv_timer_t* handle) {
146144
uv_loop_t* loop = handle->loop;
147145

148-
if (!(handle->flags & UV_HANDLE_ACTIVE))
146+
if (!uv__is_active(handle))
149147
return 0;
150148

151149
RB_REMOVE(uv_timer_tree_s, &loop->timers, handle);
152-
153-
handle->flags &= ~UV_HANDLE_ACTIVE;
154150
uv__handle_stop(handle);
155151

156152
return 0;
@@ -225,23 +221,9 @@ void uv_process_timers(uv_loop_t* loop) {
225221
for (timer = RB_MIN(uv_timer_tree_s, &loop->timers);
226222
timer != NULL && timer->due <= loop->time;
227223
timer = RB_MIN(uv_timer_tree_s, &loop->timers)) {
228-
RB_REMOVE(uv_timer_tree_s, &loop->timers, timer);
229-
230-
if (timer->repeat != 0) {
231-
/* If it is a repeating timer, reschedule with repeat timeout. */
232-
timer->due = get_clamped_due_time(timer->due, timer->repeat);
233-
if (timer->due < loop->time) {
234-
timer->due = loop->time;
235-
}
236-
if (RB_INSERT(uv_timer_tree_s, &loop->timers, timer) != NULL) {
237-
uv_fatal_error(ERROR_INVALID_DATA, "RB_INSERT");
238-
}
239-
} else {
240-
/* If non-repeating, mark the timer as inactive. */
241-
timer->flags &= ~UV_HANDLE_ACTIVE;
242-
uv__handle_stop(timer);
243-
}
244224

225+
uv_timer_stop(timer);
226+
uv_timer_again(timer);
245227
timer->timer_cb((uv_timer_t*) timer);
246228
}
247229
}

0 commit comments

Comments
 (0)