summary |
shortlog |
log |
commit | commitdiff |
tree
raw |
patch |
inline | side by side (from parent 1:
54fd78e)
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.
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
#include <urcu/uatomic.h>
struct urcu_ref {
#include <urcu/uatomic.h>
struct urcu_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.
+ */
+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)
+ return false; /* Failure. */
+ _new = old + 1;
+ res = uatomic_cmpxchg(&ref->refcount, old, _new);
+ if (res == old) {
+ return true; /* Success. */
+ }
+ old = res;
+ }
+}
+