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 void urcu_ref_get(struct urcu_ref
*ref
)
41 old
= uatomic_read(&ref
->refcount
);
43 if (old
== LONG_MAX
) {
47 res
= uatomic_cmpxchg(&ref
->refcount
, old
, _new
);
55 static inline void urcu_ref_put(struct urcu_ref
*ref
,
56 void (*release
)(struct urcu_ref
*))
58 long res
= uatomic_sub_return(&ref
->refcount
, 1);
65 * urcu_ref_get_unless_zero
67 * Allows getting a reference atomically if the reference count is not
68 * zero. Returns true if the reference is taken, false otherwise. This
69 * needs to be used in conjunction with another synchronization
70 * technique (e.g. RCU or mutex) to ensure existence of the reference
71 * count. False is also returned in case incrementing the refcount would
72 * result in an overflow.
74 static inline bool urcu_ref_get_unless_zero(struct urcu_ref
*ref
)
78 old
= uatomic_read(&ref
->refcount
);
80 if (old
== 0 || old
== LONG_MAX
)
81 return false; /* Failure. */
83 res
= uatomic_cmpxchg(&ref
->refcount
, old
, _new
);
85 return true; /* Success. */
91 #endif /* _URCU_REF_H */
This page took 0.032153 seconds and 4 git commands to generate.