Skip to content
This repository has been archived by the owner on May 4, 2018. It is now read-only.

Commit

Permalink
windows: make active and closing handle state independent
Browse files Browse the repository at this point in the history
  • Loading branch information
piscisaureus committed Aug 27, 2012
1 parent c77d08e commit 637be16
Show file tree
Hide file tree
Showing 15 changed files with 42 additions and 35 deletions.
1 change: 0 additions & 1 deletion src/fs-poll.c
Expand Up @@ -238,7 +238,6 @@ static int statbuf_eq(const uv_statbuf_t* a, const uv_statbuf_t* b) {
void uv__fs_poll_endgame(uv_loop_t* loop, uv_fs_poll_t* handle) {
assert(handle->flags & UV_HANDLE_CLOSING);
assert(!(handle->flags & UV_HANDLE_CLOSED));
uv__handle_stop(handle);
uv__handle_close(handle);
}

Expand Down
16 changes: 11 additions & 5 deletions src/uv-common.h
Expand Up @@ -53,12 +53,14 @@
enum {
UV__HANDLE_INTERNAL = 0x8000,
UV__HANDLE_ACTIVE = 0x4000,
UV__HANDLE_REF = 0x2000
UV__HANDLE_REF = 0x2000,
UV__HANDLE_CLOSING = 0 /* no-op on unix */
};
#else
# define UV__HANDLE_INTERNAL 0x80
# define UV__HANDLE_ACTIVE 0x40
# define UV__HANDLE_REF 0x20
# define UV__HANDLE_CLOSING 0x01
#endif

extern const uv_err_t uv_ok_;
Expand Down Expand Up @@ -129,28 +131,32 @@ UNUSED static int uv__is_active(const uv_handle_t* h) {

UNUSED static void uv__handle_start(uv_handle_t* h) {
if (h->flags & UV__HANDLE_ACTIVE) return;
if (h->flags & UV__HANDLE_REF) uv__active_handle_add(h);
h->flags |= UV__HANDLE_ACTIVE;
if (h->flags & UV__HANDLE_CLOSING) return;
if (h->flags & UV__HANDLE_REF) uv__active_handle_add(h);
}
#define uv__handle_start(h) uv__handle_start((uv_handle_t*)(h))

UNUSED static void uv__handle_stop(uv_handle_t* h) {
if (!(h->flags & UV__HANDLE_ACTIVE)) return;
if (h->flags & UV__HANDLE_REF) uv__active_handle_rm(h);
h->flags &= ~UV__HANDLE_ACTIVE;
if (h->flags & UV__HANDLE_CLOSING) return;
if (h->flags & UV__HANDLE_REF) uv__active_handle_rm(h);
}
#define uv__handle_stop(h) uv__handle_stop((uv_handle_t*)(h))

UNUSED static void uv__handle_ref(uv_handle_t* h) {
if (h->flags & UV__HANDLE_REF) return;
if (h->flags & UV__HANDLE_ACTIVE) uv__active_handle_add(h);
if (h->flags & (UV__HANDLE_ACTIVE | UV__HANDLE_CLOSING))
uv__active_handle_add(h);
h->flags |= UV__HANDLE_REF;
}
#define uv__handle_ref(h) uv__handle_ref((uv_handle_t*)(h))

UNUSED static void uv__handle_unref(uv_handle_t* h) {
if (!(h->flags & UV__HANDLE_REF)) return;
if (h->flags & UV__HANDLE_ACTIVE) uv__active_handle_rm(h);
if (h->flags & (UV__HANDLE_ACTIVE | UV__HANDLE_CLOSING))
uv__active_handle_rm(h);
h->flags &= ~UV__HANDLE_REF;
}
#define uv__handle_unref(h) uv__handle_unref((uv_handle_t*)(h))
Expand Down
3 changes: 1 addition & 2 deletions src/win/async.c
Expand Up @@ -32,7 +32,6 @@ void uv_async_endgame(uv_loop_t* loop, uv_async_t* handle) {
if (handle->flags & UV_HANDLE_CLOSING &&
!handle->async_sent) {
assert(!(handle->flags & UV_HANDLE_CLOSED));
uv__handle_stop(handle);
uv__handle_close(handle);
}
}
Expand Down Expand Up @@ -61,7 +60,7 @@ void uv_async_close(uv_loop_t* loop, uv_async_t* handle) {
uv_want_endgame(loop, (uv_handle_t*) handle);
}

uv__handle_start(handle);
uv__handle_closing(handle);
}


Expand Down
3 changes: 1 addition & 2 deletions src/win/fs-event.c
Expand Up @@ -470,15 +470,14 @@ void uv_fs_event_close(uv_loop_t* loop, uv_fs_event_t* handle) {
uv_want_endgame(loop, (uv_handle_t*)handle);
}

uv__handle_start(handle);
uv__handle_closing(handle);
}


void uv_fs_event_endgame(uv_loop_t* loop, uv_fs_event_t* handle) {
if (handle->flags & UV_HANDLE_CLOSING &&
!handle->req_pending) {
assert(!(handle->flags & UV_HANDLE_CLOSED));
uv__handle_stop(handle);

if (handle->buffer) {
_aligned_free(handle->buffer);
Expand Down
17 changes: 16 additions & 1 deletion src/win/handle-inl.h
Expand Up @@ -59,12 +59,27 @@
} while (0)


#define uv__handle_closing(handle) \
do { \
assert(!((handle)->flags & UV__HANDLE_CLOSING)); \
(handle)->flags |= UV__HANDLE_CLOSING; \
if ((handle)->flags & UV__HANDLE_ACTIVE) { \
(handle)->flags &= ~UV__HANDLE_ACTIVE; \
} else if ((handle)->flags & UV__HANDLE_REF) { \
uv__active_handle_add((uv_handle_t*) (handle)); \
} \
} while (0)


#define uv__handle_close(handle) \
do { \
ngx_queue_remove(&(handle)->handle_queue); \
(handle)->flags |= UV_HANDLE_CLOSED; \
if (handle->flags & UV__HANDLE_REF) { \
uv__active_handle_rm((uv_handle_t*) (handle)); \
} \
if ((handle)->close_cb) { \
(handle)->close_cb((uv_handle_t*)(handle)); \
(handle)->close_cb((uv_handle_t*) (handle)); \
} \
} while (0)

Expand Down
11 changes: 5 additions & 6 deletions src/win/handle.c
Expand Up @@ -71,7 +71,6 @@ void uv_close(uv_handle_t* handle, uv_close_cb cb) {
return;
}

handle->flags |= UV_HANDLE_CLOSING;
handle->close_cb = cb;

/* Handle-specific close actions */
Expand All @@ -98,25 +97,25 @@ void uv_close(uv_handle_t* handle, uv_close_cb cb) {

case UV_TIMER:
uv_timer_stop((uv_timer_t*)handle);
uv__handle_start(handle);
uv__handle_closing(handle);
uv_want_endgame(loop, handle);
return;

case UV_PREPARE:
uv_prepare_stop((uv_prepare_t*)handle);
uv__handle_start(handle);
uv__handle_closing(handle);
uv_want_endgame(loop, handle);
return;

case UV_CHECK:
uv_check_stop((uv_check_t*)handle);
uv__handle_start(handle);
uv__handle_closing(handle);
uv_want_endgame(loop, handle);
return;

case UV_IDLE:
uv_idle_stop((uv_idle_t*)handle);
uv__handle_start(handle);
uv__handle_closing(handle);
uv_want_endgame(loop, handle);
return;

Expand All @@ -138,7 +137,7 @@ void uv_close(uv_handle_t* handle, uv_close_cb cb) {

case UV_FS_POLL:
uv__fs_poll_close((uv_fs_poll_t*) handle);
uv__handle_start(handle);
uv__handle_closing(handle);
uv_want_endgame(loop, handle);
return;

Expand Down
1 change: 0 additions & 1 deletion src/win/loop-watcher.c
Expand Up @@ -30,7 +30,6 @@ void uv_loop_watcher_endgame(uv_loop_t* loop, uv_handle_t* handle) {
if (handle->flags & UV_HANDLE_CLOSING) {
assert(!(handle->flags & UV_HANDLE_CLOSED));
handle->flags |= UV_HANDLE_CLOSED;
uv__handle_stop(handle);
uv__handle_close(handle);
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/win/pipe.c
Expand Up @@ -357,7 +357,6 @@ void uv_pipe_endgame(uv_loop_t* loop, uv_pipe_t* handle) {
if (handle->flags & UV_HANDLE_CLOSING &&
handle->reqs_pending == 0) {
assert(!(handle->flags & UV_HANDLE_CLOSED));
uv__handle_stop(handle);

if (handle->flags & UV_HANDLE_CONNECTION) {
if (handle->pending_ipc_info.socket_info) {
Expand Down Expand Up @@ -660,7 +659,7 @@ void uv_pipe_close(uv_loop_t* loop, uv_pipe_t* handle) {
uv_want_endgame(loop, (uv_handle_t*) handle);
}

uv__handle_start(handle);
uv__handle_closing(handle);
}


Expand Down
5 changes: 2 additions & 3 deletions src/win/poll.c
Expand Up @@ -230,7 +230,7 @@ static int uv__fast_poll_set(uv_loop_t* loop, uv_poll_t* handle, int events) {

static void uv__fast_poll_close(uv_loop_t* loop, uv_poll_t* handle) {
handle->events = 0;
uv__handle_start(handle);
uv__handle_closing(handle);

if (handle->submitted_events_1 == 0 &&
handle->submitted_events_2 == 0) {
Expand Down Expand Up @@ -477,7 +477,7 @@ static int uv__slow_poll_set(uv_loop_t* loop, uv_poll_t* handle, int events) {

static void uv__slow_poll_close(uv_loop_t* loop, uv_poll_t* handle) {
handle->events = 0;
uv__handle_start(handle);
uv__handle_closing(handle);

if (handle->submitted_events_1 == 0 &&
handle->submitted_events_2 == 0) {
Expand Down Expand Up @@ -611,6 +611,5 @@ void uv_poll_endgame(uv_loop_t* loop, uv_poll_t* handle) {
assert(handle->submitted_events_1 == 0);
assert(handle->submitted_events_2 == 0);

uv__handle_stop(handle);
uv__handle_close(handle);
}
4 changes: 1 addition & 3 deletions src/win/process.c
Expand Up @@ -700,7 +700,7 @@ void uv_process_proc_exit(uv_loop_t* loop, uv_process_t* handle) {


void uv_process_close(uv_loop_t* loop, uv_process_t* handle) {
uv__handle_start(handle);
uv__handle_closing(handle);

if (handle->wait_handle != INVALID_HANDLE_VALUE) {
/* This blocks until either the wait was cancelled, or the callback has */
Expand All @@ -725,8 +725,6 @@ void uv_process_endgame(uv_loop_t* loop, uv_process_t* handle) {
assert(handle->flags & UV_HANDLE_CLOSING);
assert(!(handle->flags & UV_HANDLE_CLOSED));

uv__handle_stop(handle);

/* Clean-up the process handle. */
CloseHandle(handle->process_handle);

Expand Down
3 changes: 1 addition & 2 deletions src/win/signal.c
Expand Up @@ -333,9 +333,9 @@ void uv_process_signal_req(uv_loop_t* loop, uv_signal_t* handle,

void uv_signal_close(uv_loop_t* loop, uv_signal_t* handle) {
uv_signal_stop(handle);
uv__handle_closing(handle);

if (handle->pending_signum == 0) {
uv__handle_start(handle);
uv_want_endgame(loop, (uv_handle_t*) handle);
}
}
Expand All @@ -350,6 +350,5 @@ void uv_signal_endgame(uv_loop_t* loop, uv_signal_t* handle) {

handle->flags |= UV_HANDLE_CLOSED;

uv__handle_stop(handle);
uv__handle_close(handle);
}
3 changes: 1 addition & 2 deletions src/win/tcp.c
Expand Up @@ -187,7 +187,6 @@ void uv_tcp_endgame(uv_loop_t* loop, uv_tcp_t* handle) {
if (handle->flags & UV_HANDLE_CLOSING &&
handle->reqs_pending == 0) {
assert(!(handle->flags & UV_HANDLE_CLOSED));
uv__handle_stop(handle);

if (!(handle->flags & UV_HANDLE_TCP_SOCKET_CLOSED)) {
closesocket(handle->socket);
Expand Down Expand Up @@ -1386,7 +1385,7 @@ void uv_tcp_close(uv_loop_t* loop, uv_tcp_t* tcp) {
tcp->flags |= UV_HANDLE_TCP_SOCKET_CLOSED;
}

uv__handle_start(tcp);
uv__handle_closing(tcp);

if (tcp->reqs_pending == 0) {
uv_want_endgame(tcp->loop, (uv_handle_t*)tcp);
Expand Down
1 change: 0 additions & 1 deletion src/win/timer.c
Expand Up @@ -78,7 +78,6 @@ int uv_timer_init(uv_loop_t* loop, 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));
uv__handle_stop(handle);
uv__handle_close(handle);
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/win/tty.c
Expand Up @@ -1816,7 +1816,7 @@ void uv_tty_close(uv_tty_t* handle) {
handle->flags |= UV_HANDLE_SHUTTING;
}

uv__handle_start(handle);
uv__handle_closing(handle);

if (handle->reqs_pending == 0) {
uv_want_endgame(handle->loop, (uv_handle_t*) handle);
Expand Down Expand Up @@ -1859,7 +1859,6 @@ void uv_tty_endgame(uv_loop_t* loop, uv_tty_t* handle) {
handle->read_raw_wait == NULL);

assert(!(handle->flags & UV_HANDLE_CLOSED));
uv__handle_stop(handle);
uv__handle_close(handle);
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/win/udp.c
Expand Up @@ -143,7 +143,7 @@ void uv_udp_close(uv_loop_t* loop, uv_udp_t* handle) {
uv_udp_recv_stop(handle);
closesocket(handle->socket);

uv__handle_start(handle);
uv__handle_closing(handle);

if (handle->reqs_pending == 0) {
uv_want_endgame(loop, (uv_handle_t*) handle);
Expand All @@ -155,7 +155,6 @@ void uv_udp_endgame(uv_loop_t* loop, uv_udp_t* handle) {
if (handle->flags & UV_HANDLE_CLOSING &&
handle->reqs_pending == 0) {
assert(!(handle->flags & UV_HANDLE_CLOSED));
uv__handle_stop(handle);
uv__handle_close(handle);
}
}
Expand Down

0 comments on commit 637be16

Please sign in to comment.