Skip to content

Commit

Permalink
unix,win: unify timers implementation
Browse files Browse the repository at this point in the history
PR-URL: libuv#1058
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Imran Iqbal <imran@imraniqbal.org>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
  • Loading branch information
saghul committed Sep 23, 2016
1 parent d8baf7a commit 912d42b
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 241 deletions.
3 changes: 1 addition & 2 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ libuv_la_SOURCES = src/fs-poll.c \
src/loop-watcher.c \
src/queue.h \
src/threadpool.c \
src/timer.c \
src/uv-common.c \
src/uv-common.h \
src/version.c
Expand Down Expand Up @@ -69,7 +70,6 @@ libuv_la_SOURCES += src/win/async.c \
src/win/stream-inl.h \
src/win/tcp.c \
src/win/thread.c \
src/win/timer.c \
src/win/tty.c \
src/win/udp.c \
src/win/util.c \
Expand Down Expand Up @@ -99,7 +99,6 @@ libuv_la_SOURCES += src/unix/async.c \
src/unix/stream.c \
src/unix/tcp.c \
src/unix/thread.c \
src/unix/timer.c \
src/unix/tty.c \
src/unix/udp.c

Expand Down
20 changes: 10 additions & 10 deletions include/uv-win.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,6 @@ typedef struct {
char* errmsg;
} uv_lib_t;

RB_HEAD(uv_timer_tree_s, uv_timer_s);

#define UV_LOOP_PRIVATE_FIELDS \
/* The loop's I/O completion port */ \
HANDLE iocp; \
Expand All @@ -208,16 +206,18 @@ RB_HEAD(uv_timer_tree_s, uv_timer_s);
uv_req_t* pending_reqs_tail; \
/* Head of a single-linked list of closed handles */ \
uv_handle_t* endgame_handles; \
/* The head of the timers tree */ \
struct uv_timer_tree_s timers; \
/* Timers */ \
struct { \
void* min; \
unsigned int nelts; \
} timer_heap; \
uint64_t timer_counter; \
/* Lists of active loop (prepare / check / idle) watchers */ \
void* prepare_handles[2]; \
void* check_handles[2]; \
void* idle_handles[2]; \
/* This handle holds the peer sockets for the fast variant of uv_poll_t */ \
SOCKET poll_peer_sockets[UV_MSAFD_PROVIDER_COUNT]; \
/* Counter to started timer */ \
uint64_t timer_counter; \
/* Threadpool */ \
void* wq[2]; \
uv_mutex_t wq_mutex; \
Expand Down Expand Up @@ -407,11 +407,11 @@ RB_HEAD(uv_timer_tree_s, uv_timer_s);
unsigned char events;

#define UV_TIMER_PRIVATE_FIELDS \
RB_ENTRY(uv_timer_s) tree_entry; \
uint64_t due; \
uv_timer_cb timer_cb; \
void* heap_node[3]; \
uint64_t timeout; \
uint64_t repeat; \
uint64_t start_id; \
uv_timer_cb timer_cb;
uint64_t start_id;

#define UV_ASYNC_PRIVATE_FIELDS \
void* queue[2]; \
Expand Down
10 changes: 4 additions & 6 deletions src/unix/timer.c → src/timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/

#include "uv.h"
#include "internal.h"
#include "uv-common.h"
#include "heap-inl.h"

#include <assert.h>
Expand Down Expand Up @@ -66,7 +66,7 @@ int uv_timer_start(uv_timer_t* handle,
uint64_t clamped_timeout;

if (cb == NULL)
return -EINVAL;
return UV_EINVAL;

if (uv__is_active(handle))
uv_timer_stop(handle);
Expand Down Expand Up @@ -105,12 +105,10 @@ int uv_timer_stop(uv_timer_t* handle) {

int uv_timer_again(uv_timer_t* handle) {
if (handle->timer_cb == NULL)
return -EINVAL;
return UV_EINVAL;

if (handle->repeat) {
uv_timer_stop(handle);
if (handle->repeat)
uv_timer_start(handle, handle->timer_cb, handle->repeat, handle->repeat);
}

return 0;
}
Expand Down
5 changes: 0 additions & 5 deletions src/unix/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,6 @@ int uv__tcp_keepalive(int fd, int on, unsigned int delay);
/* pipe */
int uv_pipe_listen(uv_pipe_t* handle, int backlog, uv_connection_cb cb);

/* timer */
void uv__run_timers(uv_loop_t* loop);
int uv__next_timeout(const uv_loop_t* loop);

/* signal */
void uv__signal_close(uv_signal_t* handle);
void uv__signal_global_once_init(void);
Expand All @@ -240,7 +236,6 @@ void uv__poll_close(uv_poll_t* handle);
void uv__process_close(uv_process_t* handle);
void uv__stream_close(uv_stream_t* handle);
void uv__tcp_close(uv_tcp_t* handle);
void uv__timer_close(uv_timer_t* handle);
void uv__udp_close(uv_udp_t* handle);
void uv__udp_finish_close(uv_udp_t* handle);
uv_handle_type uv__handle_type(int fd);
Expand Down
5 changes: 5 additions & 0 deletions src/uv-common.h
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,9 @@ void uv__idle_close(uv_idle_t* handle);
void uv__prepare_close(uv_prepare_t* handle);
void uv__check_close(uv_check_t* handle);

/* Timer prototypes */
void uv__run_timers(uv_loop_t* loop);
int uv__next_timeout(const uv_loop_t* loop);
void uv__timer_close(uv_timer_t* handle);

#endif /* UV_COMMON_H_ */
26 changes: 19 additions & 7 deletions src/win/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "queue.h"
#include "handle-inl.h"
#include "req-inl.h"
#include "heap-inl.h"


static uv_loop_t default_loop_struct;
Expand Down Expand Up @@ -159,6 +160,17 @@ static void uv_init(void) {
}


/* The number of milliseconds in one second. */
#define UV__MILLISEC 1000


void uv_update_time(uv_loop_t* loop) {
uint64_t new_time = uv__hrtime(UV__MILLISEC);
assert(new_time >= loop->time);
loop->time = new_time;
}


int uv_loop_init(uv_loop_t* loop) {
int err;

Expand All @@ -185,7 +197,8 @@ int uv_loop_init(uv_loop_t* loop) {

loop->endgame_handles = NULL;

RB_INIT(&loop->timers);
heap_init((struct heap*) &loop->timer_heap);
loop->timer_counter = 0;

QUEUE_INIT(&loop->check_handles);
QUEUE_INIT(&loop->prepare_handles);
Expand All @@ -197,7 +210,6 @@ int uv_loop_init(uv_loop_t* loop) {

memset(&loop->poll_peer_sockets, 0, sizeof loop->poll_peer_sockets);

loop->timer_counter = 0;
loop->stop_flag = 0;

err = uv_mutex_init(&loop->wq_mutex);
Expand Down Expand Up @@ -289,7 +301,7 @@ int uv_backend_timeout(const uv_loop_t* loop) {
}


static void uv__loop_poll(uv_loop_t* loop, DWORD timeout) {
static void uv__loop_poll(uv_loop_t* loop, int timeout) {
BOOL success;
uv_req_t* req;
OVERLAPPED_ENTRY overlappeds[128];
Expand Down Expand Up @@ -361,7 +373,7 @@ int uv_loop_alive(const uv_loop_t* loop) {


int uv_run(uv_loop_t *loop, uv_run_mode mode) {
DWORD timeout;
int timeout;
int r;
int ran_pending;

Expand All @@ -371,7 +383,7 @@ int uv_run(uv_loop_t *loop, uv_run_mode mode) {

while (r != 0 && loop->stop_flag == 0) {
uv_update_time(loop);
uv_process_timers(loop);
uv__run_timers(loop);

ran_pending = uv_process_reqs(loop);
uv__run_idle(loop);
Expand All @@ -381,7 +393,7 @@ int uv_run(uv_loop_t *loop, uv_run_mode mode) {
if ((mode == UV_RUN_ONCE && !ran_pending) || mode == UV_RUN_DEFAULT)
timeout = uv_backend_timeout(loop);

uv__loop_poll(loop, timeout);
uv__loop_poll(loop, timeout == -1 ? INFINITE : timeout);

uv__run_check(loop);
uv_process_endgames(loop);
Expand All @@ -395,7 +407,7 @@ int uv_run(uv_loop_t *loop, uv_run_mode mode) {
* UV_RUN_NOWAIT makes no guarantees about progress so it's omitted from
* the check.
*/
uv_process_timers(loop);
uv__run_timers(loop);
}

r = uv__loop_alive(loop);
Expand Down
4 changes: 0 additions & 4 deletions src/win/handle-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,11 @@ INLINE static void uv_process_endgames(uv_loop_t* loop) {
break;

case UV_TIMER:
uv_timer_endgame(loop, (uv_timer_t*) handle);
break;

case UV_PREPARE:
case UV_CHECK:
case UV_IDLE:
assert(handle->flags & UV__HANDLE_CLOSING);
assert(!(handle->flags & UV_HANDLE_CLOSED));
handle->flags |= UV_HANDLE_CLOSED;
uv__handle_close(handle);
break;

Expand Down
2 changes: 1 addition & 1 deletion src/win/handle.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ void uv_close(uv_handle_t* handle, uv_close_cb cb) {
return;

case UV_TIMER:
uv_timer_stop((uv_timer_t*)handle);
uv__timer_close((uv_timer_t*)handle);
uv__handle_closing(handle);
uv_want_endgame(loop, handle);
return;
Expand Down
9 changes: 0 additions & 9 deletions src/win/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,15 +241,6 @@ int uv_poll_close(uv_loop_t* loop, uv_poll_t* handle);
void uv_poll_endgame(uv_loop_t* loop, uv_poll_t* handle);


/*
* Timers
*/
void uv_timer_endgame(uv_loop_t* loop, uv_timer_t* handle);

DWORD uv__next_timeout(const uv_loop_t* loop);
void uv_process_timers(uv_loop_t* loop);


/*
* Loop watchers
*/
Expand Down
Loading

0 comments on commit 912d42b

Please sign in to comment.