Version 0.8.11
[userspace-rcu.git] / urcu / ref.h
CommitLineData
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>
3a03e6f3
MD
18#include <limits.h>
19#include <stdlib.h>
a2e7bf9c 20#include <urcu/uatomic.h>
453629a9
MD
21
22struct urcu_ref {
23 long refcount; /* ATOMIC */
24};
25
26static inline void urcu_ref_set(struct urcu_ref *ref, long val)
27{
28 uatomic_set(&ref->refcount, val);
29}
30
31static inline void urcu_ref_init(struct urcu_ref *ref)
32{
33 urcu_ref_set(ref, 1);
34}
35
36static inline void urcu_ref_get(struct urcu_ref *ref)
37{
3a03e6f3
MD
38 long old, _new, res;
39
40 old = uatomic_read(&ref->refcount);
41 for (;;) {
42 if (old == LONG_MAX) {
43 abort();
44 }
45 _new = old + 1;
46 res = uatomic_cmpxchg(&ref->refcount, old, _new);
47 if (res == old) {
48 return;
49 }
50 old = res;
51 }
453629a9
MD
52}
53
54static inline void urcu_ref_put(struct urcu_ref *ref,
55 void (*release)(struct urcu_ref *))
56{
bf27322d
PB
57 long res = uatomic_sub_return(&ref->refcount, 1);
58 assert (res >= 0);
59 if (res == 0)
453629a9
MD
60 release(ref);
61}
62
63#endif /* _URCU_REF_H */
This page took 0.03274 seconds and 4 git commands to generate.