Add atomic(9) implementations of atomic operations.

Add atomic(9) implementations of atomic operations.  These are used on
FreeBSD for non-x86 architectures.
This commit is contained in:
Jason Evans 2012-04-17 13:17:54 -07:00
parent 45f208e112
commit b57d3ec571
4 changed files with 72 additions and 6 deletions

View File

@ -980,6 +980,29 @@ if test "x${je_cv_function_ffsl}" != "xyes" ; then
AC_MSG_ERROR([Cannot build without ffsl(3)]) AC_MSG_ERROR([Cannot build without ffsl(3)])
fi fi
dnl ============================================================================
dnl Check for atomic(9) operations as provided on FreeBSD.
JE_COMPILABLE([atomic(9)], [
#include <sys/types.h>
#include <machine/atomic.h>
#include <inttypes.h>
], [
{
uint32_t x32 = 0;
volatile uint32_t *x32p = &x32;
atomic_fetchadd_32(x32p, 1);
}
{
unsigned long xlong = 0;
volatile unsigned long *xlongp = &xlong;
atomic_fetchadd_long(xlongp, 1);
}
], [je_cv_atomic9])
if test "x${je_cv_atomic9}" = "xyes" ; then
AC_DEFINE([JEMALLOC_ATOMIC9])
fi
dnl ============================================================================ dnl ============================================================================
dnl Check for atomic(3) operations as provided on Darwin. dnl Check for atomic(3) operations as provided on Darwin.
@ -1031,7 +1054,7 @@ AC_DEFUN([JE_SYNC_COMPARE_AND_SWAP_CHECK],[
fi fi
]) ])
if test "x${je_cv_osatomic}" != "xyes" ; then if test "x${je_cv_atomic9}" != "xyes" -a "x${je_cv_osatomic}" != "xyes" ; then
JE_SYNC_COMPARE_AND_SWAP_CHECK(32, 4) JE_SYNC_COMPARE_AND_SWAP_CHECK(32, 4)
JE_SYNC_COMPARE_AND_SWAP_CHECK(64, 8) JE_SYNC_COMPARE_AND_SWAP_CHECK(64, 8)
fi fi

View File

@ -32,6 +32,7 @@ unsigned atomic_sub_u(unsigned *p, unsigned x);
#if (defined(JEMALLOC_ENABLE_INLINE) || defined(JEMALLOC_ATOMIC_C_)) #if (defined(JEMALLOC_ENABLE_INLINE) || defined(JEMALLOC_ATOMIC_C_))
/******************************************************************************/ /******************************************************************************/
/* 64-bit operations. */ /* 64-bit operations. */
#if (LG_SIZEOF_PTR == 3 || LG_SIZEOF_INT == 3)
# ifdef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_8 # ifdef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_8
JEMALLOC_INLINE uint64_t JEMALLOC_INLINE uint64_t
atomic_add_uint64(uint64_t *p, uint64_t x) atomic_add_uint64(uint64_t *p, uint64_t x)
@ -87,6 +88,28 @@ atomic_sub_uint64(uint64_t *p, uint64_t x)
return (x); return (x);
} }
# elif (defined(JEMALLOC_ATOMIC9))
JEMALLOC_INLINE uint64_t
atomic_add_uint64(uint64_t *p, uint64_t x)
{
/*
* atomic_fetchadd_64() doesn't exist, but we only ever use this
* function on LP64 systems, so atomic_fetchadd_long() will do.
*/
assert(sizeof(uint64_t) == sizeof(unsigned long));
return (atomic_fetchadd_long(p, (unsigned long)x) + x);
}
JEMALLOC_INLINE uint64_t
atomic_sub_uint64(uint64_t *p, uint64_t x)
{
assert(sizeof(uint64_t) == sizeof(unsigned long));
return (atomic_fetchadd_long(p, (unsigned long)(-(long)x)) - x);
}
# elif (defined(JE_FORCE_SYNC_COMPARE_AND_SWAP_8)) # elif (defined(JE_FORCE_SYNC_COMPARE_AND_SWAP_8))
JEMALLOC_INLINE uint64_t JEMALLOC_INLINE uint64_t
atomic_add_uint64(uint64_t *p, uint64_t x) atomic_add_uint64(uint64_t *p, uint64_t x)
@ -102,7 +125,6 @@ atomic_sub_uint64(uint64_t *p, uint64_t x)
return (__sync_sub_and_fetch(p, x)); return (__sync_sub_and_fetch(p, x));
} }
# else # else
# if (LG_SIZEOF_PTR == 3)
# error "Missing implementation for 64-bit atomic operations" # error "Missing implementation for 64-bit atomic operations"
# endif # endif
#endif #endif
@ -164,6 +186,20 @@ atomic_sub_uint32(uint32_t *p, uint32_t x)
return (x); return (x);
} }
#elif (defined(JEMALLOC_ATOMIC9))
JEMALLOC_INLINE uint32_t
atomic_add_uint32(uint32_t *p, uint32_t x)
{
return (atomic_fetchadd_32(p, x) + x);
}
JEMALLOC_INLINE uint32_t
atomic_sub_uint32(uint32_t *p, uint32_t x)
{
return (atomic_fetchadd_32(p, (uint32_t)(-(int32_t)x)) - x);
}
#elif (defined(JE_FORCE_SYNC_COMPARE_AND_SWAP_4)) #elif (defined(JE_FORCE_SYNC_COMPARE_AND_SWAP_4))
JEMALLOC_INLINE uint32_t JEMALLOC_INLINE uint32_t
atomic_add_uint32(uint32_t *p, uint32_t x) atomic_add_uint32(uint32_t *p, uint32_t x)

View File

@ -161,6 +161,10 @@ static const bool config_ivsalloc =
#endif #endif
; ;
#ifdef JEMALLOC_ATOMIC9
#include <machine/atomic.h>
#endif
#if (defined(JEMALLOC_OSATOMIC) || defined(JEMALLOC_OSSPIN)) #if (defined(JEMALLOC_OSATOMIC) || defined(JEMALLOC_OSSPIN))
#include <libkern/OSAtomic.h> #include <libkern/OSAtomic.h>
#endif #endif

View File

@ -47,6 +47,9 @@
*/ */
#undef CPU_SPINWAIT #undef CPU_SPINWAIT
/* Defined if the equivalent of FreeBSD's atomic(9) functions are available. */
#undef JEMALLOC_ATOMIC9
/* /*
* Defined if OSAtomic*() functions are available, as provided by Darwin, and * Defined if OSAtomic*() functions are available, as provided by Darwin, and
* documented in the atomic(3) manual page. * documented in the atomic(3) manual page.