1From 5d561e1e2dcde3c9fca4d925f12447009d0d4a4c Mon Sep 17 00:00:00 2001
2From: =?UTF-8?q?R=C3=A9mi=20Denis-Courmont?= <remi@remlab.net>
3Date: Wed, 18 Apr 2018 17:23:57 +0300
4Subject: [PATCH] posix: remove ancient run-time fallback to real-time clock
5MIME-Version: 1.0
6Content-Type: text/plain; charset=UTF-8
7Content-Transfer-Encoding: 8bit
8
9posix: remove ancient run-time fallback to real-time clock
10
11For hysterical raisins, GNU/Linux and possibly some other OSes still
12report that monotonic clock must be checked at run-time, although I
13doubt that VLC or even current glibc would run on such old kernel.
14
15Drop that to simplify and avoid the systematic one-time init check.
16
17Downloaded from upstream commit to fix build error on m68k:
18
19posix/thread.c:79:5: warning: #warning Monotonic clock not available. Expect timing issues. [-Wcpp]
20 #   warning Monotonic clock not available. Expect timing issues.
21     ^~~~~~~
22posix/thread.c: In function ‘vlc_clock_setup_once’:
23posix/thread.c:88:18: error: lvalue required as left operand of assignment
24     vlc_clock_id = (val < 0) ? CLOCK_REALTIME : CLOCK_MONOTONIC;
25
26Signed-off-by: Bernd Kuhls <bernd.kuhls@t-online.de>
27---
28 src/posix/thread.c | 96 +++++++-----------------------------------------------
29 1 file changed, 11 insertions(+), 85 deletions(-)
30
31diff --git a/src/posix/thread.c b/src/posix/thread.c
32index dab8b71f97..8878941913 100644
33--- a/src/posix/thread.c
34+++ b/src/posix/thread.c
35@@ -51,62 +51,16 @@
36 # include <sys/pset.h>
37 #endif
38
39-#if !defined (_POSIX_TIMERS)
40-# define _POSIX_TIMERS (-1)
41-#endif
42-#if !defined (_POSIX_CLOCK_SELECTION)
43-/* Clock selection was defined in 2001 and became mandatory in 2008. */
44-# define _POSIX_CLOCK_SELECTION (-1)
45-#endif
46-#if !defined (_POSIX_MONOTONIC_CLOCK)
47-# define _POSIX_MONOTONIC_CLOCK (-1)
48-#endif
49-
50-#if (_POSIX_TIMERS > 0)
51 static unsigned vlc_clock_prec;
52
53-# if (_POSIX_MONOTONIC_CLOCK > 0) && (_POSIX_CLOCK_SELECTION > 0)
54-/* Compile-time POSIX monotonic clock support */
55-#  define vlc_clock_id (CLOCK_MONOTONIC)
56-
57-# elif (_POSIX_MONOTONIC_CLOCK == 0) && (_POSIX_CLOCK_SELECTION > 0)
58-/* Run-time POSIX monotonic clock support (see clock_setup() below) */
59-static clockid_t vlc_clock_id;
60-
61-# else
62-/* No POSIX monotonic clock support */
63-#   define vlc_clock_id (CLOCK_REALTIME)
64-#   warning Monotonic clock not available. Expect timing issues.
65-
66-# endif /* _POSIX_MONOTONIC_CLOKC */
67-
68 static void vlc_clock_setup_once (void)
69 {
70-# if (_POSIX_MONOTONIC_CLOCK == 0)
71-    long val = sysconf (_SC_MONOTONIC_CLOCK);
72-    assert (val != 0);
73-    vlc_clock_id = (val < 0) ? CLOCK_REALTIME : CLOCK_MONOTONIC;
74-# endif
75-
76     struct timespec res;
77-    if (unlikely(clock_getres (vlc_clock_id, &res) != 0 || res.tv_sec != 0))
78+    if (unlikely(clock_getres(CLOCK_MONOTONIC, &res) != 0 || res.tv_sec != 0))
79         abort ();
80     vlc_clock_prec = (res.tv_nsec + 500) / 1000;
81 }
82
83-static pthread_once_t vlc_clock_once = PTHREAD_ONCE_INIT;
84-
85-# define vlc_clock_setup() \
86-    pthread_once(&vlc_clock_once, vlc_clock_setup_once)
87-
88-#else /* _POSIX_TIMERS */
89-
90-# include <sys/time.h> /* gettimeofday() */
91-
92-# define vlc_clock_setup() (void)0
93-# warning Monotonic clock not available. Expect timing issues.
94-#endif /* _POSIX_TIMERS */
95-
96 static struct timespec mtime_to_ts (mtime_t date)
97 {
98     lldiv_t d = lldiv (date, CLOCK_FREQ);
99@@ -233,14 +187,11 @@ void vlc_cond_init (vlc_cond_t *p_condvar)
100 {
101     pthread_condattr_t attr;
102
103-    if (unlikely(pthread_condattr_init (&attr)))
104-        abort ();
105-#if (_POSIX_CLOCK_SELECTION > 0)
106-    vlc_clock_setup ();
107-    pthread_condattr_setclock (&attr, vlc_clock_id);
108-#endif
109-    if (unlikely(pthread_cond_init (p_condvar, &attr)))
110+    if (unlikely(pthread_condattr_init (&attr))
111+     || unlikely(pthread_condattr_setclock(&attr, CLOCK_MONOTONIC))
112+     || unlikely(pthread_cond_init (p_condvar, &attr)))
113         abort ();
114+
115     pthread_condattr_destroy (&attr);
116 }
117
118@@ -625,44 +576,27 @@ void vlc_control_cancel (int cmd, ...)
119
120 mtime_t mdate (void)
121 {
122-#if (_POSIX_TIMERS > 0)
123     struct timespec ts;
124
125-    vlc_clock_setup ();
126-    if (unlikely(clock_gettime (vlc_clock_id, &ts) != 0))
127+    if (unlikely(clock_gettime(CLOCK_MONOTONIC, &ts) != 0))
128         abort ();
129
130     return (INT64_C(1000000) * ts.tv_sec) + (ts.tv_nsec / 1000);
131-
132-#else
133-    struct timeval tv;
134-
135-    if (unlikely(gettimeofday (&tv, NULL) != 0))
136-        abort ();
137-    return (INT64_C(1000000) * tv.tv_sec) + tv.tv_usec;
138-
139-#endif
140 }
141
142 #undef mwait
143 void mwait (mtime_t deadline)
144 {
145-#if (_POSIX_CLOCK_SELECTION > 0)
146-    vlc_clock_setup ();
147+    static pthread_once_t vlc_clock_once = PTHREAD_ONCE_INIT;
148+
149     /* If the deadline is already elapsed, or within the clock precision,
150      * do not even bother the system timer. */
151+    pthread_once(&vlc_clock_once, vlc_clock_setup_once);
152     deadline -= vlc_clock_prec;
153
154     struct timespec ts = mtime_to_ts (deadline);
155
156-    while (clock_nanosleep (vlc_clock_id, TIMER_ABSTIME, &ts, NULL) == EINTR);
157-
158-#else
159-    deadline -= mdate ();
160-    if (deadline > 0)
161-        msleep (deadline);
162-
163-#endif
164+    while (clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &ts, NULL) == EINTR);
165 }
166
167 #undef msleep
168@@ -670,15 +604,7 @@ void msleep (mtime_t delay)
169 {
170     struct timespec ts = mtime_to_ts (delay);
171
172-#if (_POSIX_CLOCK_SELECTION > 0)
173-    vlc_clock_setup ();
174-    while (clock_nanosleep (vlc_clock_id, 0, &ts, &ts) == EINTR);
175-
176-#else
177-    while (nanosleep (&ts, &ts) == -1)
178-        assert (errno == EINTR);
179-
180-#endif
181+    while (clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, &ts) == EINTR);
182 }
183
184 unsigned vlc_GetCPUCount(void)
185--
1862.14.4
187
188