5 * Userspace RCU - Reference counting
7 * Copyright (C) 2009 Novell Inc.
8 * Copyright (C) 2010 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 * Author: Jan Blunck <jblunck@suse.de>
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU Lesser General Public License version 2.1 as
14 * published by the Free Software Foundation.
21 #include <urcu/uatomic.h>
24 long refcount
; /* ATOMIC */
27 static inline void urcu_ref_set(struct urcu_ref
*ref
, long val
)
29 uatomic_set(&ref
->refcount
, val
);
32 static inline void urcu_ref_init(struct urcu_ref
*ref
)
37 static inline bool __attribute__((warn_unused_result
))
38 urcu_ref_get_safe(struct urcu_ref
*ref
)
42 old
= uatomic_read(&ref
->refcount
);
44 if (old
== LONG_MAX
) {
45 return false; /* Failure. */
48 res
= uatomic_cmpxchg(&ref
->refcount
, old
, _new
);
50 return true; /* Success. */
56 static inline void urcu_ref_get(struct urcu_ref
*ref
)
58 if (!urcu_ref_get_safe(ref
))
62 static inline void urcu_ref_put(struct urcu_ref
*ref
,
63 void (*release
)(struct urcu_ref
*))
65 long res
= uatomic_sub_return(&ref
->refcount
, 1);
72 * urcu_ref_get_unless_zero
74 * Allows getting a reference atomically if the reference count is not
75 * zero. Returns true if the reference is taken, false otherwise. This
76 * needs to be used in conjunction with another synchronization
77 * technique (e.g. RCU or mutex) to ensure existence of the reference
78 * count. False is also returned in case incrementing the refcount would
79 * result in an overflow.
81 static inline bool urcu_ref_get_unless_zero(struct urcu_ref
*ref
)
85 old
= uatomic_read(&ref
->refcount
);
87 if (old
== 0 || old
== LONG_MAX
)
88 return false; /* Failure. */
90 res
= uatomic_cmpxchg(&ref
->refcount
, old
, _new
);
92 return true; /* Success. */
98 #endif /* _URCU_REF_H */
This page took 0.035367 seconds and 4 git commands to generate.