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> | |
18 | #include <urcu/uatomic_arch.h> | |
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 | { | |
36 | long res = uatomic_add_return(&ref->refcount, 1); | |
37 | assert(res != 0); | |
38 | } | |
39 | ||
40 | static inline void urcu_ref_put(struct urcu_ref *ref, | |
41 | void (*release)(struct urcu_ref *)) | |
42 | { | |
43 | if (!uatomic_sub_return(&ref->refcount, 1)) | |
44 | release(ref); | |
45 | } | |
46 | ||
47 | #endif /* _URCU_REF_H */ |