aboutsummaryrefslogtreecommitdiffstats
path: root/src/libstrongswan/utils.c
diff options
context:
space:
mode:
authorTobias Brunner <tobias@strongswan.org>2011-12-23 11:04:55 +0100
committerTobias Brunner <tobias@strongswan.org>2011-12-23 11:04:55 +0100
commit5317dd6887d4588a36138e74b83643c258e870b0 (patch)
tree2f60fba54776c3f54688eae4b93ed6cb039fc7b5 /src/libstrongswan/utils.c
parentd6656f11e48d9b545738d5f40e59f3c3dd90ef97 (diff)
downloadstrongswan-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.c')
-rw-r--r--src/libstrongswan/utils.c22
1 files changed, 22 insertions, 0 deletions
diff --git a/src/libstrongswan/utils.c b/src/libstrongswan/utils.c
index 2fe7f653c..5a104de7f 100644
--- a/src/libstrongswan/utils.c
+++ b/src/libstrongswan/utils.c
@@ -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 */
/**