From: Martin Willi Date: Wed, 28 May 2014 10:17:15 +0000 (+0200) Subject: utils: Don't directly depend on pthread X-Git-Tag: 5.2.0dr6~24^2~103 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f1c9653e042452f98e810162904f008b071687f9;p=thirdparty%2Fstrongswan.git utils: Don't directly depend on pthread --- diff --git a/src/libstrongswan/utils/utils.c b/src/libstrongswan/utils/utils.c index 8ed0a25dd7..70fd1cb312 100644 --- a/src/libstrongswan/utils/utils.c +++ b/src/libstrongswan/utils/utils.c @@ -24,13 +24,12 @@ #include #include #include -#include -#include "utils.h" - -#include "collections/enumerator.h" -#include "utils/debug.h" -#include "utils/chunk.h" +#include +#include +#include +#include +#include ENUM(status_names, SUCCESS, NEED_MORE, "SUCCESS", @@ -47,28 +46,6 @@ ENUM(status_names, SUCCESS, NEED_MORE, "NEED_MORE", ); -/** - * See header - */ -void utils_init() -{ -#ifdef WIN32 - windows_init(); -#endif /* WIN32 */ - strerror_init(); -} - -/** - * See header - */ -void utils_deinit() -{ -#ifdef WIN32 - windows_deinit(); -#endif /* WIN32 */ - strerror_deinit(); -} - /** * Described in header. */ @@ -547,9 +524,9 @@ void nop() #if !defined(HAVE_GCC_ATOMIC_OPERATIONS) && !defined(HAVE_GCC_SYNC_OPERATIONS) /** - * We use a single mutex for all refcount variables. + * Spinlock for ref_get/put */ -static pthread_mutex_t ref_mutex = PTHREAD_MUTEX_INITIALIZER; +static spinlock_t *ref_lock; /** * Increase refcount @@ -558,9 +535,10 @@ refcount_t ref_get(refcount_t *ref) { refcount_t current; - pthread_mutex_lock(&ref_mutex); + ref_lock->lock(ref_lock); current = ++(*ref); - pthread_mutex_unlock(&ref_mutex); + ref_lock->unlock(ref_lock); + return current; } @@ -571,9 +549,9 @@ bool ref_put(refcount_t *ref) { bool more_refs; - pthread_mutex_lock(&ref_mutex); + ref_lock->lock(ref_lock); more_refs = --(*ref) > 0; - pthread_mutex_unlock(&ref_mutex); + ref_lock->unlock(ref_lock); return !more_refs; } @@ -584,16 +562,17 @@ refcount_t ref_cur(refcount_t *ref) { refcount_t current; - pthread_mutex_lock(&ref_mutex); + ref_lock->lock(ref_lock); current = *ref; - pthread_mutex_unlock(&ref_mutex); + ref_lock->unlock(ref_lock); + return current; } /** - * Single mutex for all compare and swap operations. + * Spinlock for all compare and swap operations. */ -static pthread_mutex_t cas_mutex = PTHREAD_MUTEX_INITIALIZER; +static spinlock_t *cas_lock; /** * Compare and swap if equal to old value @@ -602,9 +581,9 @@ static pthread_mutex_t cas_mutex = PTHREAD_MUTEX_INITIALIZER; bool cas_##name(type *ptr, type oldval, type newval) \ { \ bool swapped; \ - pthread_mutex_lock(&cas_mutex); \ + cas_lock->lock(cas_lock); \ if ((swapped = (*ptr == oldval))) { *ptr = newval; } \ - pthread_mutex_unlock(&cas_mutex); \ + cas_lock->unlock(cas_lock); \ return swapped; \ } @@ -658,6 +637,40 @@ FILE *fmemopen(void *buf, size_t size, const char *mode) #endif /* FMEMOPEN fallback*/ +/** + * See header + */ +void utils_init() +{ +#ifdef WIN32 + windows_init(); +#endif + +#if !defined(HAVE_GCC_ATOMIC_OPERATIONS) && !defined(HAVE_GCC_SYNC_OPERATIONS) + ref_lock = spinlock_create(); + cas_lock = spinlock_create(); +#endif + + strerror_init(); +} + +/** + * See header + */ +void utils_deinit() +{ +#ifdef WIN32 + windows_deinit(); +#endif + +#if !defined(HAVE_GCC_ATOMIC_OPERATIONS) && !defined(HAVE_GCC_SYNC_OPERATIONS) + ref_lock->destroy(ref_lock); + cas_lock->destroy(cas_lock); +#endif + + strerror_deinit(); +} + /** * Described in header. */