REFCNT_INIT(9)
π REFCNT_INIT(9) π
okay bestie, welcome to the REFCNT_INIT(9) page from the Kernel Developer's Manual β¨ this one is literally all about the reference count API and it's giving βkeep my shared object aliveβ energy π«Ά
β¨ NAME β¨
refcnt_init, refcnt_init_trace, refcnt_take, refcnt_rele, refcnt_rele_wake, refcnt_finalize, refcnt_shared, refcnt_read, REFCNT_INITIALIZER β reference count API π
π SYNOPSIS π
#include <sys/refcnt.h>
void
refcnt_init(struct refcnt *r);
void
refcnt_init_trace(struct refcnt *r, int trace);
void
refcnt_take(struct refcnt *r);
int
refcnt_rele(struct refcnt *r);
void
refcnt_rele_wake(struct refcnt *r);
void
refcnt_finalize(struct refcnt *r, const char *wmesg);
int
refcnt_shared(const struct refcnt *r);
unsigned int
refcnt_read(const struct refcnt *r);
REFCNT_INITIALIZER();
πΈ DESCRIPTION πΈ
The refcnt API provides simple reference counters that can be used to manage the lifetime of a shared object. no cap, that's the whole vibe: count your refs so your object doesn't get deleted while somebody still needs it π
refcnt_init()β sets the initial value of the counter to1to account for the caller's reference to the object. starting fresh, main character energy πrefcnt_init_trace()β does the same thing, but additionally accepts adt(4)static probe index. fancy β¨refcnt_take()β used to acquire a new reference. bestie, it is the responsibility of the caller to guarantee that it holds a valid reference before taking a new reference. don't skip that part, fr fr π«Άrefcnt_rele()β used to release an existing reference. bye, reference πrefcnt_rele_wake()β used to release an existing reference and wakes up a process that is currently waiting inrefcnt_finalize()for all the references to be released. wakey wakey πrefcnt_finalize()β releases the caller's reference and sleeps until all the other references to the relevant object have been released. there may only be one caller torefcnt_finalize()per refcntr. so likeβ¦ one finalize caller only, okay? πrefcnt_shared()β tests if the object has multiple references. is it shared? vibe check π₯refcnt_read()β returns a snapshot of the counter value. its use is discouraged, and code should userefcnt_shared()whenever possible. lowkey don't reach for this one unless you gotta πREFCNT_INITIALIZER()β initialises a declaration of a refcnt to1. cute little declaration starter π
okay and the memory-ordering part is important, so listen up: refcnt_rele(), refcnt_rele_wake() and refcnt_finalize() order prior memory loads and stores before the release of the reference. The functions enforce control dependency so that after the final reference has been released, subsequent loads and stores happen after the release. These ensure that concurrent accesses cease before the object's destructor runs and that the destructor sees all updates done during the lifetime of the object. literally the whole βdon't touch it after it's goneβ situation π
π CONTEXT π
refcnt_init(), refcnt_init_trace(), refcnt_take(), refcnt_rele(), refcnt_rele_wake(), refcnt_shared() and refcnt_read() can be called during autoconf, from process context, or from interrupt context. versatile icons β¨
refcnt_finalize() can be called from process context. that's the one with the sleepy-wait energy π΄
π RETURN VALUES π
refcnt_rele()β returns a non-zero value if the last reference has been released, otherwise0. last one out? non-zero, bestie πrefcnt_shared()β returns a non-zero value if the object has multiple references, otherwise0. multiple refs = non-zero, no cap πrefcnt_read()β returns a snapshot of the counter value. just a lil snapshot πΈ
mwah π this cutie came from Debian on August 5, 2025, and it's literally REFCNT_INIT(9) β periodt π¦
π¦ where this page comes from
- nixpkgs unstable: cvs-export
- Copyright (c) 2015 David Gwynne <dlg@openbsd.org>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms