2 .\" Copyright (c) 2009 Hudson River Trading LLC
3 .\" Written by: John H. Baldwin <jhb@FreeBSD.org>
4 .\" All rights reserved.
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
9 .\" 1. Redistributions of source code must retain the above copyright
10 .\" notice, this list of conditions and the following disclaimer.
11 .\" 2. Redistributions in binary form must reproduce the above copyright
12 .\" notice, this list of conditions and the following disclaimer in the
13 .\" documentation and/or other materials provided with the distribution.
15 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 .Nm refcount_acquire ,
37 .Nd manage a simple reference counter
42 .Fn refcount_init "volatile u_int *count, u_int value"
44 .Fn refcount_acquire "volatile u_int *count"
46 .Fn refcount_release "volatile u_int *count"
50 functions provide an API to manage a simple reference counter.
51 The caller provides the storage for the counter in an unsigned integer.
52 A pointer to this integer is passed via
54 Usually the counter is used to manage the lifetime of an object and is
55 stored as a member of the object.
59 function is used to set the initial value of the counter to
61 It is normally used when creating a reference-counted object.
65 function is used to acquire a new reference.
66 The caller is responsible for ensuring that it holds a valid reference
67 while obtaining a new reference.
69 if an object is stored on a list and the list holds a reference on the
70 object, then holding a lock that protects the list provides sufficient
71 protection for acquiring a new reference.
75 function is used to release an existing reference.
76 The function returns a non-zero value if the reference being released was
78 otherwise, it returns zero.
80 Note that these routines do not provide any inter-CPU synchronization,
82 or memory ordering guarantees except for managing the counter.
83 The caller is responsible for any additional synchronization needed by
84 consumers of any containing objects.
86 the caller is also responsible for managing the life cycle of any containing
87 objects including explicitly releasing any resources when the last reference
92 function returns non-zero when releasing the last reference and zero when
93 releasing any other reference.
95 These functions were introduced in