[vlc-devel] commit: mprec: thread-safety ( Rémi Denis-Courmont )

git version control git at videolan.org
Mon May 19 19:41:25 CEST 2008


vlc | branch: master | Rémi Denis-Courmont <rem at videolan.org> | Mon May 19 20:35:41 2008 +0300| [e2b38c968064a42fc31f82e7a0bb9a6d1d79d531]

mprec: thread-safety

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=e2b38c968064a42fc31f82e7a0bb9a6d1d79d531
---

 src/misc/mtime.c |   30 +++++++++++++++++++-----------
 1 files changed, 19 insertions(+), 11 deletions(-)

diff --git a/src/misc/mtime.c b/src/misc/mtime.c
index c2c1915..b7beef5 100644
--- a/src/misc/mtime.c
+++ b/src/misc/mtime.c
@@ -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,16 +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;
 
 /**
@@ -315,12 +326,9 @@ 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
+    if( ( date - cached_time ) < (mtime_t)mprec() ) // OK: mtime_t is signed
         return;
 
 #if 0 && defined (HAVE_CLOCK_NANOSLEEP)




More information about the vlc-devel mailing list