From f790b689a7603983f73683528176efe33b192332 Mon Sep 17 00:00:00 2001 From: Bert Belder Date: Thu, 8 Sep 2011 17:58:31 +0200 Subject: [PATCH] Remove uv_init from uv.h and windows backend --- include/uv.h | 3 +- src/win/core.c | 40 ++++++++++++++--------- src/win/internal.h | 15 +++++++++ src/win/threads.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++ uv.gyp | 1 + 5 files changed, 122 insertions(+), 18 deletions(-) create mode 100644 src/win/threads.c diff --git a/include/uv.h b/include/uv.h index e42509e9..58678c56 100644 --- a/include/uv.h +++ b/include/uv.h @@ -81,11 +81,10 @@ typedef struct uv_work_s uv_work_t; * All callbacks in libuv are made asynchronously. That is they are never * made by the function that takes them as a parameter. */ -void uv_init(); uv_loop_t* uv_loop_new(); - void uv_loop_delete(uv_loop_t*); + /* * Returns the default loop. */ diff --git a/src/win/core.c b/src/win/core.c index a8fa5c57..3211bbf2 100644 --- a/src/win/core.c +++ b/src/win/core.c @@ -33,7 +33,22 @@ /* The only event loop we support right now */ static uv_loop_t uv_default_loop_; -static int uv_default_loop_initialized_ = 0; + +/* 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; + + +static void uv_init(void) { + /* Initialize winsock */ + uv_winsock_init(); + + /* Fetch winapi function pointers */ + uv_winapi_init(); + + /* Initialize FS */ + uv_fs_init(); +} static void uv_loop_init(uv_loop_t* loop) { @@ -68,25 +83,18 @@ static void uv_loop_init(uv_loop_t* loop) { } -uv_loop_t* uv_default_loop() { - if (!uv_default_loop_initialized_) { - uv_loop_init(&uv_default_loop_); - uv_default_loop_initialized_ = 1; - } +static void uv_default_loop_init(void) { + /* Intialize libuv itself first */ + uv_once(&uv_init_guard_, uv_init); - return &uv_default_loop_; + /* Initialize the main loop */ + uv_loop_init(&uv_default_loop_); } -void uv_init() { - /* Initialize winsock */ - uv_winsock_init(); - - /* Fetch winapi function pointers */ - uv_winapi_init(); - - /* Initialize FS */ - uv_fs_init(); +uv_loop_t* uv_default_loop() { + uv_once(&uv_default_loop_init_guard_, uv_default_loop_init); + return &uv_default_loop_; } diff --git a/src/win/internal.h b/src/win/internal.h index 19085b63..ee1834ed 100644 --- a/src/win/internal.h +++ b/src/win/internal.h @@ -279,4 +279,19 @@ void uv_winsock_init(); int uv_ntstatus_to_winsock_error(NTSTATUS status); +/* Threads and synchronization */ +typedef struct uv_once_s { + unsigned char ran; + /* The actual event handle must be aligned to sizeof(HANDLE), so in */ + /* practice it might overlap padding a little. */ + HANDLE event; + HANDLE padding; +} uv_once_t; + +#define UV_ONCE_INIT \ + { 0, NULL, NULL } + +void uv_once(uv_once_t* guard, void (*callback)(void)); + + #endif /* UV_WIN_INTERNAL_H_ */ diff --git a/src/win/threads.c b/src/win/threads.c new file mode 100644 index 00000000..1fc6b73f --- /dev/null +++ b/src/win/threads.c @@ -0,0 +1,81 @@ +/* Copyright Joyent, Inc. and other Node contributors. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#include + +#include "uv.h" +#include "../uv-common.h" +#include "internal.h" + + +#ifdef _MSC_VER /* msvc */ +# define NOINLINE __declspec (noinline) +#else /* gcc */ +# define NOINLINE __attribute__ ((noinline)) +#endif + + +static NOINLINE void uv__once_inner(uv_once_t* guard, + void (*callback)(void)) { + DWORD result; + HANDLE existing_event, created_event; + HANDLE* event_ptr; + + /* Fetch and align event_ptr */ + event_ptr = (HANDLE*) (((uintptr_t) &guard->event + (sizeof(HANDLE) - 1)) & + ~(sizeof(HANDLE) - 1)); + + created_event = CreateEvent(NULL, 1, 0, NULL); + if (created_event == 0) { + /* Could fail in a low-memory situation? */ + uv_fatal_error(GetLastError(), "CreateEvent"); + } + + existing_event = InterlockedCompareExchangePointer(event_ptr, + created_event, + NULL); + + if (existing_event == NULL) { + /* We won the race */ + callback(); + + result = SetEvent(created_event); + assert(result); + guard->ran = 1; + + } else { + /* We lost the race. Destroy the event we created and wait for the */ + /* existing one to become signaled. */ + CloseHandle(created_event); + result = WaitForSingleObject(existing_event, INFINITE); + assert(result == WAIT_OBJECT_0); + } +} + + +void uv_once(uv_once_t* guard, void (*callback)(void)) { + /* Fast case - avoid WaitForSingleObject. */ + if (guard->ran) { + return; + } + + uv__once_inner(guard, callback); +} diff --git a/uv.gyp b/uv.gyp index 07352341..de68a38f 100644 --- a/uv.gyp +++ b/uv.gyp @@ -114,6 +114,7 @@ 'src/win/stream.c', 'src/win/tcp.c', 'src/win/threadpool.c', + 'src/win/threads.c', 'src/win/timer.c', 'src/win/udp.c', 'src/win/util.c',