mirror of
https://github.com/FRRouting/frr.git
synced 2026-01-16 23:14:01 +00:00
build: ditch ages-old atomic compatibility foo
It's 2025, we can assume ISO C11 support in the compiler. Just retain the few lines needed for C++. Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
This commit is contained in:
parent
10666fb6c4
commit
78c09232e3
2 changed files with 3 additions and 246 deletions
50
configure.ac
50
configure.ac
|
|
@ -1134,10 +1134,7 @@ AC_CHECK_HEADERS([stropts.h sys/ksym.h \
|
|||
|
||||
AC_CHECK_LIB([atomic], [main], [LIBS="$LIBS -latomic"], [], [])
|
||||
|
||||
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>
|
||||
|
|
@ -1151,52 +1148,7 @@ int main(int argc, char **argv) {
|
|||
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])
|
||||
])
|
||||
AC_MSG_ERROR([building FRR requires an ISO C11 compliant compiler with atomics support])
|
||||
])
|
||||
])
|
||||
|
||||
|
|
|
|||
199
lib/frratomic.h
199
lib/frratomic.h
|
|
@ -6,15 +6,7 @@
|
|||
#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 */
|
||||
/* C++ compatibility */
|
||||
#ifdef __cplusplus
|
||||
#include <stdint.h>
|
||||
#include <atomic>
|
||||
|
|
@ -32,195 +24,8 @@ typedef std::atomic<size_t> atomic_size_t;
|
|||
typedef std::atomic<uint_fast32_t> atomic_uint_fast32_t;
|
||||
typedef std::atomic<uintptr_t> atomic_uintptr_t;
|
||||
|
||||
#elif defined(HAVE_STDATOMIC_H)
|
||||
#else
|
||||
#include <stdatomic.h>
|
||||
|
||||
/* These are available in gcc, but not in stdatomic */
|
||||
#define atomic_add_fetch_explicit __atomic_add_fetch
|
||||
#define atomic_sub_fetch_explicit __atomic_sub_fetch
|
||||
#define atomic_and_fetch_explicit __atomic_and_fetch
|
||||
#define atomic_or_fetch_explicit __atomic_or_fetch
|
||||
|
||||
/* gcc 4.7 and newer */
|
||||
#elif defined(HAVE___ATOMIC)
|
||||
|
||||
#define _Atomic volatile
|
||||
#define _ATOMIC_WANT_TYPEDEFS
|
||||
|
||||
#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_fetch_and_explicit __atomic_fetch_and
|
||||
#define atomic_fetch_or_explicit __atomic_fetch_or
|
||||
|
||||
#define atomic_add_fetch_explicit __atomic_add_fetch
|
||||
#define atomic_sub_fetch_explicit __atomic_sub_fetch
|
||||
#define atomic_and_fetch_explicit __atomic_and_fetch
|
||||
#define atomic_or_fetch_explicit __atomic_or_fetch
|
||||
|
||||
#define atomic_compare_exchange_weak_explicit(atom, expect, desire, mem1, \
|
||||
mem2) \
|
||||
__atomic_compare_exchange_n(atom, expect, desire, 1, mem1, mem2)
|
||||
#define atomic_compare_exchange_strong_explicit(atom, expect, desire, mem1, \
|
||||
mem2) \
|
||||
__atomic_compare_exchange_n(atom, expect, desire, 0, 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 _ATOMIC_WANT_TYPEDEFS
|
||||
|
||||
#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_strong_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; \
|
||||
})
|
||||
#define atomic_compare_exchange_weak_explicit \
|
||||
atomic_compare_exchange_strong_explicit
|
||||
|
||||
#define atomic_fetch_and_explicit(ptr, val, mem) \
|
||||
({ \
|
||||
__sync_synchronize(); \
|
||||
typeof(*ptr) rval = __sync_fetch_and_and(ptr, val); \
|
||||
__sync_synchronize(); \
|
||||
rval; \
|
||||
})
|
||||
#define atomic_fetch_or_explicit(ptr, val, mem) \
|
||||
({ \
|
||||
__sync_synchronize(); \
|
||||
typeof(*ptr) rval = __sync_fetch_and_or(ptr, val); \
|
||||
__sync_synchronize(); \
|
||||
rval; \
|
||||
})
|
||||
|
||||
#define atomic_add_fetch_explicit(ptr, val, mem) \
|
||||
({ \
|
||||
__sync_synchronize(); \
|
||||
typeof(*ptr) rval = __sync_add_and_fetch((ptr), (val)); \
|
||||
__sync_synchronize(); \
|
||||
rval; \
|
||||
})
|
||||
#define atomic_sub_fetch_explicit(ptr, val, mem) \
|
||||
({ \
|
||||
__sync_synchronize(); \
|
||||
typeof(*ptr) rval = __sync_sub_and_fetch((ptr), (val)); \
|
||||
__sync_synchronize(); \
|
||||
rval; \
|
||||
})
|
||||
|
||||
#define atomic_and_fetch_explicit(ptr, val, mem) \
|
||||
({ \
|
||||
__sync_synchronize(); \
|
||||
typeof(*ptr) rval = __sync_and_and_fetch(ptr, val); \
|
||||
__sync_synchronize(); \
|
||||
rval; \
|
||||
})
|
||||
#define atomic_or_fetch_explicit(ptr, val, mem) \
|
||||
({ \
|
||||
__sync_synchronize(); \
|
||||
typeof(*ptr) rval = __sync_or_and_fetch(ptr, val); \
|
||||
__sync_synchronize(); \
|
||||
rval; \
|
||||
})
|
||||
|
||||
#else /* !HAVE___ATOMIC && !HAVE_STDATOMIC_H */
|
||||
#error no atomic functions...
|
||||
#endif
|
||||
|
||||
#ifdef _ATOMIC_WANT_TYPEDEFS
|
||||
#undef _ATOMIC_WANT_TYPEDEFS
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef _Atomic bool atomic_bool;
|
||||
typedef _Atomic size_t atomic_size_t;
|
||||
typedef _Atomic uint_fast32_t atomic_uint_fast32_t;
|
||||
typedef _Atomic uintptr_t atomic_uintptr_t;
|
||||
#endif
|
||||
|
||||
#endif /* _FRRATOMIC_H */
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue