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