74be50d6bbe4e46d28603ba4cb8de3db2ed7794a
[urcu.git] / urcu / ref.h
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 <stdbool.h>
19 #include <urcu/uatomic.h>
20
21 struct urcu_ref {
22 long refcount; /* ATOMIC */
23 };
24
25 static inline void urcu_ref_set(struct urcu_ref *ref, long val)
26 {
27 uatomic_set(&ref->refcount, val);
28 }
29
30 static inline void urcu_ref_init(struct urcu_ref *ref)
31 {
32 urcu_ref_set(ref, 1);
33 }
34
35 static inline void urcu_ref_get(struct urcu_ref *ref)
36 {
37 uatomic_add(&ref->refcount, 1);
38 }
39
40 static inline void urcu_ref_put(struct urcu_ref *ref,
41 void (*release)(struct urcu_ref *))
42 {
43 long res = uatomic_sub_return(&ref->refcount, 1);
44 assert (res >= 0);
45 if (res == 0)
46 release(ref);
47 }
48
49 /*
50 * urcu_ref_get_unless_zero
51 *
52 * Allows getting a reference atomically if the reference count is not
53 * zero. Returns true if the reference is taken, false otherwise. This
54 * needs to be used in conjunction with another synchronization
55 * technique (e.g. RCU or mutex) to ensure existence of the reference
56 * count.
57 */
58 static inline bool urcu_ref_get_unless_zero(struct urcu_ref *ref)
59 {
60 long old, _new, res;
61
62 old = uatomic_read(&ref->refcount);
63 for (;;) {
64 if (old == 0)
65 return false; /* Failure. */
66 _new = old + 1;
67 res = uatomic_cmpxchg(&ref->refcount, old, _new);
68 if (res == old) {
69 return true; /* Success. */
70 }
71 old = res;
72 }
73 }
74
75 #endif /* _URCU_REF_H */
This page took 0.029663 seconds and 3 git commands to generate.