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> | |
a2e7bf9c | 18 | #include <urcu/uatomic.h> |
453629a9 MD |
19 | |
20 | struct urcu_ref { | |
21 | long refcount; /* ATOMIC */ | |
22 | }; | |
23 | ||
24 | static inline void urcu_ref_set(struct urcu_ref *ref, long val) | |
25 | { | |
26 | uatomic_set(&ref->refcount, val); | |
27 | } | |
28 | ||
29 | static inline void urcu_ref_init(struct urcu_ref *ref) | |
30 | { | |
31 | urcu_ref_set(ref, 1); | |
32 | } | |
33 | ||
34 | static inline void urcu_ref_get(struct urcu_ref *ref) | |
35 | { | |
bf27322d | 36 | uatomic_add(&ref->refcount, 1); |
453629a9 MD |
37 | } |
38 | ||
39 | static inline void urcu_ref_put(struct urcu_ref *ref, | |
40 | void (*release)(struct urcu_ref *)) | |
41 | { | |
bf27322d PB |
42 | long res = uatomic_sub_return(&ref->refcount, 1); |
43 | assert (res >= 0); | |
44 | if (res == 0) | |
453629a9 MD |
45 | release(ref); |
46 | } | |
47 | ||
48 | #endif /* _URCU_REF_H */ |