[vlc-commits] vlc_atomic.h: Allow inclusion when building C++

Hugo Beauzée-Luyssen git at videolan.org
Thu Dec 17 15:19:47 UTC 2020


vlc | branch: master | Hugo Beauzée-Luyssen <hugo at beauzee.fr> | Thu Nov 26 12:25:54 2020 +0100| [876bb0e0094a5b151adc3a7e4c521514862fdcbb] | committer: Hugo Beauzée-Luyssen

vlc_atomic.h: Allow inclusion when building C++

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

 include/vlc_atomic.h | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)

diff --git a/include/vlc_atomic.h b/include/vlc_atomic.h
index ac0d5998aa..b393fafb36 100644
--- a/include/vlc_atomic.h
+++ b/include/vlc_atomic.h
@@ -18,10 +18,6 @@
  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
-#ifdef __cplusplus
-# error Not implemented in C++.
-#endif
-
 #ifndef VLC_ATOMIC_H
 # define VLC_ATOMIC_H
 
@@ -31,7 +27,14 @@
  */
 
 # include <assert.h>
+#ifndef __cplusplus
 # include <stdatomic.h>
+#else
+# include <atomic>
+using std::atomic_uintptr_t;
+using std::memory_order_relaxed;
+using std::memory_order_acq_rel;
+#endif
 # include <vlc_common.h>
 
 typedef struct vlc_atomic_rc_t {
@@ -41,13 +44,14 @@ typedef struct vlc_atomic_rc_t {
 /** Init the RC to 1 */
 static inline void vlc_atomic_rc_init(vlc_atomic_rc_t *rc)
 {
-    atomic_init(&rc->refs, 1);
+    atomic_init(&rc->refs, (uintptr_t)1);
 }
 
 /** Increment the RC */
 static inline void vlc_atomic_rc_inc(vlc_atomic_rc_t *rc)
 {
-    uintptr_t prev = atomic_fetch_add_explicit(&rc->refs, 1, memory_order_relaxed);
+    uintptr_t prev = atomic_fetch_add_explicit(&rc->refs, (uintptr_t)1,
+                                               memory_order_relaxed);
     vlc_assert(prev);
     VLC_UNUSED(prev);
 }
@@ -55,7 +59,8 @@ static inline void vlc_atomic_rc_inc(vlc_atomic_rc_t *rc)
 /** Decrement the RC and return true if it reaches 0 */
 static inline bool vlc_atomic_rc_dec(vlc_atomic_rc_t *rc)
 {
-    uintptr_t prev = atomic_fetch_sub_explicit(&rc->refs, 1, memory_order_acq_rel);
+    uintptr_t prev = atomic_fetch_sub_explicit(&rc->refs, (uintptr_t)1,
+                                               memory_order_acq_rel);
     vlc_assert(prev);
     return prev == 1;
 }



More information about the vlc-commits mailing list