if ac_fn_c_try_link "$LINENO"; then :
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5
$as_echo "yes" >&6; }
+ax_cv_have_gcc_atomics=1
$as_echo "#define HAVE_GCC_ATOMICS 1" >>confdefs.h
if ac_fn_c_try_link "$LINENO"; then :
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5
$as_echo "yes" >&6; }
+ax_cv_have_c_atomics=1
$as_echo "#define HAVE_C_ATOMICS 1" >>confdefs.h
rm -f core conftest.err conftest.$ac_objext \
conftest$ac_exeext conftest.$ac_ext
+if test -z $ax_cv_have_c_atomics$ax_cv_have_gcc_atomics; then
+ as_fn_error $? "*** Atomic operations are not supported by your compiler." "$LINENO" 5
+fi
+
# glibc, AFAIK, is the only C library that makes printing a NULL to a string safe.
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking if your system printf is NULL-safe." >&5
$as_echo_n "checking if your system printf is NULL-safe.... " >&6; }
-ac_fn_c_check_header_mongrel "$LINENO" "libkern/OSAtomic.h" "ac_cv_header_libkern_OSAtomic_h" "$ac_includes_default"
-if test "x$ac_cv_header_libkern_OSAtomic_h" = xyes; then :
-
-cat >>confdefs.h <<_ACEOF
-#define HAVE_OSX_ATOMICS 1
-_ACEOF
-
-fi
-
-
-
# The cast to long int works around a bug in the HP C Compiler
# version HP92453-01 B.11.11.23709.GP, which incorrectly rejects
# declarations like `int a3[[(sizeof (unsigned char)) >= 0]];'.
AC_LINK_IFELSE(
[AC_LANG_PROGRAM([], [int foo1; int foo2 = __sync_fetch_and_add(&foo1, 1);])],
AC_MSG_RESULT(yes)
+ax_cv_have_gcc_atomics=1
AC_DEFINE([HAVE_GCC_ATOMICS], 1, [Define to 1 if your GCC C compiler provides __sync atomic operations.]),
AC_MSG_RESULT(no)
)
AC_LINK_IFELSE(
[AC_LANG_PROGRAM([], [int foo1; int foo2 = __atomic_fetch_add(&foo1, 1, __ATOMIC_RELAXED);])],
AC_MSG_RESULT(yes)
+ax_cv_have_c_atomics=1
AC_DEFINE([HAVE_C_ATOMICS], 1, [Define to 1 if your C compiler provides __atomic operations.]),
AC_MSG_RESULT(no)
)
+if test -z $ax_cv_have_c_atomics$ax_cv_have_gcc_atomics; then
+ AC_MSG_ERROR([*** Atomic operations are not supported by your compiler.])
+fi
+
# glibc, AFAIK, is the only C library that makes printing a NULL to a string safe.
AC_MSG_CHECKING([if your system printf is NULL-safe.])
AC_RUN_IFELSE(
AST_C_DEFINE_CHECK([IP_MTU_DISCOVER], [IP_MTU_DISCOVER], [netinet/in.h])
-AC_CHECK_HEADER([libkern/OSAtomic.h],
- [AC_DEFINE_UNQUOTED([HAVE_OSX_ATOMICS], 1, [Define to 1 if OSX atomic operations are supported.])])
-
AC_CHECK_SIZEOF([int])
AC_CHECK_SIZEOF([long])
AC_CHECK_SIZEOF([long long])
#define pthread_create __use_ast_pthread_create_instead__
#endif
-/*
- * Support for atomic instructions.
- * For platforms that have it, use the native cpu instruction to
- * implement them. For other platforms, resort to a 'slow' version
- * (defined in utils.c) that protects the atomic instruction with
- * a single lock.
- * The slow versions is always available, for testing purposes,
- * as ast_atomic_fetchadd_int_slow()
- */
-
-int ast_atomic_fetchadd_int_slow(volatile int *p, int v);
+/* Support for atomic instructions. */
#include "asterisk/inline_api.h"
-#if defined(HAVE_OSX_ATOMICS)
-#include "libkern/OSAtomic.h"
+#if defined(HAVE_C_ATOMICS)
+#define ast_atomic_fetch_add(p, v, memorder) __atomic_fetch_add(p, v, memorder)
+#define ast_atomic_sub_fetch(p, v, memorder) __atomic_sub_fetch(p, v, memorder)
+#elif defined(HAVE_GCC_ATOMICS)
+#define ast_atomic_fetch_add(p, v, memorder) __sync_fetch_and_add(p, v)
+#define ast_atomic_sub_fetch(p, v, memorder) __sync_sub_and_fetch(p, v)
+#else
+#error "Atomics not available."
#endif
-/*! \brief Atomically add v to *p and return * the previous value of *p.
+/*!
+ * \brief Atomically add v to *p and return the previous value of *p.
+ *
* This can be used to handle reference counts, and the return value
* can be used to generate unique identifiers.
*/
-
-#if defined(HAVE_C_ATOMICS)
AST_INLINE_API(int ast_atomic_fetchadd_int(volatile int *p, int v),
{
- return __atomic_fetch_add(p, v, __ATOMIC_RELAXED);
+ return ast_atomic_fetch_add(p, v, __ATOMIC_RELAXED);
})
-#elif defined(HAVE_GCC_ATOMICS)
-AST_INLINE_API(int ast_atomic_fetchadd_int(volatile int *p, int v),
-{
- return __sync_fetch_and_add(p, v);
-})
-#elif defined(HAVE_OSX_ATOMICS) && (SIZEOF_INT == 4)
-AST_INLINE_API(int ast_atomic_fetchadd_int(volatile int *p, int v),
-{
- return OSAtomicAdd32(v, (int32_t *) p) - v;
-})
-#elif defined(HAVE_OSX_ATOMICS) && (SIZEOF_INT == 8)
-AST_INLINE_API(int ast_atomic_fetchadd_int(volatile int *p, int v),
-{
- return OSAtomicAdd64(v, (int64_t *) p) - v;
-})
-#elif defined (__i386__) || defined(__x86_64__)
-#ifdef sun
-AST_INLINE_API(int ast_atomic_fetchadd_int(volatile int *p, int v),
-{
- __asm __volatile (
- " lock; xaddl %0, %1 ; "
- : "+r" (v), /* 0 (result) */
- "=m" (*p) /* 1 */
- : "m" (*p)); /* 2 */
- return (v);
-})
-#else /* ifndef sun */
-AST_INLINE_API(int ast_atomic_fetchadd_int(volatile int *p, int v),
-{
- __asm __volatile (
- " lock xaddl %0, %1 ; "
- : "+r" (v), /* 0 (result) */
- "=m" (*p) /* 1 */
- : "m" (*p)); /* 2 */
- return (v);
-})
-#endif
-#else /* low performance version in utils.c */
-AST_INLINE_API(int ast_atomic_fetchadd_int(volatile int *p, int v),
-{
- return ast_atomic_fetchadd_int_slow(p, v);
-})
-#endif
-/*! \brief decrement *p by 1 and return true if the variable has reached 0.
+/*!
+ * \brief decrement *p by 1 and return true if the variable has reached 0.
+ *
* Useful e.g. to check if a refcount has reached 0.
*/
-#if defined(HAVE_C_ATOMICS)
-AST_INLINE_API(int ast_atomic_dec_and_test(volatile int *p),
-{
- return __atomic_sub_fetch(p, 1, __ATOMIC_RELAXED) == 0;
-})
-#elif defined(HAVE_GCC_ATOMICS)
-AST_INLINE_API(int ast_atomic_dec_and_test(volatile int *p),
-{
- return __sync_sub_and_fetch(p, 1) == 0;
-})
-#elif defined(HAVE_OSX_ATOMICS) && (SIZEOF_INT == 4)
-AST_INLINE_API(int ast_atomic_dec_and_test(volatile int *p),
-{
- return OSAtomicAdd32( -1, (int32_t *) p) == 0;
-})
-#elif defined(HAVE_OSX_ATOMICS) && (SIZEOF_INT == 8)
-AST_INLINE_API(int ast_atomic_dec_and_test(volatile int *p),
-{
- return OSAtomicAdd64( -1, (int64_t *) p) == 0;
-})
-#else
AST_INLINE_API(int ast_atomic_dec_and_test(volatile int *p),
{
- int a = ast_atomic_fetchadd_int(p, -1);
- return a == 1; /* true if the value is 0 now (so it was 1 previously) */
+ return ast_atomic_sub_fetch(p, 1, __ATOMIC_RELAXED) == 0;
})
-#endif
#endif /* _ASTERISK_LOCK_H */