summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Cowgill <james410@cowgill.org.uk>2017-01-15 00:16:06 +0000
committerJames Cowgill <james410@cowgill.org.uk>2017-01-15 00:16:06 +0000
commiteed8a97b2f37f5e37de2546cede17dc5aaa10024 (patch)
treeaf3032c2f7c01c487853e7a4d3177690e5666f22
parent90f9dd39b0a08802f05e2f09622197f922210c81 (diff)
downloadjack1-eed8a97b2f37f5e37de2546cede17dc5aaa10024.tar.gz
Use generic atomic functions
Use atomic_fetch_add from C11's stdatomic if available or fallback to GCC's __atomic functions. Remove atomic_add because is can trivially be implemented by calling exchange_and_add instead.
-rw-r--r--include/atomicity.h33
-rw-r--r--include/internal.h2
-rw-r--r--libjack/messagebuffer.c2
3 files changed, 22 insertions, 15 deletions
diff --git a/include/atomicity.h b/include/atomicity.h
index edf4ada..4c935c5 100644
--- a/include/atomicity.h
+++ b/include/atomicity.h
@@ -20,19 +20,26 @@
#ifndef __jack_atomicity_h__
#define __jack_atomicity_h__
-/*
- * Interface with various machine-dependent headers derived from the
- * gcc/libstdc++.v3 sources. We try to modify the GCC sources as
- * little as possible. The following include is resolved using the
- * config/configure.hosts mechanism. It will use an OS-dependent
- * version if available, otherwise the one for this CPU. Some of
- * these files might not work with older GCC compilers.
- */
-#include <sysdeps/atomicity.h>
+#if __STDC_VERSION__ >= 201112L && !defined(__STDC_NO_ATOMICS__)
+
+#include <stdatomic.h>
+
+typedef atomic_int _Atomic_word;
+
+static inline int exchange_and_add(volatile _Atomic_word* obj, int value)
+{
+ return atomic_fetch_add_explicit(obj, value, memory_order_relaxed);
+}
+
+#else
+
+typedef int _Atomic_word;
+
+static inline int exchange_and_add(volatile _Atomic_word* obj, int value)
+{
+ return __atomic_fetch_add(obj, value, __ATOMIC_RELAXED);
+}
-/* These functions are defined for each platform. The C++ library
- * function names start with "__" to avoid namespace pollution. */
-#define exchange_and_add __exchange_and_add
-#define atomic_add __atomic_add
+#endif
#endif /* __jack_atomicity_h__ */
diff --git a/include/internal.h b/include/internal.h
index e3574bd..396b456 100644
--- a/include/internal.h
+++ b/include/internal.h
@@ -60,7 +60,7 @@ void jack_set_clock_source (jack_timer_type_t);
const char* jack_clock_source_name (jack_timer_type_t);
#include <sysdeps/time.h>
-#include <sysdeps/atomicity.h>
+#include "atomicity.h"
#ifdef JACK_USE_MACH_THREADS
#include <sysdeps/mach_port.h>
diff --git a/libjack/messagebuffer.c b/libjack/messagebuffer.c
index 5f9541a..082f2f1 100644
--- a/libjack/messagebuffer.c
+++ b/libjack/messagebuffer.c
@@ -161,7 +161,7 @@ jack_messagebuffer_add (const char *fmt, ...)
pthread_cond_signal (&mb_ready_cond);
pthread_mutex_unlock (&mb_write_lock);
} else { /* lock collision */
- atomic_add (&mb_overruns, 1);
+ exchange_and_add (&mb_overruns, 1);
}
}