1 // SPDX-FileCopyrightText: 2009 Novell Inc.
2 // SPDX-FileCopyrightText: 2010 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 // SPDX-License-Identifier: LGPL-2.1-only
10 * Userspace RCU - Reference counting
12 * Author: Jan Blunck <jblunck@suse.de>
18 #include <urcu/assert.h>
19 #include <urcu/uatomic.h>
22 long refcount
; /* ATOMIC */
25 static inline void urcu_ref_set(struct urcu_ref
*ref
, long val
)
27 uatomic_set(&ref
->refcount
, val
);
30 static inline void urcu_ref_init(struct urcu_ref
*ref
)
35 static inline bool __attribute__((warn_unused_result
))
36 urcu_ref_get_safe(struct urcu_ref
*ref
)
40 old
= uatomic_read(&ref
->refcount
);
42 if (old
== LONG_MAX
) {
43 return false; /* Failure. */
46 res
= uatomic_cmpxchg(&ref
->refcount
, old
, _new
);
48 return true; /* Success. */
54 static inline void urcu_ref_get(struct urcu_ref
*ref
)
56 if (!urcu_ref_get_safe(ref
))
60 static inline void urcu_ref_put(struct urcu_ref
*ref
,
61 void (*release
)(struct urcu_ref
*))
63 long res
= uatomic_sub_return(&ref
->refcount
, 1);
64 urcu_posix_assert(res
>= 0);
70 * urcu_ref_get_unless_zero
72 * Allows getting a reference atomically if the reference count is not
73 * zero. Returns true if the reference is taken, false otherwise. This
74 * needs to be used in conjunction with another synchronization
75 * technique (e.g. RCU or mutex) to ensure existence of the reference
76 * count. False is also returned in case incrementing the refcount would
77 * result in an overflow.
79 static inline bool urcu_ref_get_unless_zero(struct urcu_ref
*ref
)
83 old
= uatomic_read(&ref
->refcount
);
85 if (old
== 0 || old
== LONG_MAX
)
86 return false; /* Failure. */
88 res
= uatomic_cmpxchg(&ref
->refcount
, old
, _new
);
90 return true; /* Success. */
96 #endif /* _URCU_REF_H */
This page took 0.033196 seconds and 4 git commands to generate.