From bb15c0e268906c01dd120449eb46f4821cf3e30b Mon Sep 17 00:00:00 2001 From: Bert Belder Date: Sun, 11 Sep 2011 22:54:16 +0200 Subject: [PATCH] win: make uv_hrtime() thread-safe --- src/win/timer.c | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/win/timer.c b/src/win/timer.c index c21ea6ec..ac209252 100644 --- a/src/win/timer.c +++ b/src/win/timer.c @@ -33,7 +33,7 @@ /* The resolution of the high-resolution clock. */ static int64_t uv_ticks_per_msec_ = 0; static uint64_t uv_hrtime_frequency_ = 0; -static char uv_hrtime_initialized_ = 0; +static uv_once_t uv_hrtime_init_guard_ = UV_ONCE_INIT; void uv_update_time(uv_loop_t* loop) { @@ -57,23 +57,23 @@ int64_t uv_now(uv_loop_t* loop) { return loop->time; } -/* TODO: thread safety */ + +static void uv_hrtime_init(void) { + LARGE_INTEGER frequency; + + if (!QueryPerformanceFrequency(&frequency)) { + uv_hrtime_frequency_ = 0; + return; + } + + uv_hrtime_frequency_ = frequency.QuadPart; +} + + uint64_t uv_hrtime(void) { LARGE_INTEGER counter; - /* When called for the first time, obtain the high-resolution clock */ - /* frequency. */ - if (!uv_hrtime_initialized_) { - uv_hrtime_initialized_ = 1; - - if (!QueryPerformanceFrequency(&counter)) { - uv_hrtime_frequency_ = 0; - /* uv_set_sys_error(loop, GetLastError()); */ - return 0; - } - - uv_hrtime_frequency_ = counter.QuadPart; - } + uv_once(&uv_hrtime_init_guard_, uv_hrtime_init); /* If the performance frequency is zero, there's no support. */ if (!uv_hrtime_frequency_) {