Skip to content

Commit

Permalink
windows: properly close the default loop
Browse files Browse the repository at this point in the history
Also close the IOCP handle for any loop.

Closes #393
  • Loading branch information
saghul committed Sep 15, 2014
1 parent f70b3fa commit 0e5004b
Showing 1 changed file with 25 additions and 24 deletions.
49 changes: 25 additions & 24 deletions src/win/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,11 @@
#include "req-inl.h"


/* The only event loop we support right now */
static uv_loop_t uv_default_loop_;
static uv_loop_t default_loop_struct;
static uv_loop_t* default_loop_ptr;

/* uv_once intialization guards */
static uv_once_t uv_init_guard_ = UV_ONCE_INIT;
static uv_once_t uv_default_loop_init_guard_ = UV_ONCE_INIT;


#if defined(_DEBUG) && (defined(_MSC_VER) || defined(__MINGW64_VERSION_MAJOR))
Expand Down Expand Up @@ -181,48 +180,45 @@ int uv_loop_init(uv_loop_t* loop) {
}


static void uv_default_loop_init(void) {
/* Initialize libuv itself first */
uv__once_init();

/* Initialize the main loop */
uv_loop_init(&uv_default_loop_);
}


void uv__once_init(void) {
uv_once(&uv_init_guard_, uv_init);
}


uv_loop_t* uv_default_loop(void) {
uv_once(&uv_default_loop_init_guard_, uv_default_loop_init);
return &uv_default_loop_;
if (default_loop_ptr != NULL)
return default_loop_ptr;

if (uv_loop_init(&default_loop_struct))
return NULL;

default_loop_ptr = &default_loop_struct;
return default_loop_ptr;
}


static void uv__loop_close(uv_loop_t* loop) {
size_t i;

/* close the async handle without needeing an extra loop iteration */
assert(!loop->wq_async.async_sent);
loop->wq_async.close_cb = NULL;
uv__handle_closing(&loop->wq_async);
uv__handle_close(&loop->wq_async);

if (loop != &uv_default_loop_) {
size_t i;
for (i = 0; i < ARRAY_SIZE(loop->poll_peer_sockets); i++) {
SOCKET sock = loop->poll_peer_sockets[i];
if (sock != 0 && sock != INVALID_SOCKET)
closesocket(sock);
}
for (i = 0; i < ARRAY_SIZE(loop->poll_peer_sockets); i++) {
SOCKET sock = loop->poll_peer_sockets[i];
if (sock != 0 && sock != INVALID_SOCKET)
closesocket(sock);
}
/* TODO: cleanup default loop*/

uv_mutex_lock(&loop->wq_mutex);
assert(QUEUE_EMPTY(&loop->wq) && "thread pool work queue not empty!");
assert(!uv__has_active_reqs(loop));
uv_mutex_unlock(&loop->wq_mutex);
uv_mutex_destroy(&loop->wq_mutex);

CloseHandle(loop->iocp);
}


Expand All @@ -242,6 +238,8 @@ int uv_loop_close(uv_loop_t* loop) {
#ifndef NDEBUG
memset(loop, -1, sizeof(*loop));
#endif
if (loop == default_loop_ptr)
default_loop_ptr = NULL;

return 0;
}
Expand All @@ -265,9 +263,12 @@ uv_loop_t* uv_loop_new(void) {


void uv_loop_delete(uv_loop_t* loop) {
int err = uv_loop_close(loop);
uv_loop_t* default_loop;
int err;
default_loop = default_loop_ptr;
err = uv_loop_close(loop);
assert(err == 0);
if (loop != &uv_default_loop_)
if (loop != default_loop)
free(loop);
}

Expand Down

0 comments on commit 0e5004b

Please sign in to comment.