]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - share/man/man9/refcount.9
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / share / man / man9 / refcount.9
1 .\"
2 .\" Copyright (c) 2009 Advanced Computing Technologies LLC
3 .\" Written by: John H. Baldwin <jhb@FreeBSD.org>
4 .\" All rights reserved.
5 .\"
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
8 .\" are met:
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.
14 .\"
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
25 .\" SUCH DAMAGE.
26 .\"
27 .\" $FreeBSD$
28 .\"
29 .Dd January 20, 2009
30 .Dt REFCOUNT 9
31 .Os
32 .Sh NAME
33 .Nm refcount ,
34 .Nm refcount_init ,
35 .Nm refcount_acquire ,
36 .Nm refcount_release
37 .Nd manage a simple reference counter
38 .Sh SYNOPSIS
39 .In sys/param.h
40 .In sys/refcount.h
41 .Ft void
42 .Fn refcount_init "volatile u_int *count, u_int value"
43 .Ft void
44 .Fn refcount_acquire "volatile u_int *count"
45 .Ft int
46 .Fn refcount_release "volatile u_int *count"
47 .Sh DESCRIPTION
48 The
49 .Nm
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
53 .Fa count .
54 Usually the counter is used to manage the lifetime of an object and is
55 stored as a member of the object.
56 .Pp
57 The
58 .Fn refcount_init
59 function is used to set the initial value of the counter to
60 .Fa value .
61 It is normally used when creating a reference-counted object.
62 .Pp
63 The
64 .Fn refcount_acquire
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.
68 For example,
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.
72 .Pp
73 The
74 .Fn refcount_release
75 function is used to release an existing reference.
76 The function returns a non-zero value if the reference being released was
77 the last reference;
78 otherwise, it returns zero.
79 .Pp
80 Note that these routines do not provide any inter-CPU synchronization,
81 data protection,
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.
85 In addition,
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
88 is released.
89 .Sh RETURN VALUES
90 The
91 .Nm refcount_release
92 function returns non-zero when releasing the last reference and zero when
93 releasing any other reference.
94 .Sh HISTORY
95 These functions were introduced in
96 .Fx 6.0 .