#1 Check Windows build version as to whether Sleep(*0ms*) yields or sleeps. #2 Use GetSystemTimeAsFileTime() instead of GetSystemtime().
I don't know if SdkDdkver.h needs to be explicitly brought in, the WINVERand _WIN32_WINNT symbols are broken for detecting Windows Server 2003. http://msdn.microsoft.com/en-us/library/aa383745.aspx Noting that zclock_log isn't very useful with buffered output. Also it uses three expensive output calls which need to be reduced if you ever direct to disk whilst HDDs remain popular. -- Steve-o
From 6840414f623fac5b33176812fc248de3456d1eaa Mon Sep 17 00:00:00 2001 From: Steven McCoy <[email protected]> Date: Thu, 26 May 2011 18:28:21 -0400 Subject: [PATCH 1/2] Avoid 0ms sleep as yield on Windows 2000/XP. Signed-off-by: Steven McCoy <[email protected]> --- src/zclock.c | 13 +++++++++++++ 1 files changed, 13 insertions(+), 0 deletions(-) diff --git a/src/zclock.c b/src/zclock.c index b19ba13..02f5c4a 100644 --- a/src/zclock.c +++ b/src/zclock.c @@ -36,6 +36,9 @@ are a concept we can deal with. Seconds are too fat, nanoseconds too tiny, but milliseconds are just right for slices of time we want to work with at the 0MQ scale. zclock doesn't give you objects to work with, we like the czmq class model but we're not insane. There, got it in again. +The Win32 Sleep() call defaults to 16ms resolution unless the system timer +resolution is increased with a call to timeBeginPeriod() permitting 1ms +granularity. @end */ @@ -55,7 +58,17 @@ zclock_sleep (int msecs) t.tv_nsec = (msecs % 1000) * 1000000; nanosleep (&t, NULL); #elif (defined (__WINDOWS__)) +// Windows XP/2000: A value of zero causes the thread to relinquish the +// remainder of its time slice to any other thread of equal priority that is +// ready to run. If there are no other threads of equal priority ready to run, +// the function returns immediately, and the thread continues execution. This +// behavior changed starting with Windows Server 2003. +#if defined (NTDDI_VERSION) && defined (NTDDI_WS03) && (NTDDI_VERSION >= NTDDI_WS03) Sleep (msecs); +#else + if (msecs > 0) + Sleep (msecs); +#endif #endif } -- 1.7.4.1
From 2f8edde23b67245110186167d49c7b94d10b5ff1 Mon Sep 17 00:00:00 2001 From: Steven McCoy <[email protected]> Date: Thu, 26 May 2011 18:29:54 -0400 Subject: [PATCH 2/2] Use GetSystemTimeAsFileTime() instead of GetSystemTime(). Signed-off-by: Steven McCoy <[email protected]> --- src/zclock.c | 19 ++++++++++++++++--- 1 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/zclock.c b/src/zclock.c index 02f5c4a..9fd4400 100644 --- a/src/zclock.c +++ b/src/zclock.c @@ -46,6 +46,19 @@ granularity. #include "../include/zclock.h" +#if defined (__WINDOWS__) +// -------------------------------------------------------------------------- +// Convert FILETIME "Contains a 64-bit value representing the number of +// 100-nanosecond intervals since January 1, 1601 (UTC)." + +static int64_t FileTimeToMilliseconds (const FILETIME& ft) +{ + int64_t t; + memcpy (&t, &ft, sizeof (ft)); + return (int64_t) (t / 100); +} +#endif + // -------------------------------------------------------------------------- // Sleep for a number of milliseconds @@ -84,9 +97,9 @@ zclock_time (void) gettimeofday (&tv, NULL); return (int64_t) (tv.tv_sec * 1000 + tv.tv_usec / 1000); #elif (defined (__WINDOWS__)) - SYSTEMTIME st; - GetSystemTime (&st); - return (int64_t) st.wSecond * 1000 + st.wMilliseconds; + FILETIME ft; + GetSystemTimeAsFileTime (&ft); + return FileTimeToMilliseconds (ft); #endif } -- 1.7.4.1
_______________________________________________ zeromq-dev mailing list [email protected] http://lists.zeromq.org/mailman/listinfo/zeromq-dev
