path: root/compat
diff options
authorwm4 <wm4@nowhere>2014-05-21 01:04:47 +0200
committerwm4 <wm4@nowhere>2014-05-21 02:21:18 +0200
commit8e7cf4bc992f13dbb523bb42d6b9de4bc2f486c2 (patch)
tree77f21515a336c368d2bb97eab11d950bed9f5c4d /compat
parent2f65f0e2548f95b3b8ba6620efe6c0e3cb02420b (diff)
atomics: switch to C11 stdatomic.h
In my opinion, we shouldn't use atomics at all, but ok. This switches the mpv code to use C11 stdatomic.h, and for compilers that don't support stdatomic.h yet, we emulate the subset used by mpv using the builtins commonly provided by gcc and clang. This supersedes an earlier similar attempt by Kovensky. That attempt unfortunately relied on a big copypasted freebsd header (which also depended on much more highly compiler-specific functionality, defined reserved symbols, etc.), so it had to be NIH'ed. Some issues: - C11 says default initialization of atomics "produces a valid state", but it's not sure whether the stored value is really 0. But we rely on this. - I'm pretty sure our use of the __atomic... builtins is/was incorrect. We don't use atomic load/store intrinsics, and access stuff directly. - Our wrapper actually does stricter typechecking than the stdatomic.h implementation by gcc 4.9. We make the atomic types incompatible with normal types by wrapping them into structs. (The FreeBSD wrapper does the same.) - I couldn't test on MinGW.
Diffstat (limited to 'compat')
1 files changed, 36 insertions, 6 deletions
diff --git a/compat/atomics.h b/compat/atomics.h
index 14f0f2720f..56328ab354 100644
--- a/compat/atomics.h
+++ b/compat/atomics.h
@@ -16,17 +16,47 @@
* with mpv. If not, see <>.
-// At this point both gcc and clang had __sync_synchronize support for some
-// time. We only support a full memory barrier.
+#ifndef MP_ATOMICS_H
+#define MP_ATOMICS_H
+#include <inttypes.h>
#include "config.h"
+#include <stdatomic.h>
+// Emulate the parts of C11 stdatomic.h needed by mpv.
+// Still relies on gcc/clang atomic builtins.
+typedef struct { volatile unsigned long v; } atomic_ulong;
+typedef struct { volatile int v; } atomic_int;
+typedef struct { volatile _Bool v; } atomic_bool;
+typedef struct { volatile long long v; } atomic_llong;
+typedef struct { volatile uint_least32_t v; } atomic_uint_least32_t;
+typedef struct { volatile unsigned long long v; } atomic_ullong;
+#define ATOMIC_VAR_INIT(x) \
+ {.v = (x)}
+#define atomic_load(p) \
+ (mp_memory_barrier(), (p)->v)
+#define atomic_store(p, val) \
+ ((p)->v = (val), mp_memory_barrier())
-# define mp_memory_barrier() __atomic_thread_fence(__ATOMIC_SEQ_CST)
-# define mp_atomic_add_and_fetch(a, b) __atomic_add_fetch(a, b,__ATOMIC_SEQ_CST)
+# define mp_memory_barrier() \
+ __atomic_thread_fence(__ATOMIC_SEQ_CST)
+# define atomic_fetch_add(a, b) \
+ __atomic_add_fetch(&(a)->v, b, __ATOMIC_SEQ_CST)
-# define mp_memory_barrier() __sync_synchronize()
-# define mp_atomic_add_and_fetch(a, b) __sync_add_and_fetch(a, b)
+# define mp_memory_barrier() \
+ __sync_synchronize()
+# define atomic_fetch_add(a, b) \
+ (__sync_add_and_fetch(&(a)->v, b), mp_memory_barrier())
# error "this should have been a configuration error, report a bug please"
+#endif /* else HAVE_STDATOMIC */