[vlc-devel] [[PATCH v3] 3/6] threads: store the condition variable value as an atomic_uint with LIBVLC_NEED_CONDVAR

Steve Lhomme robux4 at ycbcr.xyz
Mon Feb 10 14:39:12 CET 2020


We can include stdatomic.h in the headers, we do it in many places. But we
can't use it with C++ because the std:atomic_int storage is likely different
and can be initialized/released the same way.

We change the static assert in the core to make sure the type in the union that
will be used for storage by C++ modules is correct.

In C++11 there is std::condition_variable anyway. The vlc_cond_t would only be
useful to share it with C code in a module or legacy C++ code.
---
 include/vlc_threads.h | 7 ++++++-
 src/misc/threads.c    | 8 ++++----
 2 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/include/vlc_threads.h b/include/vlc_threads.h
index 1598c9c804d..68b98b6b335 100644
--- a/include/vlc_threads.h
+++ b/include/vlc_threads.h
@@ -357,7 +357,12 @@ typedef struct vlc_timer *vlc_timer_t;
 
 typedef struct
 {
-    unsigned value;
+    union {
+#ifndef __cplusplus
+        atomic_uint value;
+#endif
+        int        cpp_value;
+    };
 } vlc_cond_t;
 # define VLC_STATIC_COND { 0 }
 #endif
diff --git a/src/misc/threads.c b/src/misc/threads.c
index b40348ca903..7edb369bed4 100644
--- a/src/misc/threads.c
+++ b/src/misc/threads.c
@@ -200,12 +200,12 @@ void (vlc_tick_sleep)(vlc_tick_t delay)
 
 static inline atomic_uint *vlc_cond_value(vlc_cond_t *cond)
 {
-    /* XXX: ugly but avoids including stdatomic.h in vlc_threads.h */
-    static_assert (sizeof (cond->value) <= sizeof (atomic_uint),
+    /* Don't use C++ atomic types in vlc_threads.h for the C atomic storage */
+    static_assert (sizeof (cond->cpp_value) <= sizeof (cond->value),
                    "Size mismatch!");
-    static_assert ((alignof (cond->value) % alignof (atomic_uint)) == 0,
+    static_assert ((alignof (cond->cpp_value) % alignof (cond->value)) == 0,
                    "Alignment mismatch");
-    return (atomic_uint *)&cond->value;
+    return &cond->value;
 }
 
 void vlc_cond_init(vlc_cond_t *cond)
-- 
2.17.1



More information about the vlc-devel mailing list