5 * Userspace RCU - Reference counting
7 * Copyright (C) 2009 Novell Inc.
8 * Copyright (C) 2010 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 * Author: Jan Blunck <jblunck@suse.de>
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.
19 #include <urcu/uatomic.h>
22 long refcount
; /* ATOMIC */
25 static inline void urcu_ref_set(struct urcu_ref
*ref
, long val
)
27 uatomic_set(&ref
->refcount
, val
);
30 static inline void urcu_ref_init(struct urcu_ref
*ref
)
35 static inline void urcu_ref_get(struct urcu_ref
*ref
)
37 uatomic_add(&ref
->refcount
, 1);
40 static inline void urcu_ref_put(struct urcu_ref
*ref
,
41 void (*release
)(struct urcu_ref
*))
43 long res
= uatomic_sub_return(&ref
->refcount
, 1);
50 * urcu_ref_get_unless_zero
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
58 static inline bool urcu_ref_get_unless_zero(struct urcu_ref
*ref
)
62 old
= uatomic_read(&ref
->refcount
);
65 return false; /* Failure. */
67 res
= uatomic_cmpxchg(&ref
->refcount
, old
, _new
);
69 return true; /* Success. */
75 #endif /* _URCU_REF_H */
This page took 0.030324 seconds and 4 git commands to generate.