[Asterisk-code-review] Build System: Require sync or atomic functions. (asterisk[master])

Jenkins2 asteriskteam at digium.com
Tue Jan 30 06:56:09 CST 2018


Jenkins2 has submitted this change and it was merged. ( https://gerrit.asterisk.org/8049 )

Change subject: Build System: Require __sync or __atomic functions.
......................................................................

Build System: Require __sync or __atomic functions.

This change causes the configure script to throw an error if neither
__sync nor __atomic builtin functions are available.

ASTERISK-27619

Change-Id: Ie01a281e0f5c41dfeeb5f250c1ccea8752f56ef9
---
M README.md
M configure
M configure.ac
M include/asterisk/autoconfig.h.in
M include/asterisk/lock.h
M main/utils.c
M utils/check_expr.c
7 files changed, 33 insertions(+), 125 deletions(-)

Approvals:
  George Joseph: Looks good to me, but someone else must approve
  Michael L. Young: Looks good to me, but someone else must approve
  Joshua Colp: Looks good to me, approved
  Jenkins2: Approved for Submit



diff --git a/README.md b/README.md
index 153c4f3..10158b3 100644
--- a/README.md
+++ b/README.md
@@ -69,7 +69,7 @@
 
   Ensure that your system contains a compatible compiler and development
 libraries.  Asterisk requires either the GNU Compiler Collection (GCC) version
-3.0 or higher, or a compiler that supports the C99 specification and some of
+4.1 or higher, or a compiler that supports the C99 specification and some of
 the gcc language extensions.  In addition, your system needs to have the C
 library headers available, and the headers and libraries for ncurses.
 
diff --git a/configure b/configure
index f000c66..f6e83d9 100755
--- a/configure
+++ b/configure
@@ -17909,6 +17909,7 @@
 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
 
@@ -17936,6 +17937,7 @@
 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
 
@@ -17946,6 +17948,10 @@
 fi
 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
@@ -19354,17 +19360,6 @@
 rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
 	CPPFLAGS="${saved_cppflags}"
     fi
-
-
-
-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
 
 
 
diff --git a/configure.ac b/configure.ac
index 07ff3b3..6e54f3c 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1074,6 +1074,7 @@
 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)
 )
@@ -1082,9 +1083,14 @@
 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.])
@@ -1412,9 +1418,6 @@
 AST_C_DEFINE_CHECK([RTLD_NOLOAD], [RTLD_NOLOAD], [dlfcn.h])
 
 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])
diff --git a/include/asterisk/autoconfig.h.in b/include/asterisk/autoconfig.h.in
index 18f9d4e..b95d526 100644
--- a/include/asterisk/autoconfig.h.in
+++ b/include/asterisk/autoconfig.h.in
@@ -603,9 +603,6 @@
 /* Define to 1 if you have the Open Sound System library. */
 #undef HAVE_OSS
 
-/* Define to 1 if OSX atomic operations are supported. */
-#undef HAVE_OSX_ATOMICS
-
 /* Define to 1 if your system defines the file flag O_EVTONLY in fcntl.h */
 #undef HAVE_O_EVTONLY
 
diff --git a/include/asterisk/lock.h b/include/asterisk/lock.h
index d912b56..c3dd691 100644
--- a/include/asterisk/lock.h
+++ b/include/asterisk/lock.h
@@ -617,107 +617,39 @@
 #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.
  */
+AST_INLINE_API(int ast_atomic_fetchadd_int(volatile int *p, int v),
+{
+	return ast_atomic_fetch_add(p, v, __ATOMIC_RELAXED);
+})
 
-#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);
-})
-#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;
+	return ast_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) */
-})
-#endif
 
 #endif /* _ASTERISK_LOCK_H */
diff --git a/main/utils.c b/main/utils.c
index 85228c1..7f74f40 100644
--- a/main/utils.c
+++ b/main/utils.c
@@ -1985,18 +1985,6 @@
 	return res;
 }
 
-AST_MUTEX_DEFINE_STATIC(fetchadd_m); /* used for all fetc&add ops */
-
-int ast_atomic_fetchadd_int_slow(volatile int *p, int v)
-{
-	int ret;
-	ast_mutex_lock(&fetchadd_m);
-	ret = *p;
-	*p += v;
-	ast_mutex_unlock(&fetchadd_m);
-	return ret;
-}
-
 /*! \brief
  * get values from config variables.
  */
diff --git a/utils/check_expr.c b/utils/check_expr.c
index e15705e..1e4b9d1 100644
--- a/utils/check_expr.c
+++ b/utils/check_expr.c
@@ -143,13 +143,6 @@
 void parse_file(const char *fname);
 
 int ast_add_profile(const char *x, uint64_t scale) { return 0;}
-int ast_atomic_fetchadd_int_slow(volatile int *p, int v)
-{
-        int ret;
-        ret = *p;
-        *p += v;
-        return ret;
-}
 
 char *find_var(const char *varname) /* the list should be pretty short, if there's any list at all */
 {

-- 
To view, visit https://gerrit.asterisk.org/8049
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: Ie01a281e0f5c41dfeeb5f250c1ccea8752f56ef9
Gerrit-Change-Number: 8049
Gerrit-PatchSet: 2
Gerrit-Owner: Corey Farrell <git at cfware.com>
Gerrit-Reviewer: Corey Farrell <git at cfware.com>
Gerrit-Reviewer: George Joseph <gjoseph at digium.com>
Gerrit-Reviewer: Jenkins2
Gerrit-Reviewer: Joshua Colp <jcolp at digium.com>
Gerrit-Reviewer: Michael L. Young <elgueromexicano at gmail.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20180130/b2d264f4/attachment-0001.html>


More information about the asterisk-code-review mailing list