REFCOUNT(9)
π refcount(9) β manage a simple reference counter π
okay bestie, welcome to the refcount(9) page from the Kernel Developer's Manual. This is the whole lowdown on a simple reference counter: no cap, just a bunch of static inline helpers and some very real synchronization tea βπ
.
β¨ NAME β¨
refcount, refcount_init, refcount_acquire, refcount_release β manage a simple reference counter.
(yes, the synopsis down there is longer β we love a little cast expansion π«Ά)
β¨ SYNOPSIS β¨
#include <sys/param.h>
#include <sys/refcount.h>
void
refcount_init(volatile u_int *count, u_int value);
u_int
refcount_load(volatile u_int *count);
u_int
refcount_acquire(volatile u_int *count);
bool
refcount_acquire_checked(volatile u_int *count);
bool
refcount_acquire_if_not_zero(volatile u_int *count);
bool
refcount_release(volatile u_int *count);
bool
refcount_release_if_last(volatile u_int *count);
bool
refcount_release_if_not_last(volatile u_int *count);
π¦ DESCRIPTION π¦
The refcount functions are the API for managing a simple reference counter. You, the caller, bring the storage: it's an unsigned integer, and you pass a pointer to it via count. Usually this counter is managing the lifetime of an object and lives as a member of that object β main character counter energy, honestly π.
Currently all functions are implemented as static inline β tiny, inline, and ready to go.
π the functions, one by one π
refcount_init()β sets the initial value of the counter tovalue. It's normally used when you're creating a reference-counted object. Fresh object, fresh count, slay β¨.refcount_load()β returns a snapshot of the counter value. Heads up: that value may immediately become out-of-date in the absence of external synchronization. Userefcount_load()instead of relying on the properties of thevolatilequalifier. It's giving βtrust the function, not the qualifierβ π.refcount_acquire()β acquires a new reference. It returns the counter value before the new reference was acquired. The caller is responsible for ensuring it holds a valid reference while obtaining a new reference. Example time: if an object is stored on a list and the list holds a reference on the object, then holding a lock that protects the list provides sufficient protection for acquiring a new reference. Locked and loaded, bestie π.refcount_acquire_checked()β same operation asrefcount_acquire(), but it additionally checks that the count value does not overflow as a result of the operation. It returnstrueif the reference was successfully obtained, andfalseif it was not, due to the overflow. Overflow said βnot todayβ π.refcount_acquire_if_not_zero()β yet anotherrefcount_acquire()variant. It only obtains the reference when some reference already exists. In other words,*countmust already be greater than zero for the function to succeed; then the return value istrue, otherwisefalseis returned. Zero count? No reference for you π.refcount_release()β releases an existing reference. It returnstrueif the reference being released was the last reference; otherwise, it returnsfalse. Last one out gets thetrue, periodt π .refcount_release_if_last()andrefcount_release_if_not_last()β variants ofrefcount_release()that only drop the reference when it is or is not the last reference, respectively. In other words,refcount_release_if_last()returnstruewhen*countis equal to one, in which case it is decremented to zero. Otherwise,*countis not modified and the function returnsfalse. Similarly,refcount_release_if_not_last()returnstruewhen*countis greater than one, in which case*countis decremented. Otherwise, if*countis equal to one, the reference is not released and the function returnsfalse. Conditional release, conditional vibes π¦.
β οΈ Big caveat, no cap: these routines do not provide any inter-CPU synchronization or data protection for managing the counter. The caller is responsible for any additional synchronization needed by consumers of any containing objects. The caller is also responsible for managing the life cycle of any containing objects, including explicitly releasing any resources when the last reference is released. You are the main character of cleanup here π«Ά.
And one more very important fence note: refcount_release() unconditionally executes a release fence (see atomic(9)) before releasing the reference, which synchronizes with an acquire fence executed right before returning the true value. This ensures that the destructor β supposedly executed by the caller after the last reference was dropped β sees all updates done during the lifetime of the object. Fence drama, but make it synchronized β‘.
π RETURN VALUES π
The refcount_release function returns true when releasing the last reference and false when releasing any other reference. That's the return-value tea, and it's piping hot β.
πΈ HISTORY πΈ
These functions were introduced in FreeBSD 6.0. Vintage but still iconic π.
okay that's the whole page, bestie π«Ά β mwah π, and this little icon was brought to you by the Debian version dated October 12, 2022 β¨.
π¦ where this page comes from
- freebsd 14.5: base 14.5
- freebsd 15.1: base 15.1
- Copyright (c) 2009 Hudson River Trading LLC
- Copyright (c) 2019 The FreeBSD Foundation
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms