Tcl Source Code

Artifact [a24422d93a]
Login

Artifact a24422d93a2e8786ea892a7e9868dc91cc4b77d9:

Attachment "tclWinTime.c.diff" to ticket [1353858fff] added by matt-newman 2005-11-11 18:32:44.
Index: win/tclWinTime.c
===================================================================
RCS file: /cvs/kit/tcl/win/tclWinTime.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -b -w -r1.1 -r1.2
--- win/tclWinTime.c	2005/03/30 05:31:14	1.1
+++ win/tclWinTime.c	2005/09/14 13:10:04	1.2
@@ -13,6 +13,7 @@
  */
 
 #include "tclWinInt.h"
+#include "mmsystem.h"
 
 #define SECSPERDAY (60L * 60L * 24L)
 #define SECSPERYEAR (SECSPERDAY * 365L)
@@ -265,7 +266,56 @@
     int useFtime = 1;		/* Flag == TRUE if we need to fall back
 				 * on ftime rather than using the perf
 				 * counter */
+#if !defined(USE_PERF_COUNTERS)
+    /*
+     * Use the multi-media timer support in winmm.lib
+     */
+
+    if ( !timeInfo.initialized ) { 
+	TclpInitLock();
+	if ( !timeInfo.initialized ) {
+	    TIMECAPS tc;
+
+	    InitializeCriticalSection( &timeInfo.cs );
+
+	    timeGetDevCaps(&tc, sizeof(tc));
+	    timeInfo.perfCounterAvailable =
+		(timeBeginPeriod(tc.wPeriodMin) == TIMERR_NOERROR);
+	    timeInfo.fileTimeLastCall.QuadPart = 0;
+	    timeInfo.initialized = TRUE;
+	}
+	TclpInitUnlock();
+    }
+    if (timeInfo.perfCounterAvailable) {
+	DWORD now = timeGetTime();
+
+	EnterCriticalSection( &timeInfo.cs );
+
+	/*
+	 * if timer has wrapped or we have done this more that 100 times
+	 * resample. BUG: if Tcl sleeps for 49 days this will go wrong
+	 * for a second before re-syncing
+	 */
+	if (timeInfo.fileTimeLastCall.QuadPart == 0 ||
+	    (now - timeInfo.perfCounterLastCall.QuadPart) < 0 ||
+	    (now - timeInfo.perfCounterLastCall.QuadPart) > 1000) {
+	    FILETIME curFileTime;
 
+	    GetSystemTimeAsFileTime( &curFileTime );
+	    timeInfo.perfCounterLastCall.QuadPart = now;
+	    timeInfo.fileTimeLastCall.LowPart  = curFileTime.dwLowDateTime;
+	    timeInfo.fileTimeLastCall.HighPart = curFileTime.dwHighDateTime;
+	} else {
+	    /* increment last fileTime by tick delta */
+	    timeInfo.fileTimeLastCall.QuadPart += 10000*(now - timeInfo.perfCounterLastCall.QuadPart);
+	    timeInfo.perfCounterLastCall.QuadPart = now;
+	}
+	timePtr->sec  = (time_t)((timeInfo.fileTimeLastCall.QuadPart - 116444736000000000i64) / 10000000);
+	timePtr->usec = (unsigned long )((timeInfo.fileTimeLastCall.QuadPart/10) % 1000000);
+	LeaveCriticalSection( &timeInfo.cs );
+	useFtime = 0;
+    }
+#else
     /* Initialize static storage on the first trip through. */
 
     /*
@@ -429,7 +479,7 @@
 
 	LeaveCriticalSection( &timeInfo.cs );
     }
-
+#endif
     if ( useFtime ) {
 	/* High resolution timer is not available.  Just use ftime */
 
Index: win/makefile.vc
===================================================================
RCS file: /cvs/kit/tcl/win/makefile.vc,v
retrieving revision 1.1
retrieving revision 1.3
diff -u -d -b -w -r1.1 -r1.3
--- win/makefile.vc	2005/06/29 16:40:54	1.1
+++ win/makefile.vc	2005/09/14 13:10:04	1.3
@@ -398,7 +398,7 @@
 conlflags = $(lflags) -subsystem:console
 guilflags = $(lflags) -subsystem:windows
 
-baselibs   = kernel32.lib advapi32.lib user32.lib
+baselibs   = kernel32.lib advapi32.lib user32.lib winmm.lib
 
 
 #---------------------------------------------------------------------