[vlc-commits] posix: remove ancient run-time fallback to real-time clock

Rémi Denis-Courmont git at videolan.org
Wed Apr 18 19:40:51 CEST 2018


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Wed Apr 18 20:23:57 2018 +0300| [951ceb9e13d6f89a3bd20402f9604b53bc931725] | committer: Rémi Denis-Courmont

posix: remove ancient run-time fallback to real-time clock

For hysterical raisins, GNU/Linux and possibly some other OSes still
report that monotonic clock must be checked at run-time, although I
doubt that VLC or even current glibc would run on such old kernel.

Drop that to simplify and avoid the systematic one-time init check.

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

 src/posix/thread.c | 96 +++++++-----------------------------------------------
 1 file changed, 11 insertions(+), 85 deletions(-)

diff --git a/src/posix/thread.c b/src/posix/thread.c
index e849abc5bb..3002949774 100644
--- a/src/posix/thread.c
+++ b/src/posix/thread.c
@@ -51,62 +51,16 @@
 # include <sys/pset.h>
 #endif
 
-#if !defined (_POSIX_TIMERS)
-# define _POSIX_TIMERS (-1)
-#endif
-#if !defined (_POSIX_CLOCK_SELECTION)
-/* Clock selection was defined in 2001 and became mandatory in 2008. */
-# define _POSIX_CLOCK_SELECTION (-1)
-#endif
-#if !defined (_POSIX_MONOTONIC_CLOCK)
-# define _POSIX_MONOTONIC_CLOCK (-1)
-#endif
-
-#if (_POSIX_TIMERS > 0)
 static unsigned vlc_clock_prec;
 
-# if (_POSIX_MONOTONIC_CLOCK > 0) && (_POSIX_CLOCK_SELECTION > 0)
-/* Compile-time POSIX monotonic clock support */
-#  define vlc_clock_id (CLOCK_MONOTONIC)
-
-# elif (_POSIX_MONOTONIC_CLOCK == 0) && (_POSIX_CLOCK_SELECTION > 0)
-/* Run-time POSIX monotonic clock support (see clock_setup() below) */
-static clockid_t vlc_clock_id;
-
-# else
-/* No POSIX monotonic clock support */
-#   define vlc_clock_id (CLOCK_REALTIME)
-#   warning Monotonic clock not available. Expect timing issues.
-
-# endif /* _POSIX_MONOTONIC_CLOKC */
-
 static void vlc_clock_setup_once (void)
 {
-# if (_POSIX_MONOTONIC_CLOCK == 0)
-    long val = sysconf (_SC_MONOTONIC_CLOCK);
-    assert (val != 0);
-    vlc_clock_id = (val < 0) ? CLOCK_REALTIME : CLOCK_MONOTONIC;
-# endif
-
     struct timespec res;
-    if (unlikely(clock_getres (vlc_clock_id, &res) != 0 || res.tv_sec != 0))
+    if (unlikely(clock_getres(CLOCK_MONOTONIC, &res) != 0 || res.tv_sec != 0))
         abort ();
     vlc_clock_prec = (res.tv_nsec + 500) / 1000;
 }
 
-static pthread_once_t vlc_clock_once = PTHREAD_ONCE_INIT;
-
-# define vlc_clock_setup() \
-    pthread_once(&vlc_clock_once, vlc_clock_setup_once)
-
-#else /* _POSIX_TIMERS */
-
-# include <sys/time.h> /* gettimeofday() */
-
-# define vlc_clock_setup() (void)0
-# warning Monotonic clock not available. Expect timing issues.
-#endif /* _POSIX_TIMERS */
-
 static struct timespec mtime_to_ts (mtime_t date)
 {
     lldiv_t d = lldiv (date, CLOCK_FREQ);
@@ -233,14 +187,11 @@ void vlc_cond_init (vlc_cond_t *p_condvar)
 {
     pthread_condattr_t attr;
 
-    if (unlikely(pthread_condattr_init (&attr)))
-        abort ();
-#if (_POSIX_CLOCK_SELECTION > 0)
-    vlc_clock_setup ();
-    pthread_condattr_setclock (&attr, vlc_clock_id);
-#endif
-    if (unlikely(pthread_cond_init (p_condvar, &attr)))
+    if (unlikely(pthread_condattr_init (&attr))
+     || unlikely(pthread_condattr_setclock(&attr, CLOCK_MONOTONIC))
+     || unlikely(pthread_cond_init (p_condvar, &attr)))
         abort ();
+
     pthread_condattr_destroy (&attr);
 }
 
@@ -629,44 +580,27 @@ void vlc_control_cancel (int cmd, ...)
 
 mtime_t mdate (void)
 {
-#if (_POSIX_TIMERS > 0)
     struct timespec ts;
 
-    vlc_clock_setup ();
-    if (unlikely(clock_gettime (vlc_clock_id, &ts) != 0))
+    if (unlikely(clock_gettime(CLOCK_MONOTONIC, &ts) != 0))
         abort ();
 
     return (INT64_C(1000000) * ts.tv_sec) + (ts.tv_nsec / 1000);
-
-#else
-    struct timeval tv;
-
-    if (unlikely(gettimeofday (&tv, NULL) != 0))
-        abort ();
-    return (INT64_C(1000000) * tv.tv_sec) + tv.tv_usec;
-
-#endif
 }
 
 #undef mwait
 void mwait (mtime_t deadline)
 {
-#if (_POSIX_CLOCK_SELECTION > 0)
-    vlc_clock_setup ();
+    static pthread_once_t vlc_clock_once = PTHREAD_ONCE_INIT;
+
     /* If the deadline is already elapsed, or within the clock precision,
      * do not even bother the system timer. */
+    pthread_once(&vlc_clock_once, vlc_clock_setup_once);
     deadline -= vlc_clock_prec;
 
     struct timespec ts = mtime_to_ts (deadline);
 
-    while (clock_nanosleep (vlc_clock_id, TIMER_ABSTIME, &ts, NULL) == EINTR);
-
-#else
-    deadline -= mdate ();
-    if (deadline > 0)
-        msleep (deadline);
-
-#endif
+    while (clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &ts, NULL) == EINTR);
 }
 
 #undef msleep
@@ -674,15 +608,7 @@ void msleep (mtime_t delay)
 {
     struct timespec ts = mtime_to_ts (delay);
 
-#if (_POSIX_CLOCK_SELECTION > 0)
-    vlc_clock_setup ();
-    while (clock_nanosleep (vlc_clock_id, 0, &ts, &ts) == EINTR);
-
-#else
-    while (nanosleep (&ts, &ts) == -1)
-        assert (errno == EINTR);
-
-#endif
+    while (clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, &ts) == EINTR);
 }
 
 unsigned vlc_GetCPUCount(void)



More information about the vlc-commits mailing list