Fix: tests: invoke destroy APIs for queues/stacks
[urcu.git] / urcu / ref.h
index a422a9906fd787d1482210a5a61b6f3611b7f99c..e546da567120e086e5773950ac135d510900b4fc 100644 (file)
@@ -15,6 +15,9 @@
  */
 
 #include <assert.h>
+#include <stdbool.h>
+#include <limits.h>
+#include <stdlib.h>
 #include <urcu/uatomic.h>
 
 struct urcu_ref {
@@ -31,9 +34,29 @@ static inline void urcu_ref_init(struct urcu_ref *ref)
        urcu_ref_set(ref, 1);
 }
 
+static inline bool  __attribute__((warn_unused_result))
+               urcu_ref_get_safe(struct urcu_ref *ref)
+{
+       long old, _new, res;
+
+       old = uatomic_read(&ref->refcount);
+       for (;;) {
+               if (old == LONG_MAX) {
+                       return false;   /* Failure. */
+               }
+               _new = old + 1;
+               res = uatomic_cmpxchg(&ref->refcount, old, _new);
+               if (res == old) {
+                       return true;    /* Success. */
+               }
+               old = res;
+       }
+}
+
 static inline void urcu_ref_get(struct urcu_ref *ref)
 {
-       uatomic_add(&ref->refcount, 1);
+       if (!urcu_ref_get_safe(ref))
+               abort();
 }
 
 static inline void urcu_ref_put(struct urcu_ref *ref,
@@ -45,4 +68,31 @@ static inline void urcu_ref_put(struct urcu_ref *ref,
                release(ref);
 }
 
+/*
+ * urcu_ref_get_unless_zero
+ *
+ * Allows getting a reference atomically if the reference count is not
+ * zero. Returns true if the reference is taken, false otherwise. This
+ * needs to be used in conjunction with another synchronization
+ * technique (e.g.  RCU or mutex) to ensure existence of the reference
+ * count. False is also returned in case incrementing the refcount would
+ * result in an overflow.
+ */
+static inline bool urcu_ref_get_unless_zero(struct urcu_ref *ref)
+{
+       long old, _new, res;
+
+       old = uatomic_read(&ref->refcount);
+       for (;;) {
+               if (old == 0 || old == LONG_MAX)
+                       return false;   /* Failure. */
+               _new = old + 1;
+               res = uatomic_cmpxchg(&ref->refcount, old, _new);
+               if (res == old) {
+                       return true;    /* Success. */
+               }
+               old = res;
+       }
+}
+
 #endif /* _URCU_REF_H */
This page took 0.023727 seconds and 4 git commands to generate.