]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
Added atomic compare and swap operations.
authorTobias Brunner <tobias@strongswan.org>
Fri, 23 Dec 2011 10:04:55 +0000 (11:04 +0100)
committerTobias Brunner <tobias@strongswan.org>
Fri, 23 Dec 2011 10:04:55 +0000 (11:04 +0100)
Using a GCC atomic builtin if available or a global mutex otherwise.

src/libstrongswan/utils.c
src/libstrongswan/utils.h

index 2fe7f653c06d52012677d23599041b4512ad755d..5a104de7fc3b2e5128222dbe230baa69676dc7ea 100644 (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 */
 
 /**
index 3014e2f60fb5961de0a46d93e2e1967b7f1ce0ed..3ddaea0895f2e92ec75d2979de75998adf828851 100644 (file)
@@ -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 */
 
 /**