summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Lamparter <equinox@opensourcerouting.org>2015-05-29 09:56:51 +0200
committerDavid Lamparter <equinox@opensourcerouting.org>2017-03-31 17:59:45 +0200
commit899a4fd2a14bf51b8a37e6d0fadd5dbf9395b427 (patch)
tree4749d7a3ac4e9e10fd213e76863fc6c117d228ac
parentbuild: bump to C11 (diff)
downloadfrr-899a4fd2a14bf51b8a37e6d0fadd5dbf9395b427.tar.xz
frr-899a4fd2a14bf51b8a37e6d0fadd5dbf9395b427.zip
build: try to find some kind of atomic ops
Either one of: - ISO C11 - gcc __atomic_* - gcc/clang __sync_* Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
-rwxr-xr-xconfigure.ac66
-rw-r--r--lib/Makefile.am1
-rw-r--r--lib/frratomic.h118
3 files changed, 185 insertions, 0 deletions
diff --git a/configure.ac b/configure.ac
index b8cebd8d2..4875a6849 100755
--- a/configure.ac
+++ b/configure.ac
@@ -541,6 +541,72 @@ AC_CHECK_HEADERS([stropts.h sys/ksym.h \
linux/version.h asm/types.h \
sys/cdefs.h])
+ac_stdatomic_ok=false
+AC_DEFINE(FRR_AUTOCONF_ATOMIC, 1, [did autoconf checks for atomic funcs])
+AC_CHECK_HEADER([stdatomic.h],[
+
+ AC_MSG_CHECKING([whether _Atomic qualifier works])
+ AC_LINK_IFELSE([AC_LANG_SOURCE([[
+#include <stdatomic.h>
+int main(int argc, char **argv) {
+ _Atomic int i = 0;
+ return i;
+}
+]])], [
+ AC_DEFINE(HAVE_STDATOMIC_H, 1, [found stdatomic.h])
+ AC_MSG_RESULT([yes])
+ ac_stdatomic_ok=true
+ ], [
+ AC_MSG_RESULT([no])
+ ])
+])
+
+AS_IF([$ac_stdatomic_ok], [true], [
+ AC_MSG_CHECKING([for __atomic_* builtins])
+ AC_LINK_IFELSE([AC_LANG_SOURCE([[
+int main(int argc, char **argv) {
+ volatile int i = 1;
+ __atomic_store_n (&i, 0, __ATOMIC_RELEASE);
+ return __atomic_load_n (&i, __ATOMIC_ACQUIRE);
+}
+]])], [
+ AC_DEFINE(HAVE___ATOMIC, 1, [found __atomic builtins])
+ AC_MSG_RESULT([yes])
+ ], [
+ AC_MSG_RESULT([no])
+
+ dnl FreeBSD 9 has a broken stdatomic.h where _Atomic doesn't work
+ AC_MSG_CHECKING([for __sync_* builtins])
+ AC_LINK_IFELSE([AC_LANG_SOURCE([[
+int main(int argc, char **argv) {
+ volatile int i = 1;
+ __sync_fetch_and_sub (&i, 1);
+ return __sync_val_compare_and_swap (&i, 0, 1);
+}
+]])], [
+ AC_DEFINE(HAVE___SYNC, 1, [found __sync builtins])
+ AC_MSG_RESULT([yes])
+
+ AC_MSG_CHECKING([for __sync_swap builtin])
+ AC_LINK_IFELSE([AC_LANG_SOURCE([[
+int main(int argc, char **argv) {
+ volatile int i = 1;
+ return __sync_swap (&i, 2);
+}
+]])], [
+ AC_DEFINE(HAVE___SYNC_SWAP, 1, [found __sync_swap builtin])
+ AC_MSG_RESULT([yes])
+ ], [
+ AC_MSG_RESULT([no])
+ ])
+
+ ], [
+ AC_MSG_RESULT([no])
+ AC_MSG_FAILURE([stdatomic.h unavailable and $CC has neither __atomic nor __sync builtins])
+ ])
+ ])
+])
+
dnl Utility macro to avoid retyping includes all the time
m4_define([FRR_INCLUDES],
[#ifdef SUNOS_5
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 1a8c7af42..d7a4439d5 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -38,6 +38,7 @@ BUILT_SOURCES = route_types.h gitversion.h command_parse.h command_lex.h
libfrr_la_LIBADD = @LIBCAP@
pkginclude_HEADERS = \
+ frratomic.h \
buffer.h checksum.h filter.h getopt.h hash.h \
if.h linklist.h log.h \
graph.h command_match.h \
diff --git a/lib/frratomic.h b/lib/frratomic.h
new file mode 100644
index 000000000..183790aeb
--- /dev/null
+++ b/lib/frratomic.h
@@ -0,0 +1,118 @@
+/*
+ * Copyright (c) 2015-16 David Lamparter, for NetDEF, Inc.
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#ifndef _FRRATOMIC_H
+#define _FRRATOMIC_H
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#ifndef FRR_AUTOCONF_ATOMIC
+#error autoconf checks for atomic functions were not properly run
+#endif
+
+/* ISO C11 */
+#ifdef HAVE_STDATOMIC_H
+#include <stdatomic.h>
+
+/* gcc 4.7 and newer */
+#elif defined(HAVE___ATOMIC)
+
+#define _Atomic volatile
+
+#define memory_order_relaxed __ATOMIC_RELAXED
+#define memory_order_consume __ATOMIC_CONSUME
+#define memory_order_acquire __ATOMIC_ACQUIRE
+#define memory_order_release __ATOMIC_RELEASE
+#define memory_order_acq_rel __ATOMIC_ACQ_REL
+#define memory_order_seq_cst __ATOMIC_SEQ_CST
+
+#define atomic_load_explicit __atomic_load_n
+#define atomic_store_explicit __atomic_store_n
+#define atomic_exchange_explicit __atomic_exchange_n
+#define atomic_fetch_add_explicit __atomic_fetch_add
+#define atomic_fetch_sub_explicit __atomic_fetch_sub
+
+#define atomic_compare_exchange_weak_explicit(atom, expect, desire, mem1, mem2) \
+ __atomic_compare_exchange_n(atom, expect, desire, 1, mem1, mem2)
+
+/* gcc 4.1 and newer,
+ * clang 3.3 (possibly older)
+ *
+ * __sync_swap isn't in gcc's documentation, but clang has it
+ *
+ * note __sync_synchronize()
+ */
+#elif defined(HAVE___SYNC)
+
+#define _Atomic volatile
+
+#define memory_order_relaxed 0
+#define memory_order_consume 0
+#define memory_order_acquire 0
+#define memory_order_release 0
+#define memory_order_acq_rel 0
+#define memory_order_seq_cst 0
+
+#define atomic_load_explicit(ptr, mem) \
+ ({ __sync_synchronize(); \
+ typeof(*ptr) rval = __sync_fetch_and_add((ptr), 0); \
+ __sync_synchronize(); rval; })
+#define atomic_store_explicit(ptr, val, mem) \
+ ({ __sync_synchronize(); \
+ *(ptr) = (val); \
+ __sync_synchronize(); (void)0; })
+#ifdef HAVE___SYNC_SWAP
+#define atomic_exchange_explicit(ptr, val, mem) \
+ ({ __sync_synchronize(); \
+ typeof(*ptr) rval = __sync_swap((ptr, val), 0); \
+ __sync_synchronize(); rval; })
+#else /* !HAVE___SYNC_SWAP */
+#define atomic_exchange_explicit(ptr, val, mem) \
+ ({ typeof(ptr) _ptr = (ptr); typeof(val) _val = (val); \
+ __sync_synchronize(); \
+ typeof(*ptr) old1, old2 = __sync_fetch_and_add(_ptr, 0); \
+ do { \
+ old1 = old2; \
+ old2 = __sync_val_compare_and_swap (_ptr, old1, _val); \
+ } while (old1 != old2); \
+ __sync_synchronize(); \
+ old2; \
+ })
+#endif /* !HAVE___SYNC_SWAP */
+#define atomic_fetch_add_explicit(ptr, val, mem) \
+ ({ __sync_synchronize(); \
+ typeof(*ptr) rval = __sync_fetch_and_add((ptr), (val)); \
+ __sync_synchronize(); rval; })
+#define atomic_fetch_sub_explicit(ptr, val, mem) \
+ ({ __sync_synchronize(); \
+ typeof(*ptr) rval = __sync_fetch_and_sub((ptr), (val)); \
+ __sync_synchronize(); rval; })
+
+#define atomic_compare_exchange_weak_explicit(atom, expect, desire, mem1, mem2) \
+ ({ typeof(atom) _atom = (atom); typeof(expect) _expect = (expect); \
+ typeof(desire) _desire = (desire); \
+ __sync_synchronize(); \
+ typeof(*atom) rval = __sync_val_compare_and_swap(_atom, *_expect, _desire); \
+ __sync_synchronize(); \
+ bool ret = (rval == *_expect); *_expect = rval; ret; })
+
+#else /* !HAVE___ATOMIC && !HAVE_STDATOMIC_H */
+#error no atomic functions...
+#endif
+
+#endif /* _FRRATOMIC_H */