Commit | Line | Data |
---|---|---|
453629a9 MD |
1 | #ifndef _URCU_REF_H |
2 | #define _URCU_REF_H | |
3 | ||
4 | /* | |
5 | * Userspace RCU - Reference counting | |
6 | * | |
7 | * Copyright (C) 2009 Novell Inc. | |
8 | * Copyright (C) 2010 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
9 | * | |
10 | * Author: Jan Blunck <jblunck@suse.de> | |
11 | * | |
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. | |
15 | */ | |
16 | ||
17 | #include <assert.h> | |
cd21955a | 18 | #include <stdbool.h> |
440b950d MD |
19 | #include <limits.h> |
20 | #include <stdlib.h> | |
a2e7bf9c | 21 | #include <urcu/uatomic.h> |
453629a9 MD |
22 | |
23 | struct urcu_ref { | |
24 | long refcount; /* ATOMIC */ | |
25 | }; | |
26 | ||
27 | static inline void urcu_ref_set(struct urcu_ref *ref, long val) | |
28 | { | |
29 | uatomic_set(&ref->refcount, val); | |
30 | } | |
31 | ||
32 | static inline void urcu_ref_init(struct urcu_ref *ref) | |
33 | { | |
34 | urcu_ref_set(ref, 1); | |
35 | } | |
36 | ||
37 | static inline void urcu_ref_get(struct urcu_ref *ref) | |
38 | { | |
440b950d MD |
39 | long old, _new, res; |
40 | ||
41 | old = uatomic_read(&ref->refcount); | |
42 | for (;;) { | |
43 | if (old == LONG_MAX) { | |
44 | abort(); | |
45 | } | |
46 | _new = old + 1; | |
47 | res = uatomic_cmpxchg(&ref->refcount, old, _new); | |
48 | if (res == old) { | |
49 | return; | |
50 | } | |
51 | old = res; | |
52 | } | |
453629a9 MD |
53 | } |
54 | ||
55 | static inline void urcu_ref_put(struct urcu_ref *ref, | |
56 | void (*release)(struct urcu_ref *)) | |
57 | { | |
bf27322d PB |
58 | long res = uatomic_sub_return(&ref->refcount, 1); |
59 | assert (res >= 0); | |
60 | if (res == 0) | |
453629a9 MD |
61 | release(ref); |
62 | } | |
63 | ||
cd21955a MD |
64 | /* |
65 | * urcu_ref_get_unless_zero | |
66 | * | |
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 | |
440b950d MD |
71 | * count. False is also returned in case incrementing the refcount would |
72 | * result in an overflow. | |
cd21955a MD |
73 | */ |
74 | static inline bool urcu_ref_get_unless_zero(struct urcu_ref *ref) | |
75 | { | |
76 | long old, _new, res; | |
77 | ||
78 | old = uatomic_read(&ref->refcount); | |
79 | for (;;) { | |
440b950d | 80 | if (old == 0 || old == LONG_MAX) |
cd21955a MD |
81 | return false; /* Failure. */ |
82 | _new = old + 1; | |
83 | res = uatomic_cmpxchg(&ref->refcount, old, _new); | |
84 | if (res == old) { | |
85 | return true; /* Success. */ | |
86 | } | |
87 | old = res; | |
88 | } | |
89 | } | |
90 | ||
453629a9 | 91 | #endif /* _URCU_REF_H */ |