<html><head></head><body>It depends what you mean by "initialized". This is C, not C++. The static initializers are constants. It's always safe to initialize an object to a constant value, so long as you don't use it.<br><br>What this does is no different that initializing a condition variable in a calloc'ed or memset data structure.<br><br><div class="gmail_quote">Le 24 février 2020 10:57:30 GMT+02:00, Thomas Guillem <thomas@gllm.fr> a écrit :<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<pre class="k9mail"><br><br>On Sun, Feb 23, 2020, at 16:26, Rémi Denis-Courmont wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 1ex 0.8ex; border-left: 1px solid #729fcf; padding-left: 1ex;">This gets the earlier timed-wait behaviour back on NetBSD and other<br>platforms without futex.<hr> src/posix/wait.c | 58 ++++++++++++++++++++++++++++++++++++++----------<br> 1 file changed, 46 insertions(+), 12 deletions(-)<br><br>diff --git a/src/posix/wait.c b/src/posix/wait.c<br>index 5c146b61e2..26bce27448 100644<br>--- a/src/posix/wait.c<br>+++ b/src/posix/wait.c<br>@@ -32,6 +32,8 @@<br> #include <sys/types.h><br> #include <pthread.h><br> <br>+static clockid_t vlc_clock_id = CLOCK_REALTIME;<br>+<br> #define WAIT_BUCKET_INIT \<br> { PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, 0 }<br> #define WAIT_BUCKET_INIT_2 WAIT_BUCKET_INIT, WAIT_BUCKET_INIT<br>@@ -101,22 +103,29 @@ static int vlc_atomic_timedwait_timespec(void <br>*addr, unsigned value,<br> return ret;<br> }<br> <br>-int vlc_atomic_timedwait(void *addr, unsigned value, vlc_tick_t deadline)<br>+static void vlc_timespec_adjust(clockid_t cid, struct timespec *restrict ts)<br> {<br>- struct timespec ts;<br>- vlc_tick_t delay = deadline - vlc_tick_now();<br>- lldiv_t d = lldiv((delay >= 0) ? delay : 0, CLOCK_FREQ);<br>+ struct timespec now_from, now_to;<br>+ lldiv_t d;<br>+<br>+ if (vlc_clock_id == cid)<br>+ return;<br> <br>- /* TODO: use monotonic clock directly */<br>- clock_gettime(CLOCK_REALTIME, &ts);<br>- ts.tv_sec += d.quot;<br>- ts.tv_nsec += NS_FROM_VLC_TICK(d.rem);<br>+ clock_gettime(cid, &now_from);<br>+ clock_gettime(vlc_clock_id, &now_to);<br> <br>- if (ts.tv_nsec >= 1000000000) {<br>- ts.tv_sec++;<br>- ts.tv_nsec -= 1000000000;<br>- }<br>+ d = lldiv((ts->tv_sec - now_from.tv_sec + now_to.tv_sec) * 1000000000LL<br>+ + ts->tv_nsec - now_from.tv_nsec - now_to.tv_nsec, 1000000000LL);<br>+<br>+ ts->tv_sec = d.quot;<br>+ ts->tv_nsec = d.rem;<br>+}<br> <br>+int vlc_atomic_timedwait(void *addr, unsigned value, vlc_tick_t deadline)<br>+{<br>+ struct timespec ts = timespec_from_vlc_tick(deadline);<br>+<br>+ vlc_timespec_adjust(CLOCK_MONOTONIC, &ts);<br> return vlc_atomic_timedwait_timespec(addr, value, &ts);<br> }<br> <br>@@ -124,6 +133,7 @@ int vlc_atomic_timedwait_daytime(void *addr, <br>unsigned value, time_t deadline)<br> {<br> struct timespec ts = { .tv_sec = deadline, .tv_nsec = 0 };<br> <br>+ vlc_timespec_adjust(CLOCK_REALTIME, &ts);<br> return vlc_atomic_timedwait_timespec(addr, value, &ts);<br> }<br> <br>@@ -141,3 +151,27 @@ void vlc_atomic_notify_all(void *addr)<br> pthread_cond_broadcast(&bucket->wait);<br> pthread_mutex_unlock(&bucket->lock);<br> }<br>+<br>+#ifdef __ELF__<br>+__attribute__((constructor))<br>+static void vlc_atomic_clock_select(void)<br>+{<br>+ pthread_condattr_t attr;<br>+<br>+ pthread_condattr_init(&attr);<br>+ pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);<br>+<br>+ for (size_t i = 0; i < ARRAY_SIZE(wait_buckets); i++)<br>+ pthread_cond_init(&wait_buckets[i].wait, &attr);<br></blockquote><br>So, the condition variables end up being initialized two times ?<br>Here and by PTHREAD_COND_INITIALIZER.<br><br>Are we sure it's safe ?<br><br><blockquote class="gmail_quote" style="margin: 0pt 0pt 1ex 0.8ex; border-left: 1px solid #729fcf; padding-left: 1ex;">+<br>+ pthread_condattr_destroy(&attr);<br>+ vlc_clock_id = CLOCK_MONOTONIC;<br>+}<br>+<br>+__attribute__((destructor))<br>+static void vlc_atomic_clock_deselect(void)<br>+{<br>+ for (size_t i = 0; i < ARRAY_SIZE(wait_buckets); i++)<br>+ pthread_cond_destroy(&wait_buckets[i].wait);<br>+}<br>+#endif<br>-- <br>2.25.1<hr>vlc-devel mailing list<br>To unsubscribe or modify your subscription options:<br><a href="https://mailman.videolan.org/listinfo/vlc-devel">https://mailman.videolan.org/listinfo/vlc-devel</a><br></blockquote><hr>vlc-devel mailing list<br>To unsubscribe or modify your subscription options:<br><a href="https://mailman.videolan.org/listinfo/vlc-devel">https://mailman.videolan.org/listinfo/vlc-devel</a></pre></blockquote></div><br>-- <br>Envoyé de mon appareil Android avec Courriel K-9 Mail. Veuillez excuser ma brièveté.</body></html>