From d21fc6c333c383fb2fbec1f2fc587d459c4ea47c Mon Sep 17 00:00:00 2001 From: Bert Belder Date: Sat, 2 Jun 2012 20:37:08 +0200 Subject: [PATCH] windows: move uv_hrtime() to util.c * It has nothing to do with timer handles, so it doesn't belong in timer.c * uv_hrtime_init() was merged into uv__util_init() --- src/win/timer.c | 46 ---------------------------------------------- src/win/util.c | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 46 deletions(-) diff --git a/src/win/timer.c b/src/win/timer.c index e71e4c65..810960c6 100644 --- a/src/win/timer.c +++ b/src/win/timer.c @@ -27,15 +27,6 @@ #include "tree.h" -#undef NANOSEC -#define NANOSEC 1000000000 - - -/* The resolution of the high-resolution clock. */ -static uint64_t uv_hrtime_frequency_ = 0; -static uv_once_t uv_hrtime_init_guard_ = UV_ONCE_INIT; - - void uv_update_time(uv_loop_t* loop) { DWORD ticks = GetTickCount(); @@ -58,43 +49,6 @@ int64_t uv_now(uv_loop_t* loop) { } -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; - - uv_once(&uv_hrtime_init_guard_, uv_hrtime_init); - - /* If the performance frequency is zero, there's no support. */ - if (!uv_hrtime_frequency_) { - /* uv__set_sys_error(loop, ERROR_NOT_SUPPORTED); */ - return 0; - } - - if (!QueryPerformanceCounter(&counter)) { - /* uv__set_sys_error(loop, GetLastError()); */ - return 0; - } - - /* Because we have no guarantee about the order of magnitude of the */ - /* performance counter frequency, and there may not be much headroom to */ - /* multiply by NANOSEC without overflowing, we use 128-bit math instead. */ - return ((uint64_t) counter.LowPart * NANOSEC / uv_hrtime_frequency_) + - (((uint64_t) counter.HighPart * NANOSEC / uv_hrtime_frequency_) - << 32); -} - - static int uv_timer_compare(uv_timer_t* a, uv_timer_t* b) { if (a->due < b->due) return -1; diff --git a/src/win/util.c b/src/win/util.c index 3e20e79c..ad9e574a 100644 --- a/src/win/util.c +++ b/src/win/util.c @@ -47,11 +47,18 @@ */ #define MAX_TITLE_LENGTH 8192 +/* The number of nanoseconds in one second. */ +#undef NANOSEC +#define NANOSEC 1000000000 + /* Cached copy of the process title, plus a mutex guarding it. */ static char *process_title; static CRITICAL_SECTION process_title_lock; +/* The tick frequency of the high-resolution clock. */ +static uint64_t hrtime_frequency_ = 0; + /* * One-time intialization code for functionality defined in util.c. @@ -59,6 +66,10 @@ static CRITICAL_SECTION process_title_lock; void uv__util_init() { /* Initialize process title access mutex. */ InitializeCriticalSection(&process_title_lock); + + /* Retrieve high-resolution timer frequency. */ + if (!QueryPerformanceFrequency((LARGE_INTEGER*) &hrtime_frequency_)) + hrtime_frequency_ = 0; } @@ -375,6 +386,31 @@ uv_err_t uv_get_process_title(char* buffer, size_t size) { } +uint64_t uv_hrtime(void) { + LARGE_INTEGER counter; + + uv__once_init(); + + /* If the performance frequency is zero, there's no support. */ + if (!hrtime_frequency_) { + /* uv__set_sys_error(loop, ERROR_NOT_SUPPORTED); */ + return 0; + } + + if (!QueryPerformanceCounter(&counter)) { + /* uv__set_sys_error(loop, GetLastError()); */ + return 0; + } + + /* Because we have no guarantee about the order of magnitude of the */ + /* performance counter frequency, and there may not be much headroom to */ + /* multiply by NANOSEC without overflowing, we use 128-bit math instead. */ + return ((uint64_t) counter.LowPart * NANOSEC / hrtime_frequency_) + + (((uint64_t) counter.HighPart * NANOSEC / hrtime_frequency_) + << 32); +} + + uv_err_t uv_resident_set_memory(size_t* rss) { HANDLE current_process; PROCESS_MEMORY_COUNTERS pmc;