/* gettimeofday.c - Don Yang (uguu.org) 10/02/06 */ #include #include #include"gettimeofday.h" #pragma warning(disable:4100) #ifdef USE_QUERY_PERFORMANCE_COUNTER static LARGE_INTEGER ClockFrequency = { 0LL }; /* Emulate gettimeofday using QueryPerformanceFrequency. This version appears to be unstable on multiprocessor or variable clock rate machines. */ int gettimeofday(/*@out@*//*@null@*/struct timeval *tv, /*@unused@*//*@null@*/void *tz) { LARGE_INTEGER current_time; if( ClockFrequency.QuadPart == 0LL ) { if( !QueryPerformanceFrequency(&ClockFrequency) ) { (void)puts("High resolution clock not supported!"); exit(EXIT_FAILURE); } if( ClockFrequency.QuadPart == 0LL ) { (void)puts("High resolution clock not supported!"); exit(EXIT_FAILURE); } } if( !QueryPerformanceCounter(¤t_time) ) { (void)puts("Error querying counters!"); exit(EXIT_FAILURE); } current_time.QuadPart = (1000000ULL * current_time.QuadPart) / ClockFrequency.QuadPart; tv->tv_sec = (long)(current_time.QuadPart / 1000000ULL); tv->tv_usec = (long)(current_time.QuadPart % 1000000ULL); return 0; } #else /* timeGetTime is less accurate, and introduces dependency on winmm.lib (which GLUT already uses anyways), but seems generally more reliable. */ int gettimeofday(/*@out@*//*@null@*/struct timeval *tv, /*@unused@*//*@null@*/void *tz) { DWORD t = timeGetTime(); tv->tv_sec = t / 1000; tv->tv_usec = (t % 1000) * 1000; return 0; } #endif