refcount_dec_if_one(9)
π refcount_dec_if_one
okay bestie, welcome to the kernel API manual β yes it's that Kernel API Manual vibe, and yes this page is a little gem. It's called refcount_dec_if_one and it's literally the "decrement a refcount if it is 1" girlie. No cap. π
β¨ SYNOPSIS β¨
bool refcount_dec_if_one (refcount_t *r );
that's the whole signature, she's compact, she's iconic, she takes exactly one argument and she gives you back a bool. periodt. π¦
πΈ ARGUMENTS πΈ
rβ the refcount π (yes, that's it, that's the whole argument, she's the pointer to therefcount_tyou're messing with)
π§ DESCRIPTION π§
okay so here's the tea: there is no atomic_t counterpart for this one. Not a thing. It attempts a 1 -> 0 transition and returns whether that worked or not. that's the whole assignment. π₯
Like every other decrement operation in the family, it provides release memory order and provides a control dependency. So likeβ¦ she's not just cute, she's got proper ordering semantics. main character energy tbh. π
You can use it like a try-delete operator β that's literally its whole purpose. And listen, this explicit case is provided (and not just done with cmpxchg in generic) on purpose, because using cmpxchg in generic would allow implementing unsafe operations. So the restriction is a feature, not a bug, iykyk. π
β οΈ fr fr, do not try to sneak a generic cmpxchg version in here β the whole point of exposing this exact helper is to prevent unsafe stuff from being built on top of it.
β RETURN β
trueif the resulting refcount is0πfalseotherwise π
that's it. binary. no in-between. slay.
π¦ SEE ALSO π¦
- Kernel file
./lib/refcount.c refcount_dec_not_one(9)refcount_dec_and_mutex_lock(9)refcount_dec_and_lock(9)refcount_dec_and_lock_irqsave(9)
go read those too, bestie, they're the rest of the friend group. π
mwah π β this page lives in ./lib, it's dated October 2026, and it goes by refcount_dec_if_one(9). stay safe out there and always decrement responsibly π«Άβ¨
π¦ where this page comes from
- nixpkgs unstable: linux-manual 7.3.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms