win: make uv_hrtime() thread-safe

This commit is contained in:
Bert Belder 2011-09-11 22:54:16 +02:00 committed by Ryan Dahl
parent 0dc564a2aa
commit bb15c0e268

View File

@ -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_) {