]> git.sesse.net Git - vlc/blobdiff - src/misc/mtime.c
* ./include/*, ./src/*: separated WIN32 #tests and UNDER_CE #tests, because
[vlc] / src / misc / mtime.c
index 9272a11ec15246b7500d05140073f82f93d97468..6dfa582c1ed2e0a15398e80f2f2db1c81975d61f 100644 (file)
@@ -2,7 +2,8 @@
  * mtime.c: high rezolution time management functions
  * Functions are prototyped in mtime.h.
  *****************************************************************************
- * Copyright (C) 1998, 1999, 2000 VideoLAN
+ * Copyright (C) 1998-2001 VideoLAN
+ * $Id: mtime.c,v 1.35 2002/11/11 14:39:12 sam Exp $
  *
  * Authors: Vincent Seguin <seguin@via.ecp.fr>
  *
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
-#include "defs.h"
-
 #include <stdio.h>                                              /* sprintf() */
-#include <unistd.h>                                              /* select() */
-#include <sys/time.h>
+
+#include <vlc/vlc.h>
+
+#if defined( PTH_INIT_IN_PTH_H )                                  /* GNU Pth */
+#   include <pth.h>
+#endif
+
+#ifdef HAVE_UNISTD_H
+#   include <unistd.h>                                           /* select() */
+#endif
 
 #ifdef HAVE_KERNEL_OS_H
-#include <kernel/OS.h>
+#   include <kernel/OS.h>
 #endif
 
-#include "config.h"
-#include "common.h"
-#include "mtime.h"
+#if defined( WIN32 ) || defined( UNDER_CE )
+#   include <windows.h>
+#else
+#   include <sys/time.h>
+#endif
+
+#if defined(HAVE_NANOSLEEP) && !defined(HAVE_STRUCT_TIMESPEC)
+struct timespec
+{
+    time_t  tv_sec;
+    int32_t tv_nsec;
+};
+#endif
+
+#if defined(HAVE_NANOSLEEP) && !defined(HAVE_DECL_NANOSLEEP)
+int nanosleep(struct timespec *, struct timespec *);
+#endif
 
 /*****************************************************************************
  * mstrtime: return a date in a readable format
  *****************************************************************************/
 char *mstrtime( char *psz_buffer, mtime_t date )
 {
+    static mtime_t ll1000 = 1000, ll60 = 60, ll24 = 24;
+
     sprintf( psz_buffer, "%02d:%02d:%02d-%03d.%03d",
-             (int) (date / (1000LL * 1000LL * 60LL * 60LL) % 24LL),
-             (int) (date / (1000LL * 1000LL * 60LL) % 60LL),
-             (int) (date / (1000LL * 1000LL) % 60LL),
-             (int) (date / 1000LL % 1000LL),
-             (int) (date % 1000LL) );
+             (int) (date / (ll1000 * ll1000 * ll60 * ll60) % ll24),
+             (int) (date / (ll1000 * ll1000 * ll60) % ll60),
+             (int) (date / (ll1000 * ll1000) % ll60),
+             (int) (date / ll1000 % ll1000),
+             (int) (date % ll1000) );
     return( psz_buffer );
 }
 
 /*****************************************************************************
- * mdate: return high precision date (inline function)
+ * mdate: return high precision date
  *****************************************************************************
  * Uses the gettimeofday() function when possible (1 MHz resolution) or the
  * ftime() function (1 kHz resolution).
  *****************************************************************************/
 mtime_t mdate( void )
 {
-#ifdef HAVE_KERNEL_OS_H
+#if defined( HAVE_KERNEL_OS_H )
     return( real_time_clock_usecs() );
-    
+
+#elif defined( WIN32 ) || defined( UNDER_CE )
+    /* 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 ) )
+    {
+        /* Microsecond resolution */
+        QueryPerformanceCounter( (LARGE_INTEGER *)&usec_time );
+        return ( usec_time * 1000000 ) / freq;
+    }
+
+    /* Milisecond resolution */
+    return 1000 * GetTickCount();
+
 #else
     struct timeval tv_date;
 
@@ -80,12 +119,12 @@ 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
 }
 
 /*****************************************************************************
- * mwait: wait for a date (inline function)
+ * mwait: wait for a date
  *****************************************************************************
  * This function uses select() and an system date function to wake up at a
  * precise date. It should be used for process synchronization. If current date
@@ -93,8 +132,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();
@@ -103,64 +141,106 @@ void mwait( mtime_t date )
         return;
     }
     snooze( delay );
-#else
 
-#ifdef HAVE_USLEEP
-    struct timeval tv_date;
+#elif defined( WIN32 ) || defined( UNDER_CE )
+    mtime_t usec_time, delay;
+
+    usec_time = mdate();
+    delay = date - usec_time;
+    if( delay <= 0 )
+    {
+        return;
+    }
+    msleep( delay );
 
 #else
-    struct timeval tv_date, tv_delay;
 
-#endif
+    struct timeval tv_date;
     mtime_t        delay;          /* delay in msec, signed to detect errors */
 
     /* see mdate() about gettimeofday() possible errors */
     gettimeofday( &tv_date, NULL );
 
     /* calculate delay and check if current date is before wished date */
-    delay = date - (mtime_t) tv_date.tv_sec * 1000000 - (mtime_t) tv_date.tv_usec;
+    delay = date - (mtime_t) tv_date.tv_sec * 1000000
+                 - (mtime_t) tv_date.tv_usec
+                 - 10000;
+
+    /* Linux/i386 has a granularity of 10 ms. It's better to be in advance
+     * than to be late. */
     if( delay <= 0 )                 /* wished date is now or already passed */
     {
         return;
     }
 
-#ifdef HAVE_USLEEP
-    usleep( delay );
-#else
-    tv_delay.tv_sec = delay / 1000000;
-    tv_delay.tv_usec = delay % 1000000;
+#   if defined( PTH_INIT_IN_PTH_H )
+    pth_usleep( delay );
 
+#   elif defined( ST_INIT_IN_ST_H )
+    st_usleep( delay );
+
+#   else
+
+#       if defined( HAVE_NANOSLEEP )
+    {
+        struct timespec ts_delay;
+        ts_delay.tv_sec = delay / 1000000;
+        ts_delay.tv_nsec = (delay % 1000000) * 1000;
+
+        nanosleep( &ts_delay, NULL );
+    }
+
+#       else
+    tv_date.tv_sec = delay / 1000000;
+    tv_date.tv_usec = delay % 1000000;
     /* see msleep() about select() errors */
-    select( 0, NULL, NULL, NULL, &tv_delay );
-#endif
+    select( 0, NULL, NULL, NULL, &tv_date );
+#       endif
+
+#   endif
 
-#endif /* HAVE_KERNEL_OS_H */
+#endif
 }
 
 /*****************************************************************************
- * msleep: more precise sleep() (inline function)                        (ok ?)
+ * msleep: more precise sleep()
  *****************************************************************************
  * Portable usleep() function.
  *****************************************************************************/
 void msleep( mtime_t delay )
 {
-#ifdef HAVE_KERNEL_OS_H
+#if defined( HAVE_KERNEL_OS_H )
     snooze( delay );
-#else
 
-#ifdef HAVE_USLEEP
-    usleep( delay );
+#elif defined( PTH_INIT_IN_PTH_H )
+    pth_usleep( delay );
+
+#elif defined( ST_INIT_IN_ST_H )
+    st_usleep( delay );
+
+#elif defined( WIN32 ) || defined( UNDER_CE )
+    Sleep( (int) (delay / 1000) );
+
+#elif defined( HAVE_NANOSLEEP )
+    struct timespec ts_delay;
+
+    ts_delay.tv_sec = delay / 1000000;
+    ts_delay.tv_nsec = (delay % 1000000) * 1000;
+
+    nanosleep( &ts_delay, NULL );
+
 #else
     struct timeval tv_delay;
 
     tv_delay.tv_sec = delay / 1000000;
     tv_delay.tv_usec = delay % 1000000;
+
     /* select() return value should be tested, since several possible errors
      * can occur. However, they should only happen in very particular occasions
      * (i.e. when a signal is sent to the thread, or when memory is full), and
-     * can be ingnored. */
+     * can be ignored. */
     select( 0, NULL, NULL, NULL, &tv_delay );
-#endif
 
-#endif /* HAVE_KERNEL_OS_H */
+#endif
 }
+