]> git.sesse.net Git - vlc/blobdiff - src/misc/mtime.c
* Coding style fixes here and there.
[vlc] / src / misc / mtime.c
index 79a0331c4f9259757cb0e16e8202190c40a59d2c..5b02400d9ff349554ccf1533088a1e336d49ccb2 100644 (file)
@@ -3,7 +3,7 @@
  * 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"
@@ -70,9 +74,30 @@ char *mstrtime( char *psz_buffer, mtime_t date )
  *****************************************************************************/
 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;
 
@@ -81,7 +106,7 @@ mtime_t mdate( void )
      * 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
 }
 
@@ -94,8 +119,7 @@ mtime_t mdate( void )
  *****************************************************************************/
 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();
@@ -104,15 +128,26 @@ void mwait( mtime_t date )
         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 */
@@ -127,17 +162,17 @@ void mwait( mtime_t date )
         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
 }
 
 /*****************************************************************************
@@ -147,12 +182,17 @@ void mwait( mtime_t date )
  *****************************************************************************/
 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;
 
@@ -163,7 +203,7 @@ void msleep( mtime_t 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
 }
+