NetBSD specific atomic operation implementation

Signed-off-by: Lennart Poettering <lennart@poettering.net>
This commit is contained in:
Jared D. McNeill 2009-01-22 01:52:35 +01:00 committed by Lennart Poettering
parent cc425ed260
commit 8d89ccdcf2
2 changed files with 150 additions and 53 deletions

View file

@ -129,16 +129,31 @@ AC_ARG_ENABLE([atomic-arm-memory-barrier],
esac
],)
AC_ARG_ENABLE([netbsd-atomic-ops],
AS_HELP_STRING([--enable-netbsd-atomic-ops],[Use the native NetBSD atomic_ops implementation]),
[
case "${enableval}" in
yes) atomic_netbsd_helpers=yes ;;
no) atomic_netbsd_helpers=no ;;
*) AC_MSG_ERROR(bad value ${enableval} for --enable-netbsd-atomic-ops) ;;
esac
],
[atomic_netbsd_helpers=auto])
AC_MSG_CHECKING([target operating system])
case $host in
*-*-linux*)
AC_MSG_RESULT([linux])
pulse_target_os=linux
;;
*)
AC_MSG_RESULT([unknown])
pulse_target_os=unknown
;;
*-*-linux*)
AC_MSG_RESULT([linux])
pulse_target_os=linux
;;
*-*-netbsd*)
AC_MSG_RESULT([netbsd])
pulse_target_os=netbsd
;;
*)
AC_MSG_RESULT([unknown])
pulse_target_os=unknown
;;
esac
# If everything else fails use libatomic_ops
@ -160,42 +175,48 @@ else
AC_MSG_CHECKING([architecture for native atomic operations])
case $host_cpu in
arm*)
AC_MSG_RESULT([arm])
AC_MSG_CHECKING([whether we can use Linux kernel helpers])
# The Linux kernel helper functions have been there since 2.6.16. However
# compile time checking for kernel version in cross compile environment
# (which is usually the case for arm cpu) is tricky (or impossible).
if test "x$pulse_target_os" = "xlinux" && test "x$atomic_arm_linux_helpers" != "xno"; then
AC_MSG_RESULT([yes])
AC_DEFINE_UNQUOTED(ATOMIC_ARM_LINUX_HELPERS, 1, [special arm linux implementation])
need_libatomic_ops=no
else
AC_MSG_RESULT([no])
AC_CACHE_CHECK([compiler support for arm inline asm atomic operations],
pulseaudio_cv_support_arm_atomic_ops,
[AC_COMPILE_IFELSE(
AC_LANG_PROGRAM([],
[[volatile int a=0;
int o=0, n=1, r;
asm volatile ("ldrex %0, [%1]\n"
"subs %0, %0, %2\n"
"strexeq %0, %3, [%1]\n"
: "=&r" (r)
: "r" (&a), "Ir" (o), "r" (n)
: "cc");
AC_MSG_RESULT([arm])
AC_MSG_CHECKING([whether we can use Linux kernel helpers])
# The Linux kernel helper functions have been there since 2.6.16. However
# compile time checking for kernel version in cross compile environment
# (which is usually the case for arm cpu) is tricky (or impossible).
if test "x$pulse_target_os" = "xlinux" && test "x$atomic_arm_linux_helpers" != "xno"; then
AC_MSG_RESULT([yes])
AC_DEFINE_UNQUOTED(ATOMIC_ARM_LINUX_HELPERS, 1, [special arm linux implementation])
need_libatomic_ops=no
else
AC_MSG_RESULT([no])
AC_CACHE_CHECK([compiler support for arm inline asm atomic operations],
pulseaudio_cv_support_arm_atomic_ops,
[AC_COMPILE_IFELSE(
AC_LANG_PROGRAM([],
[[volatile int a=0;
int o=0, n=1, r;
asm volatile ("ldrex %0, [%1]\n"
"subs %0, %0, %2\n"
"strexeq %0, %3, [%1]\n"
: "=&r" (r)
: "r" (&a), "Ir" (o), "r" (n)
: "cc");
return (a==1 ? 0 : -1);
]]),
[pulseaudio_cv_support_arm_atomic_ops=yes],
[pulseaudio_cv_support_arm_atomic_ops=no])
])
AS_IF([test "$pulseaudio_cv_support_arm_atomic_ops" = "yes"], [
AC_DEFINE([ATOMIC_ARM_INLINE_ASM], 1, [Have ARMv6 instructions.])
need_libatomic_ops=no
])
fi
;;
]]),
[pulseaudio_cv_support_arm_atomic_ops=yes],
[pulseaudio_cv_support_arm_atomic_ops=no])
])
AS_IF([test "$pulseaudio_cv_support_arm_atomic_ops" = "yes"], [
AC_DEFINE([ATOMIC_ARM_INLINE_ASM], 1, [Have ARMv6 instructions.])
need_libatomic_ops=no
])
fi
;;
*)
AC_MSG_RESULT([unknown])
if test "x$pulse_target_os" = "xnetbsd" && test "x$atomic_netbsd_helpers" = "xyes"; then
AC_MSG_RESULT([yes])
AC_DEFINE_UNQUOTED(NETBSD_ATOMIC_OPS, 1, [netbsd implementation])
need_libatomic_ops=no
else
AC_MSG_RESULT([unknown])
fi
;;
esac
fi
@ -225,11 +246,11 @@ LTDL_INIT([convenience recursive])
os_is_win32=0
case "$host_os" in
mingw*)
mingw*)
AC_DEFINE([OS_IS_WIN32], 1, [Build target is Windows.])
os_is_win32=1
;;
esac
;;
esac
AM_CONDITIONAL(OS_IS_WIN32, test "x$os_is_win32" = "x1")
@ -248,14 +269,14 @@ AC_CHECK_HEADERS([arpa/inet.h glob.h grp.h netdb.h netinet/in.h \
sys/mman.h sys/resource.h sys/select.h sys/socket.h sys/wait.h \
sys/uio.h syslog.h sys/dl.h dlfcn.h linux/sockios.h])
AC_CHECK_HEADERS([netinet/ip.h], [], [],
[#include <sys/types.h>
#if HAVE_NETINET_IN_H
# include <netinet/in.h>
#endif
#if HAVE_NETINET_IN_SYSTM_H
# include <netinet/in_systm.h>
#endif
])
[#include <sys/types.h>
#if HAVE_NETINET_IN_H
# include <netinet/in.h>
#endif
#if HAVE_NETINET_IN_SYSTM_H
# include <netinet/in_systm.h>
#endif
])
AC_CHECK_HEADERS([regex.h], [HAVE_REGEX=1], [HAVE_REGEX=0])
AC_CHECK_HEADERS([sys/un.h], [HAVE_AF_UNIX=1], [HAVE_AF_UNIX=0])
@ -275,6 +296,9 @@ AC_CHECK_HEADERS([sys/filio.h])
# Windows
AC_CHECK_HEADERS([windows.h winsock2.h ws2tcpip.h])
# NetBSD
AC_CHECK_HEADERS([sys/atomic.h])
# Other
AC_CHECK_HEADERS([sys/ioctl.h])
AC_CHECK_HEADERS([byteswap.h])

View file

@ -107,6 +107,79 @@ static inline pa_bool_t pa_atomic_ptr_cmpxchg(pa_atomic_ptr_t *a, void *old_p, v
return __sync_bool_compare_and_swap(&a->value, (long) old_p, (long) new_p);
}
#elif defined(__NetBSD__) && defined(HAVE_SYS_ATOMIC_H)
/* NetBSD 5.0+ atomic_ops(3) implementation */
#include <sys/atomic.h>
typedef struct pa_atomic {
volatile unsigned int value;
} pa_atomic_t;
#define PA_ATOMIC_INIT(v) { .value = (unsigned int) (v) }
static inline int pa_atomic_load(const pa_atomic_t *a) {
membar_sync();
return (int) a->value;
}
static inline void pa_atomic_store(pa_atomic_t *a, int i) {
a->value = (unsigned int) i;
membar_sync();
}
/* Returns the previously set value */
static inline int pa_atomic_add(pa_atomic_t *a, int i) {
int nv = (int) atomic_add_int_nv(&a->value, i);
return nv - i;
}
/* Returns the previously set value */
static inline int pa_atomic_sub(pa_atomic_t *a, int i) {
int nv = (int) atomic_add_int_nv(&a->value, -i);
return nv + i;
}
/* Returns the previously set value */
static inline int pa_atomic_inc(pa_atomic_t *a) {
int nv = (int) atomic_inc_uint_nv(&a->value);
return nv - 1;
}
/* Returns the previously set value */
static inline int pa_atomic_dec(pa_atomic_t *a) {
int nv = (int) atomic_dec_uint_nv(&a->value);
return nv + 1;
}
/* Returns TRUE when the operation was successful. */
static inline pa_bool_t pa_atomic_cmpxchg(pa_atomic_t *a, int old_i, int new_i) {
unsigned int r = atomic_cas_uint(&a->value, (unsigned int) old_i, (unsigned int) new_i);
return (int) r == old_i;
}
typedef struct pa_atomic_ptr {
volatile void *value;
} pa_atomic_ptr_t;
#define PA_ATOMIC_PTR_INIT(v) { .value = (v) }
static inline void* pa_atomic_ptr_load(const pa_atomic_ptr_t *a) {
membar_sync();
return (void *) a->value;
}
static inline void pa_atomic_ptr_store(pa_atomic_ptr_t *a, void *p) {
a->value = p;
membar_sync();
}
static inline pa_bool_t pa_atomic_ptr_cmpxchg(pa_atomic_ptr_t *a, void *old_p, void* new_p) {
void *r = atomic_cas_ptr(&a->value, old_p, new_p);
return r == old_p;
}
#elif defined(__GNUC__) && (defined(__amd64__) || defined(__x86_64__))
#warn "The native atomic operations implementation for AMD64 has not been tested thoroughly. libatomic_ops is known to not work properly on AMD64 and your gcc version is too old for the gcc-builtin atomic ops support. You have three options now: test the native atomic operations implementation for AMD64, fix libatomic_ops, or upgrade your GCC."