X-Git-Url: http://git.liburcu.org/?p=urcu.git;a=blobdiff_plain;f=urcu%2Fref.h;h=74be50d6bbe4e46d28603ba4cb8de3db2ed7794a;hp=a422a9906fd787d1482210a5a61b6f3611b7f99c;hb=cd21955a846566b3cd7ca77d58cd3d006f3c533a;hpb=54fd78efce9aeddcdc7b35151ad6744b7ee629cb diff --git a/urcu/ref.h b/urcu/ref.h index a422a99..74be50d 100644 --- a/urcu/ref.h +++ b/urcu/ref.h @@ -15,6 +15,7 @@ */ #include +#include #include struct urcu_ref { @@ -45,4 +46,30 @@ 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. + */ +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; + } +} + #endif /* _URCU_REF_H */