From efd0fe21e44f0f757b3cd80cdbaddb8423eeb410 Mon Sep 17 00:00:00 2001 From: Martin Willi Date: Tue, 2 Dec 2008 12:14:32 +0000 Subject: [PATCH] ref_get()/ref_put() use atomic gcc operations if supported, thanks to Thomas Jarosch for the patch --- configure.in | 15 +++++++++++++++ src/libstrongswan/utils.c | 20 +++++++------------- src/libstrongswan/utils.h | 10 ++++++++++ 3 files changed, 32 insertions(+), 13 deletions(-) diff --git a/configure.in b/configure.in index a251c99125..ea9b6dd203 100644 --- a/configure.in +++ b/configure.in @@ -708,6 +708,21 @@ AC_HAVE_LIBRARY(dl) AC_CHECK_FUNCS(backtrace) AC_CHECK_FUNCS(dladdr) +AC_MSG_CHECKING([for gcc atomic operations]) +AC_TRY_RUN( +[ + int main() { + volatile int ref = 1; + __sync_fetch_and_add (&ref, 1); + __sync_sub_and_fetch (&ref, 1); + /* Make sure test fails if operations are not supported */ + __sync_val_compare_and_swap(&ref, 1, 0); + return ref; + } +], +[AC_MSG_RESULT([yes]); AC_DEFINE(HAVE_GCC_ATOMIC_OPERATIONS)], +[AC_MSG_RESULT([no])]) + if test x$gmp = xtrue; then AC_HAVE_LIBRARY([gmp],[LIBS="$LIBS"],[AC_MSG_ERROR([GNU Multi Precision library gmp not found])]) AC_MSG_CHECKING([gmp.h version >= 4.1.4]) diff --git a/src/libstrongswan/utils.c b/src/libstrongswan/utils.c index 942f2ade04..aa50b86b4f 100644 --- a/src/libstrongswan/utils.c +++ b/src/libstrongswan/utils.c @@ -20,7 +20,6 @@ #include #include -#include #include #include #include @@ -138,19 +137,16 @@ void nop() { } +#ifndef HAVE_GCC_ATOMIC_OPERATIONS +#include + /** - * We use a single mutex for all refcount variables. This - * is not optimal for performance, but the critical section - * is not that long... - * TODO: Consider to include a mutex in each refcount_t variable. + * We use a single mutex for all refcount variables. */ static pthread_mutex_t ref_mutex = PTHREAD_MUTEX_INITIALIZER; /** - * Described in header. - * - * TODO: May be implemented with atomic CPU instructions - * instead of a mutex. + * Increase refcount */ void ref_get(refcount_t *ref) { @@ -160,10 +156,7 @@ void ref_get(refcount_t *ref) } /** - * Described in header. - * - * TODO: May be implemented with atomic CPU instructions - * instead of a mutex. + * Decrease refcount */ bool ref_put(refcount_t *ref) { @@ -174,6 +167,7 @@ bool ref_put(refcount_t *ref) pthread_mutex_unlock(&ref_mutex); return !more_refs; } +#endif /* HAVE_GCC_ATOMIC_OPERATIONS */ /** * output handler in printf() for time_t diff --git a/src/libstrongswan/utils.h b/src/libstrongswan/utils.h index af619ea4b7..298253fdd4 100644 --- a/src/libstrongswan/utils.h +++ b/src/libstrongswan/utils.h @@ -248,6 +248,14 @@ void nop(); */ typedef volatile u_int refcount_t; + +#ifdef HAVE_GCC_ATOMIC_OPERATIONS + +#define ref_get(ref) {__sync_fetch_and_add(ref, 1); } +#define ref_put(ref) (!__sync_sub_and_fetch(ref, 1)) + +#else /* !HAVE_GCC_ATOMIC_OPERATIONS */ + /** * Get a new reference. * @@ -268,6 +276,8 @@ void ref_get(refcount_t *ref); */ bool ref_put(refcount_t *ref); +#endif /* HAVE_GCC_ATOMIC_OPERATIONS */ + /** * Get printf hooks for time. * -- 2.47.2