xref: /freebsd/share/man/man9/refcount.9 (revision bc96366c864c07ef352edb92017357917c75b36c)
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
48The
49.Nm
50functions provide an API to manage a simple reference counter.
51The caller provides the storage for the counter in an unsigned integer.
52A pointer to this integer is passed via
53.Fa count .
54Usually the counter is used to manage the lifetime of an object and is
55stored as a member of the object.
56.Pp
57The
58.Fn refcount_init
59function is used to set the initial value of the counter to
60.Fa value .
61It is normally used when creating a reference-counted object.
62.Pp
63The
64.Fn refcount_acquire
65function is used to acquire a new reference.
66The caller is responsible for ensuring that it holds a valid reference
67while obtaining a new reference.
68For example,
69if an object is stored on a list and the list holds a reference on the
70object, then holding a lock that protects the list provides sufficient
71protection for acquiring a new reference.
72.Pp
73The
74.Fn refcount_release
75function is used to release an existing reference.
76The function returns a non-zero value if the reference being released was
77the last reference;
78otherwise, it returns zero.
79.Pp
80Note that these routines do not provide any inter-CPU synchronization,
81data protection,
82or memory ordering guarantees except for managing the counter.
83The caller is responsible for any additional synchronization needed by
84consumers of any containing objects.
85In addition,
86the caller is also responsible for managing the life cycle of any containing
87objects including explicitly releasing any resources when the last reference
88is released.
89.Sh RETURN VALUES
90The
91.Nm refcount_release
92function returns non-zero when releasing the last reference and zero when
93releasing any other reference.
94.Sh HISTORY
95These functions were introduced in
96.Fx 6.0 .
97