+
+#elif defined( WIN32 ) || defined( UNDER_CE )
+ /* We don't need the real date, just the value of a high precision timer */
+ static mtime_t freq = I64C(-1);
+ mtime_t usec_time;
+
+ if( freq == I64C(-1) )
+ {
+ /* Extract from the Tcl source code:
+ * (http://www.cs.man.ac.uk/fellowsd-bin/TIP/7.html)
+ *
+ * Some hardware abstraction layers use the CPU clock
+ * in place of the real-time clock as a performance counter
+ * reference. This results in:
+ * - inconsistent results among the processors on
+ * multi-processor systems.
+ * - unpredictable changes in performance counter frequency
+ * on "gearshift" processors such as Transmeta and
+ * SpeedStep.
+ * There seems to be no way to test whether the performance
+ * counter is reliable, but a useful heuristic is that
+ * if its frequency is 1.193182 MHz or 3.579545 MHz, it's
+ * derived from a colorburst crystal and is therefore
+ * the RTC rather than the TSC. If it's anything else, we
+ * presume that the performance counter is unreliable.
+ */
+
+ freq = ( QueryPerformanceFrequency( (LARGE_INTEGER *)&freq ) &&
+ (freq == I64C(1193182) || freq == I64C(3579545) ) )
+ ? freq : 0;
+ }
+
+ if( freq != 0 )
+ {
+ /* Microsecond resolution */
+ QueryPerformanceCounter( (LARGE_INTEGER *)&usec_time );
+ return ( usec_time * 1000000 ) / freq;
+ }
+ else
+ {
+ /* Fallback on GetTickCount() which has a milisecond resolution
+ * (actually, best case is about 10 ms resolution)
+ * GetTickCount() only returns a DWORD thus will wrap after
+ * about 49.7 days so we try to detect the wrapping. */
+
+ static CRITICAL_SECTION date_lock;
+ static mtime_t i_previous_time = I64C(-1);
+ static int i_wrap_counts = -1;
+
+ if( i_wrap_counts == -1 )
+ {
+ /* Initialization */
+ i_previous_time = I64C(1000) * GetTickCount();
+ InitializeCriticalSection( &date_lock );
+ i_wrap_counts = 0;
+ }
+
+ EnterCriticalSection( &date_lock );
+ usec_time = I64C(1000) *
+ (i_wrap_counts * I64C(0x100000000) + GetTickCount());
+ if( i_previous_time > usec_time )
+ {
+ /* Counter wrapped */
+ i_wrap_counts++;
+ usec_time += I64C(0x100000000000);
+ }
+ i_previous_time = usec_time;
+ LeaveCriticalSection( &date_lock );
+
+ return usec_time;
+ }
+