hppa: allocate membarrier system call number
[urcu.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>
cd21955a 18#include <stdbool.h>
a2e7bf9c 19#include <urcu/uatomic.h>
453629a9
MD
20
21struct urcu_ref {
22 long refcount; /* ATOMIC */
23};
24
25static inline void urcu_ref_set(struct urcu_ref *ref, long val)
26{
27 uatomic_set(&ref->refcount, val);
28}
29
30static inline void urcu_ref_init(struct urcu_ref *ref)
31{
32 urcu_ref_set(ref, 1);
33}
34
35static inline void urcu_ref_get(struct urcu_ref *ref)
36{
bf27322d 37 uatomic_add(&ref->refcount, 1);
453629a9
MD
38}
39
40static inline void urcu_ref_put(struct urcu_ref *ref,
41 void (*release)(struct urcu_ref *))
42{
bf27322d
PB
43 long res = uatomic_sub_return(&ref->refcount, 1);
44 assert (res >= 0);
45 if (res == 0)
453629a9
MD
46 release(ref);
47}
48
cd21955a
MD
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 */
58static 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
453629a9 75#endif /* _URCU_REF_H */
This page took 0.034306 seconds and 4 git commands to generate.