xref: /freebsd/share/man/man9/refcount.9 (revision 87fa64c569c54c2ec444ada699acde2ada696d97)
1497435aaSJohn Baldwin.\"
2179fa75eSJohn Baldwin.\" Copyright (c) 2009 Hudson River Trading LLC
3497435aaSJohn Baldwin.\" Written by: John H. Baldwin <jhb@FreeBSD.org>
4497435aaSJohn Baldwin.\" All rights reserved.
5497435aaSJohn Baldwin.\"
643806bc5SKonstantin Belousov.\" Copyright (c) 2019 The FreeBSD Foundation, Inc.
743806bc5SKonstantin Belousov.\"
843806bc5SKonstantin Belousov.\" Parts of this documentation was written by
943806bc5SKonstantin Belousov.\" Konstantin Belousov <kib@FreeBSD.org> under sponsorship
1043806bc5SKonstantin Belousov.\" from the FreeBSD Foundation.
1143806bc5SKonstantin Belousov.\"
12497435aaSJohn Baldwin.\" Redistribution and use in source and binary forms, with or without
13497435aaSJohn Baldwin.\" modification, are permitted provided that the following conditions
14497435aaSJohn Baldwin.\" are met:
15497435aaSJohn Baldwin.\" 1. Redistributions of source code must retain the above copyright
16497435aaSJohn Baldwin.\"    notice, this list of conditions and the following disclaimer.
17497435aaSJohn Baldwin.\" 2. Redistributions in binary form must reproduce the above copyright
18497435aaSJohn Baldwin.\"    notice, this list of conditions and the following disclaimer in the
19497435aaSJohn Baldwin.\"    documentation and/or other materials provided with the distribution.
20497435aaSJohn Baldwin.\"
21497435aaSJohn Baldwin.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22497435aaSJohn Baldwin.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23497435aaSJohn Baldwin.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24497435aaSJohn Baldwin.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25497435aaSJohn Baldwin.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26497435aaSJohn Baldwin.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27497435aaSJohn Baldwin.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28497435aaSJohn Baldwin.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29497435aaSJohn Baldwin.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30497435aaSJohn Baldwin.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31497435aaSJohn Baldwin.\" SUCH DAMAGE.
32497435aaSJohn Baldwin.\"
33497435aaSJohn Baldwin.\" $FreeBSD$
34497435aaSJohn Baldwin.\"
35*87fa64c5SKa Ho Ng.Dd October 12, 2022
36497435aaSJohn Baldwin.Dt REFCOUNT 9
37497435aaSJohn Baldwin.Os
38497435aaSJohn Baldwin.Sh NAME
39497435aaSJohn Baldwin.Nm refcount ,
40497435aaSJohn Baldwin.Nm refcount_init ,
41497435aaSJohn Baldwin.Nm refcount_acquire ,
42497435aaSJohn Baldwin.Nm refcount_release
43497435aaSJohn Baldwin.Nd manage a simple reference counter
44497435aaSJohn Baldwin.Sh SYNOPSIS
45497435aaSJohn Baldwin.In sys/param.h
46497435aaSJohn Baldwin.In sys/refcount.h
47497435aaSJohn Baldwin.Ft void
481e9469d1SChristian Brueffer.Fn refcount_init "volatile u_int *count" "u_int value"
49e8900461SMark Johnston.Ft u_int
50e8900461SMark Johnston.Fn refcount_load "volatile u_int *count"
51*87fa64c5SKa Ho Ng.Ft u_int
52497435aaSJohn Baldwin.Fn refcount_acquire "volatile u_int *count"
5313ff4eb1SKonstantin Belousov.Ft bool
5443806bc5SKonstantin Belousov.Fn refcount_acquire_checked "volatile u_int *count"
5543806bc5SKonstantin Belousov.Ft bool
5643806bc5SKonstantin Belousov.Fn refcount_acquire_if_not_zero "volatile u_int *count"
5743806bc5SKonstantin Belousov.Ft bool
58497435aaSJohn Baldwin.Fn refcount_release "volatile u_int *count"
5943806bc5SKonstantin Belousov.Ft bool
60e8900461SMark Johnston.Fn refcount_release_if_last "volatile u_int *count"
61e8900461SMark Johnston.Ft bool
6243806bc5SKonstantin Belousov.Fn refcount_release_if_not_last "volatile u_int *count"
63497435aaSJohn Baldwin.Sh DESCRIPTION
64497435aaSJohn BaldwinThe
65497435aaSJohn Baldwin.Nm
66497435aaSJohn Baldwinfunctions provide an API to manage a simple reference counter.
67497435aaSJohn BaldwinThe caller provides the storage for the counter in an unsigned integer.
68497435aaSJohn BaldwinA pointer to this integer is passed via
69497435aaSJohn Baldwin.Fa count .
70497435aaSJohn BaldwinUsually the counter is used to manage the lifetime of an object and is
71497435aaSJohn Baldwinstored as a member of the object.
72497435aaSJohn Baldwin.Pp
7343806bc5SKonstantin BelousovCurrently all functions are implemented as static inline.
7443806bc5SKonstantin Belousov.Pp
75497435aaSJohn BaldwinThe
76497435aaSJohn Baldwin.Fn refcount_init
77497435aaSJohn Baldwinfunction is used to set the initial value of the counter to
78497435aaSJohn Baldwin.Fa value .
79497435aaSJohn BaldwinIt is normally used when creating a reference-counted object.
80497435aaSJohn Baldwin.Pp
81497435aaSJohn BaldwinThe
82e8900461SMark Johnston.Fn refcount_load
83e8900461SMark Johnstonfunction returns a snapshot of the counter value.
84e8900461SMark JohnstonThis value may immediately become out-of-date in the absence of external
85e8900461SMark Johnstonsynchronization.
86e8900461SMark Johnston.Fn refcount_load
87e8900461SMark Johnstonshould be used instead of relying on the properties of the
88e8900461SMark Johnston.Vt volatile
89e8900461SMark Johnstonqualifier.
90e8900461SMark Johnston.Pp
91e8900461SMark JohnstonThe
92497435aaSJohn Baldwin.Fn refcount_acquire
93497435aaSJohn Baldwinfunction is used to acquire a new reference.
94*87fa64c5SKa Ho NgIt returns the counter value before the new reference was acquired.
95497435aaSJohn BaldwinThe caller is responsible for ensuring that it holds a valid reference
96497435aaSJohn Baldwinwhile obtaining a new reference.
97497435aaSJohn BaldwinFor example,
98497435aaSJohn Baldwinif an object is stored on a list and the list holds a reference on the
99497435aaSJohn Baldwinobject, then holding a lock that protects the list provides sufficient
100497435aaSJohn Baldwinprotection for acquiring a new reference.
101497435aaSJohn Baldwin.Pp
102497435aaSJohn BaldwinThe
10343806bc5SKonstantin Belousov.Fn refcount_acquire_checked
10443806bc5SKonstantin Belousovvariant performs the same operation as
10543806bc5SKonstantin Belousov.Fn refcount_acquire ,
10643806bc5SKonstantin Belousovbut additionally checks that the
10743806bc5SKonstantin Belousov.Fa count
10843806bc5SKonstantin Belousovvalue does not overflow as result of the operation.
10943806bc5SKonstantin BelousovIt returns
11043806bc5SKonstantin Belousov.Dv true
11143806bc5SKonstantin Belousovif the reference was sucessfully obtained, and
11243806bc5SKonstantin Belousov.Dv false
11343806bc5SKonstantin Belousovif it was not, due to the overflow.
11443806bc5SKonstantin Belousov.Pp
11543806bc5SKonstantin BelousovThe
11643806bc5SKonstantin Belousov.Fn refcount_acquire_if_not_zero
11743806bc5SKonstantin Belousovfunction is yet another variant of
11843806bc5SKonstantin Belousov.Fn refcount_acquire ,
11943806bc5SKonstantin Belousovwhich only obtains the reference when some reference already exists.
12043806bc5SKonstantin BelousovIn other words,
12143806bc5SKonstantin Belousov.Fa *count
12243806bc5SKonstantin Belousovmust be already greater than zero for the function to succeed, in which
12343806bc5SKonstantin Belousovcase the return value is
12443806bc5SKonstantin Belousov.Dv true ,
12543806bc5SKonstantin Belousovotherwise
12643806bc5SKonstantin Belousov.Dv false
12743806bc5SKonstantin Belousovis returned.
12843806bc5SKonstantin Belousov.Pp
12943806bc5SKonstantin BelousovThe
130497435aaSJohn Baldwin.Fn refcount_release
131497435aaSJohn Baldwinfunction is used to release an existing reference.
13213ff4eb1SKonstantin BelousovThe function returns true if the reference being released was
133497435aaSJohn Baldwinthe last reference;
13413ff4eb1SKonstantin Belousovotherwise, it returns false.
135497435aaSJohn Baldwin.Pp
13643806bc5SKonstantin BelousovThe
137e8900461SMark Johnston.Fn refcount_release_if_last
138e8900461SMark Johnstonand
13943806bc5SKonstantin Belousov.Fn refcount_release_if_not_last
140e8900461SMark Johnstonfunctions are variants of
14143806bc5SKonstantin Belousov.Fn refcount_release
142e8900461SMark Johnstonwhich only drop the reference when it is or is not the last reference,
143e8900461SMark Johnstonrespectively.
144e8900461SMark JohnstonIn other words,
145e8900461SMark Johnston.Fn refcount_release_if_last
146e8900461SMark Johnstonreturns
147e8900461SMark Johnston.Dv true
148e8900461SMark Johnstonwhen
149e8900461SMark Johnston.Fa *count
150e8900461SMark Johnstonis equal to one, in which case it is decremented to zero.
151e8900461SMark JohnstonOtherwise,
152e8900461SMark Johnston.Fa *count
153e8900461SMark Johnstonis not modified and the function returns
154e8900461SMark Johnston.Dv false .
155e8900461SMark JohnstonSimilarly,
156e8900461SMark Johnston.Fn refcount_release_if_not_last
157e8900461SMark Johnstonreturns
15843806bc5SKonstantin Belousov.Dv true
15943806bc5SKonstantin Belousovwhen
16043806bc5SKonstantin Belousov.Fa *count
16143806bc5SKonstantin Belousovis greater than one, in which case
162e8900461SMark Johnston.Fa *count
163e8900461SMark Johnstonis decremented.
16443806bc5SKonstantin BelousovOtherwise, if
16543806bc5SKonstantin Belousov.Fa *count
16643806bc5SKonstantin Belousovis equal to one, the reference is not released and the function returns
16743806bc5SKonstantin Belousov.Dv false .
16843806bc5SKonstantin Belousov.Pp
16943806bc5SKonstantin BelousovNote that these routines do not provide any inter-CPU synchronization or
17043806bc5SKonstantin Belousovdata protection for managing the counter.
171497435aaSJohn BaldwinThe caller is responsible for any additional synchronization needed by
172497435aaSJohn Baldwinconsumers of any containing objects.
173497435aaSJohn BaldwinIn addition,
174497435aaSJohn Baldwinthe caller is also responsible for managing the life cycle of any containing
175497435aaSJohn Baldwinobjects including explicitly releasing any resources when the last reference
176497435aaSJohn Baldwinis released.
17743806bc5SKonstantin Belousov.Pp
17843806bc5SKonstantin BelousovThe
17943806bc5SKonstantin Belousov.Fn refcount_release
18043806bc5SKonstantin Belousovunconditionally executes a release fence (see
18143806bc5SKonstantin Belousov.Xr atomic 9 ) before releasing the reference, which
18243806bc5SKonstantin Belousovsynchronizes with an acquire fence executed right before
18343806bc5SKonstantin Belousovreturning the
18443806bc5SKonstantin Belousov.Dv true
18543806bc5SKonstantin Belousovvalue.
18643806bc5SKonstantin BelousovThis ensures that the destructor, supposedly executed by the caller after
18743806bc5SKonstantin Belousovthe last reference was dropped, sees all updates done during the lifetime
18843806bc5SKonstantin Belousovof the object.
189497435aaSJohn Baldwin.Sh RETURN VALUES
190497435aaSJohn BaldwinThe
191497435aaSJohn Baldwin.Nm refcount_release
19213ff4eb1SKonstantin Belousovfunction returns true when releasing the last reference and false when
193497435aaSJohn Baldwinreleasing any other reference.
194497435aaSJohn Baldwin.Sh HISTORY
195497435aaSJohn BaldwinThese functions were introduced in
196497435aaSJohn Baldwin.Fx 6.0 .
197