* Functions are prototyped in mtime.h.
*****************************************************************************
* Copyright (C) 1998, 1999, 2000 VideoLAN
- * $Id: mtime.c,v 1.16 2001/03/21 13:42:34 sam Exp $
+ * $Id: mtime.c,v 1.17 2001/04/28 03:36:25 sam Exp $
*
* Authors: Vincent Seguin <seguin@via.ecp.fr>
*
#include <kernel/OS.h>
#endif
+#ifdef WIN32
+#include <windows.h>
+#endif
+
#include "config.h"
#include "common.h"
#include "mtime.h"
*****************************************************************************/
mtime_t mdate( void )
{
-#ifdef HAVE_KERNEL_OS_H
+#if defined( HAVE_KERNEL_OS_H )
return( real_time_clock_usecs() );
-
+
+#elif defined( WIN32 )
+ /* We don't get the real date, just the value of a high precision timer.
+ * this is because the usual time functions have at best only a milisecond
+ * resolution */
+ mtime_t freq,usec_time;
+
+ if( !QueryPerformanceFrequency((LARGE_INTEGER *)&freq) )
+ {
+ /* Milisecond resolution */
+ FILETIME file_time;
+ GetSystemTimeAsFileTime((FILETIME *)&file_time);
+ usec_time *= 1000;
+ }
+ else
+ {
+ /* Microsecond resolution */
+ QueryPerformanceCounter((LARGE_INTEGER *)&usec_time);
+ usec_time /= (freq/1000000);
+ }
+ return( usec_time );
+
#else
struct timeval tv_date;
* here, since tv is a local variable. */
gettimeofday( &tv_date, NULL );
return( (mtime_t) tv_date.tv_sec * 1000000 + (mtime_t) tv_date.tv_usec );
-
+
#endif
}
*****************************************************************************/
void mwait( mtime_t date )
{
-#ifdef HAVE_KERNEL_OS_H
-
+#if defined( HAVE_KERNEL_OS_H )
mtime_t delay;
delay = date - real_time_clock_usecs();
return;
}
snooze( delay );
-#else
-#ifdef HAVE_USLEEP
- struct timeval tv_date;
+#elif defined( WIN32 )
+ mtime_t usec_time, delay;
+
+ usec_time = mdate();
+ delay = date - usec_time;
+ if( delay <= 0 )
+ {
+ return;
+ }
+ /* Sleep only has milisecond resolution */
+ Sleep( (DWORD)(delay/1000) );
#else
- struct timeval tv_date, tv_delay;
-#endif
+# ifdef HAVE_USLEEP
+ struct timeval tv_date;
+# else
+ struct timeval tv_date, tv_delay;
+# endif
mtime_t delay; /* delay in msec, signed to detect errors */
/* see mdate() about gettimeofday() possible errors */
return;
}
-#ifdef HAVE_USLEEP
+# ifdef HAVE_USLEEP
usleep( delay );
-#else
+# else
tv_delay.tv_sec = delay / 1000000;
tv_delay.tv_usec = delay % 1000000;
/* see msleep() about select() errors */
select( 0, NULL, NULL, NULL, &tv_delay );
-#endif
+# endif
-#endif /* HAVE_KERNEL_OS_H */
+#endif
}
/*****************************************************************************
*****************************************************************************/
void msleep( mtime_t delay )
{
-#ifdef HAVE_KERNEL_OS_H
+#if defined( HAVE_KERNEL_OS_H )
snooze( delay );
-#else
-#ifdef HAVE_USLEEP
+#elif defined( WIN32 )
+ Sleep( delay/1000 ); /* Sleep only has milisecond resolution */
+ /* Maybe we could use the multimedia timer to reach the right resolution, */
+ /* or the old Winsock select() function ?*/
+
+#elif defined( HAVE_USLEEP )
usleep( delay );
+
#else
struct timeval tv_delay;
* (i.e. when a signal is sent to the thread, or when memory is full), and
* can be ingnored. */
select( 0, NULL, NULL, NULL, &tv_delay );
-#endif
-#endif /* HAVE_KERNEL_OS_H */
+#endif
}
+