diff options
author | Tobias Brunner <tobias@strongswan.org> | 2011-12-23 11:04:55 +0100 |
---|---|---|
committer | Tobias Brunner <tobias@strongswan.org> | 2011-12-23 11:04:55 +0100 |
commit | 5317dd6887d4588a36138e74b83643c258e870b0 (patch) | |
tree | 2f60fba54776c3f54688eae4b93ed6cb039fc7b5 /src/libstrongswan/utils.h | |
parent | d6656f11e48d9b545738d5f40e59f3c3dd90ef97 (diff) | |
download | strongswan-5317dd6887d4588a36138e74b83643c258e870b0.tar.bz2 strongswan-5317dd6887d4588a36138e74b83643c258e870b0.tar.xz |
Added atomic compare and swap operations.
Using a GCC atomic builtin if available or a global mutex otherwise.
Diffstat (limited to 'src/libstrongswan/utils.h')
-rw-r--r-- | src/libstrongswan/utils.h | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/src/libstrongswan/utils.h b/src/libstrongswan/utils.h index 3014e2f60..3ddaea089 100644 --- a/src/libstrongswan/utils.h +++ b/src/libstrongswan/utils.h @@ -565,6 +565,11 @@ typedef volatile u_int refcount_t; #define ref_get(ref) {__sync_fetch_and_add(ref, 1); } #define ref_put(ref) (!__sync_sub_and_fetch(ref, 1)) +#define cas_bool(ptr, oldval, newval) \ + (__sync_bool_compare_and_swap(ptr, oldval, newval)) +#define cas_ptr(ptr, oldval, newval) \ + (__sync_bool_compare_and_swap(ptr, oldval, newval)) + #else /* !HAVE_GCC_ATOMIC_OPERATIONS */ /** @@ -587,6 +592,27 @@ void ref_get(refcount_t *ref); */ bool ref_put(refcount_t *ref); +/** + * Atomically replace value of ptr with newval if it currently equals oldval. + * + * @param ptr pointer to variable + * @param oldval old value of the variable + * @param newval new value set if possible + * @return TRUE if value equaled oldval and newval was written + */ +bool cas_bool(bool *ptr, bool oldval, bool newval); + +/** + * Atomically replace value of ptr with newval if it currently equals oldval. + * + * @param ptr pointer to variable + * @param oldval old value of the variable + * @param newval new value set if possible + * @return TRUE if value equaled oldval and newval was written + */ +bool cas_ptr(void **ptr, void *oldval, void *newval); + + #endif /* HAVE_GCC_ATOMIC_OPERATIONS */ /** |