]> git.sesse.net Git - vlc/blobdiff - src/misc/mtime.c
Fix memleak in lua module.
[vlc] / src / misc / mtime.c
index c2c19151cbfe651aedb26bea9d231da20fa701e3..9966e473bec8f5a046fcf1b92ccbb132331bba48 100644 (file)
@@ -33,7 +33,7 @@
 # include "config.h"
 #endif
 
-#include <vlc/vlc.h>
+#include <vlc_common.h>
 
 #include <time.h>                      /* clock_gettime(), clock_nanosleep() */
 #include <assert.h>
@@ -144,6 +144,19 @@ char *secstotimestr( char *psz_buffer, int i_seconds )
     return( psz_buffer );
 }
 
+#if defined (HAVE_CLOCK_NANOSLEEP)
+static unsigned prec = 0;
+
+static void mprec_once( void )
+{
+    struct timespec ts;
+    if( clock_getres( CLOCK_MONOTONIC, &ts ))
+        clock_getres( CLOCK_REALTIME, &ts );
+
+    prec = ts.tv_nsec / 1000;
+}
+#endif
+
 /**
  * Return a value that is no bigger than the clock precision
  * (possibly zero).
@@ -151,18 +164,14 @@ char *secstotimestr( char *psz_buffer, int i_seconds )
 static inline unsigned mprec( void )
 {
 #if defined (HAVE_CLOCK_NANOSLEEP)
-    struct timespec ts;
-    if( clock_getres( CLOCK_MONOTONIC, &ts ))
-        clock_getres( CLOCK_REALTIME, &ts );
-
-    return ts.tv_nsec / 1000;
-#endif
+    static pthread_once_t once = PTHREAD_ONCE_INIT;
+    pthread_once( &once, mprec_once );
+    return prec;
+#else
     return 0;
+#endif
 }
 
-static unsigned prec = 0;
-static volatile mtime_t cached_time = 0;
-
 /**
  * Return high precision date
  *
@@ -302,7 +311,7 @@ mtime_t mdate( void )
     res = (mtime_t) tv_date.tv_sec * 1000000 + (mtime_t) tv_date.tv_usec;
 #endif
 
-    return cached_time = res;
+    return res;
 }
 
 /**
@@ -315,15 +324,11 @@ mtime_t mdate( void )
  */
 void mwait( mtime_t date )
 {
-    if( prec == 0 )
-        prec = mprec();
-
     /* If the deadline is already elapsed, or within the clock precision,
-     * do not even bother the clock. */
-    if( ( date - cached_time ) < (mtime_t)prec ) // OK: mtime_t is signed
-        return;
+     * do not even bother the system timer. */
+    date -= mprec();
 
-#if 0 && defined (HAVE_CLOCK_NANOSLEEP)
+#if defined (HAVE_CLOCK_NANOSLEEP)
     lldiv_t d = lldiv( date, 1000000 );
     struct timespec ts = { d.quot, d.rem * 1000 };
 
@@ -352,8 +357,6 @@ void mwait( mtime_t date )
  */
 void msleep( mtime_t delay )
 {
-    mtime_t earlier = cached_time;
-
 #if defined( HAVE_CLOCK_NANOSLEEP )
     lldiv_t d = lldiv( delay, 1000000 );
     struct timespec ts = { d.quot, d.rem * 1000 };
@@ -390,10 +393,6 @@ void msleep( mtime_t delay )
      * or clock_nanosleep() if this is an issue. */
     select( 0, NULL, NULL, NULL, &tv_delay );
 #endif
-
-    earlier += delay;
-    if( cached_time < earlier )
-        cached_time = earlier;
 }
 
 /*