p As a rough heuristic, .Nm should be used for classes of objects of which there are perhaps a few dozen instances (such as .Xr autoconf 9 devices) but not thousands of instances (such as network flows) and on which there may be a mixture of long-term I/O waits, such as xyzread for a device xyz(4), and short-term fast operations, such as .Dv xyzioctl(IOC_READ_A_CPU_REG) . .Sh FUNCTIONS l -tag -width abcd t Fn localcount_init "lc" Dynamically initialize localcount .Ar lc for use.
p No other operations can be performed on a localcount until it has been initialized. t Fn localcount_fini "lc" Release resources used by localcount .Ar lc . The caller must have already called .Fn localcount_drain . The localcount may not be used after .Fn localcount_fini has been called until it has been re-initialized by .Fn localcount_init . t Fn localcount_acquire "lc" Acquire a reference to the localcount .Ar lc .
p The caller must ensure by some other mechanism that the localcount will not be destroyed before the call to .Fn localcount_acquire ; typically this will be via a .Xr pserialize 9 read section. t Fn localcount_release "lc" "cv" "mtx" Release a reference to the localcount .Ar lc . If the localcount is currently being drained, the mutex .Ar mtx will be used to synchronize updates to the global reference count (i.e., the total across all CPUs). If the reference count goes to zero, .Fn localcount_release will broadcast availability of the condvar .Ar cv . t Fn localcount_drain "lc" "cv" "mtx" Wait for all references to the localcount .Ar lc to be released. The caller must hold the mutex .Ar mtx ; the mutex will be released during inter-CPU cross-calls (see .Xr xcall 9 ) and while waiting on the condvar .Ar cv . The same .Ar cv and .Ar mtx must be used with .Fn localcount_release .
p The caller must guarantee that no new references can be acquired with .Fn localcount_acquire before calling .Fn localcount_drain . For example, any object that may be found in a list and acquired must be removed from the list before calling .Fn localcount_drain ; removal from the list would typically be protected by calling .Xr pserialize_perform 9 to wait for any .Xr pserialize 9 readers to complete.
p Once the localcount object .Ar lc is passed to .Fn localcount_drain , it must be passed to .Fn localcount_fini before any other re-use. .El .Sh CODE REFERENCES The core of the localcount implementation is located in
a sys/kern/subr_localcount.c .
p The header file
a sys/sys/localcount.h describes the public interface, and interfaces that machine-dependent code must provide to support localcounts. .Sh SEE ALSO .Xr atomic_ops 3 , .Xr condvar 9 , .Xr mutex 9 , .Xr psref 9 .Sh HISTORY The localcount primitives first appeared in .Nx 8.0 . .Sh AUTHORS .Nm was designed by .An -nosplit .An Taylor R. Campbell , who also provided a draft implementation. The implementation was completed, tested, and integrated by .An Paul Goyette . .Sh CAVEATS The .Nm facility does not provide any way to examine the reference count without actually waiting for the count to reach zero.
p Waiting for a .Nm reference count to drain (reach zero) is a one-shot operation. Once the .Nm has been drained, no further operations are allowed until the .Nm has been re-initialized.