From: Mathieu Desnoyers Date: Thu, 21 Jan 2016 16:45:58 +0000 (-0500) Subject: urcu_ref_get_safe: introduce new API X-Git-Tag: v0.10.0~55 X-Git-Url: https://git.liburcu.org/?p=urcu.git;a=commitdiff_plain;h=8b4b5a1a5f8587cb995391e4f49d03674ec60c0c urcu_ref_get_safe: introduce new API Add a urcu_ref_get_safe API, which returns a boolean. It takes the value "false" if a LONG_MAX overflow would occur (get is not performed in this case), or true otherwise. It warns the user (at compile-time) if the return value is unchecked. Signed-off-by: Mathieu Desnoyers --- diff --git a/urcu/ref.h b/urcu/ref.h index 2b803e5..e546da5 100644 --- a/urcu/ref.h +++ b/urcu/ref.h @@ -34,24 +34,31 @@ static inline void urcu_ref_init(struct urcu_ref *ref) urcu_ref_set(ref, 1); } -static inline void urcu_ref_get(struct urcu_ref *ref) +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) { - abort(); + return false; /* Failure. */ } _new = old + 1; res = uatomic_cmpxchg(&ref->refcount, old, _new); if (res == old) { - return; + return true; /* Success. */ } old = res; } } +static inline void urcu_ref_get(struct urcu_ref *ref) +{ + if (!urcu_ref_get_safe(ref)) + abort(); +} + static inline void urcu_ref_put(struct urcu_ref *ref, void (*release)(struct urcu_ref *)) {