[vlc-devel] [PATCH 5/5] threads: store the condition variable value as an atomic_int

Steve Lhomme robux4 at ycbcr.xyz
Fri Feb 7 16:02:10 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.
---
 include/vlc_threads.h | 12 +++++++++++-
 src/misc/threads.c    |  8 ++++----
 2 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/include/vlc_threads.h b/include/vlc_threads.h
index a5425236b1c..075083d1005 100644
--- a/include/vlc_threads.h
+++ b/include/vlc_threads.h
@@ -351,9 +351,19 @@ typedef struct vlc_timer *vlc_timer_t;
 #endif
 
 #ifdef LIBVLC_NEED_CONDVAR
+#ifndef __cplusplus
+#include <stdatomic.h>
+#endif
+
+
 typedef struct
 {
-    unsigned value;
+    union {
+#ifndef __cplusplus
+        atomic_int 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 7699cfae05d..47bf1dfcb14 100644
--- a/src/misc/threads.c
+++ b/src/misc/threads.c
@@ -210,12 +210,12 @@ void (vlc_tick_sleep)(vlc_tick_t delay)
 
 static inline atomic_int *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_int),
+    /* 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_int)) == 0,
+    static_assert ((alignof (cond->cpp_value) % alignof (cond->value)) == 0,
                    "Alignment mismatch");
-    return (atomic_int *)&cond->value;
+    return &cond->value;
 }
 
 void vlc_cond_init(vlc_cond_t *cond)
-- 
2.17.1



More information about the vlc-devel mailing list