Added atomic compare and swap operations.

Using a GCC atomic builtin if available or a global mutex otherwise.
This commit is contained in:
Tobias Brunner
2011-12-23 11:04:55 +01:00
parent d6656f11e4
commit 5317dd6887
2 changed files with 48 additions and 0 deletions
+22
View File
@@ -344,6 +344,28 @@ bool ref_put(refcount_t *ref)
pthread_mutex_unlock(&ref_mutex);
return !more_refs;
}
/**
* Single mutex for all compare and swap operations.
*/
static pthread_mutex_t cas_mutex = PTHREAD_MUTEX_INITIALIZER;
/**
* Compare and swap if equal to old value
*/
#define _cas_impl(name, type) \
bool cas_##name(type *ptr, type oldval, type newval) \
{ \
bool swapped; \
pthread_mutex_lock(&cas_mutex); \
if ((swapped = (*ptr == oldval))) { *ptr = newval; } \
pthread_mutex_unlock(&cas_mutex); \
return swapped; \
}
_cas_impl(bool, bool)
_cas_impl(ptr, void*)
#endif /* HAVE_GCC_ATOMIC_OPERATIONS */
/**