Commit | Line | Data |
---|---|---|
17bb07b4 PMF |
1 | #ifndef _KCOMPAT_KREF_H |
2 | #define _KCOMPAT_KREF_H | |
3 | ||
4 | /* | |
5 | * Kernel sourcecode compatible reference counting implementation | |
6 | * | |
7 | * Copyright (C) 2009 Novell Inc. | |
8 | * | |
9 | * Author: Jan Blunck <jblunck@suse.de> | |
10 | * | |
11 | * This program is free software; you can redistribute it and/or modify it | |
12 | * under the terms of the GNU Lesser General Public License version 2.1 as | |
13 | * published by the Free Software Foundation. | |
14 | */ | |
15 | ||
16 | #include <assert.h> | |
17 | #include <urcu/uatomic_arch.h> | |
18 | ||
19 | struct kref { | |
20 | long refcount; /* ATOMIC */ | |
21 | }; | |
22 | ||
23 | static inline void kref_set(struct kref *ref, int val) | |
24 | { | |
25 | uatomic_set(&ref->refcount, val); | |
26 | } | |
27 | ||
28 | static inline void kref_init(struct kref *ref) | |
29 | { | |
30 | kref_set(ref, 1); | |
31 | } | |
32 | ||
33 | static inline void kref_get(struct kref *ref) | |
34 | { | |
35 | long result = uatomic_add_return(&ref->refcount, 1); | |
36 | assert(result != 0); | |
37 | } | |
38 | ||
39 | static inline void kref_put(struct kref *ref, void (*release)(struct kref *)) | |
40 | { | |
41 | long res = uatomic_sub_return(&ref->refcount, 1); | |
42 | if (res == 0) | |
43 | release(ref); | |
44 | } | |
45 | ||
46 | #endif /* _KCOMPAT_KREF_H */ |