Skip to content

Commit d9fc855

Browse files
committed
Merge pull request #1743 from hjelmn/gcc_atomics_fix
atomic/gcc: add check for 128-bit CAS being lock-free
2 parents 5aab4b2 + d86e41e commit d9fc855

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

config/opal_config_asm.m4

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,21 @@ AC_DEFUN([OPAL_CHECK_GCC_BUILTIN_CSWAP_INT128], [
148148
149149
CFLAGS=$CFLAGS_save
150150
fi
151+
152+
if test $atomic_compare_exchange_n_128_result = 1 ; then
153+
AC_MSG_CHECKING([if __int128 atomic compare-and-swap is always lock-free])
154+
AC_RUN_IFELSE([AC_LANG_PROGRAM([], [if (!__atomic_always_lock_free(16, 0)) { return 1; }])],
155+
[AC_MSG_RESULT([yes])],
156+
[AC_MSG_RESULT([no])
157+
OPAL_CHECK_SYNC_BUILTIN_CSWAP_INT128
158+
atomic_compare_exchange_n_128_result=0],
159+
[AC_MSG_RESULT([no (cross compiling)])])
160+
fi
151161
else
152162
AC_MSG_CHECKING([for compiler support of __atomic builtin atomic compare-and-swap on 128-bit values])
153163
154164
# Check if the compiler supports the __atomic builtin
155-
AC_TRY_LINK([], [__int128 x = 0; __atomic_bool_compare_and_swap (&x, 0, 1);],
165+
AC_TRY_LINK([], [__int128 x = 0, y = 0; __atomic_compare_exchange_n (&x, &y, 1, 0, __ATOMIC_RELAXED, __ATOMIC_RELAXED);],
156166
[AC_MSG_RESULT([yes])
157167
atomic_compare_exchange_n_128_result=1],
158168
[AC_MSG_RESULT([no])])
@@ -162,7 +172,7 @@ AC_DEFUN([OPAL_CHECK_GCC_BUILTIN_CSWAP_INT128], [
162172
CFLAGS="$CFLAGS -mcx16"
163173
164174
AC_MSG_CHECKING([for __atomic builtin atomic compare-and-swap on 128-bit values with -mcx16 flag])
165-
AC_TRY_LINK([], [__int128 x = 0; __atomic_bool_compare_and_swap (&x, 0, 1);],
175+
AC_TRY_LINK([], [__int128 x = 0, y = 0; __atomic_compare_exchange_n (&x, &y, 1, 0, __ATOMIC_RELAXED, __ATOMIC_RELAXED);],
166176
[AC_MSG_RESULT([yes])
167177
atomic_compare_exchange_n_128_result=1
168178
CFLAGS_save="$CFLAGS"],
@@ -173,7 +183,7 @@ AC_DEFUN([OPAL_CHECK_GCC_BUILTIN_CSWAP_INT128], [
173183
fi
174184
175185
AC_DEFINE_UNQUOTED([OPAL_HAVE_GCC_BUILTIN_CSWAP_INT128], [$atomic_compare_exchange_n_128_result],
176-
[Whether the __atomic builtin atomic compare and swap supports 128-bit values])
186+
[Whether the __atomic builtin atomic compare and swap is lock-free on 128-bit values])
177187
178188
OPAL_VAR_SCOPE_POP
179189
])

opal/include/opal/sys/gcc_builtin/atomic.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,18 @@ static inline int opal_atomic_cmpset_128 (volatile opal_int128_t *addr,
156156
__ATOMIC_ACQUIRE, __ATOMIC_RELAXED);
157157
}
158158

159+
#elif defined(OPAL_HAVE_SYNC_BUILTIN_CSWAP_INT128) && OPAL_HAVE_SYNC_BUILTIN_CSWAP_INT128
160+
161+
#define OPAL_HAVE_ATOMIC_CMPSET_128 1
162+
163+
/* __atomic version is not lock-free so use legacy __sync version */
164+
165+
static inline int opal_atomic_cmpset_128 (volatile opal_int128_t *addr,
166+
opal_int128_t oldval, opal_int128_t newval)
167+
{
168+
return __sync_bool_compare_and_swap (addr, oldval, newval);
169+
}
170+
159171
#endif
160172

161173
#if defined(__HLE__)

0 commit comments

Comments
 (0)