Add `urcu_posix_assert()` as `assert()` replacement
[urcu.git] / include / 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
cd21955a 17#include <stdbool.h>
7ce99d02
MD
18#include <limits.h>
19#include <stdlib.h>
01477510 20#include <urcu/assert.h>
a2e7bf9c 21#include <urcu/uatomic.h>
453629a9
MD
22
23struct urcu_ref {
24 long refcount; /* ATOMIC */
25};
26
27static inline void urcu_ref_set(struct urcu_ref *ref, long val)
28{
29 uatomic_set(&ref->refcount, val);
30}
31
32static inline void urcu_ref_init(struct urcu_ref *ref)
33{
34 urcu_ref_set(ref, 1);
35}
36
8b4b5a1a
MD
37static inline bool __attribute__((warn_unused_result))
38 urcu_ref_get_safe(struct urcu_ref *ref)
453629a9 39{
7ce99d02
MD
40 long old, _new, res;
41
42 old = uatomic_read(&ref->refcount);
43 for (;;) {
44 if (old == LONG_MAX) {
8b4b5a1a 45 return false; /* Failure. */
7ce99d02
MD
46 }
47 _new = old + 1;
48 res = uatomic_cmpxchg(&ref->refcount, old, _new);
49 if (res == old) {
8b4b5a1a 50 return true; /* Success. */
7ce99d02
MD
51 }
52 old = res;
53 }
453629a9
MD
54}
55
8b4b5a1a
MD
56static inline void urcu_ref_get(struct urcu_ref *ref)
57{
58 if (!urcu_ref_get_safe(ref))
59 abort();
60}
61
453629a9
MD
62static inline void urcu_ref_put(struct urcu_ref *ref,
63 void (*release)(struct urcu_ref *))
64{
bf27322d 65 long res = uatomic_sub_return(&ref->refcount, 1);
01477510 66 urcu_posix_assert(res >= 0);
bf27322d 67 if (res == 0)
453629a9
MD
68 release(ref);
69}
70
cd21955a
MD
71/*
72 * urcu_ref_get_unless_zero
73 *
74 * Allows getting a reference atomically if the reference count is not
75 * zero. Returns true if the reference is taken, false otherwise. This
76 * needs to be used in conjunction with another synchronization
77 * technique (e.g. RCU or mutex) to ensure existence of the reference
7ce99d02
MD
78 * count. False is also returned in case incrementing the refcount would
79 * result in an overflow.
cd21955a
MD
80 */
81static inline bool urcu_ref_get_unless_zero(struct urcu_ref *ref)
82{
83 long old, _new, res;
84
85 old = uatomic_read(&ref->refcount);
86 for (;;) {
7ce99d02 87 if (old == 0 || old == LONG_MAX)
cd21955a
MD
88 return false; /* Failure. */
89 _new = old + 1;
90 res = uatomic_cmpxchg(&ref->refcount, old, _new);
91 if (res == old) {
92 return true; /* Success. */
93 }
94 old = res;
95 }
96}
97
453629a9 98#endif /* _URCU_REF_H */
This page took 0.047292 seconds and 4 git commands to generate.