ATOMIC_INC_INT(9)
π ATOMIC_INC_INT(9) β bestie's guide to atomic increments
okay so this is from the Kernel Developer's Manual, section 9, and it's literally the glow-up of "just add one" β but, like, safely. no cap.
β¨ NAME β¨
atomic_inc_int, atomic_inc_int_nv, atomic_inc_long, atomic_inc_long_nv β the atomic increment operations. yes queen, all four of them, and they're a package deal.
π SYNOPSIS π
first things first, you gotta pull in the header:
#include <sys/atomic.h>
then the whole squad, in order, exactly like this:
void
atomic_inc_int(volatile unsigned int *p);
unsigned int
atomic_inc_int_nv(volatile unsigned int *p);
void
atomic_inc_long(volatile unsigned long *p);
unsigned long
atomic_inc_long_nv(volatile unsigned long *p);
notice the _nv girlies return a value and the plain ones don't β that's the whole plot twist, more on that below π«Ά
π¦ DESCRIPTION π¦
so the atomic_inc fam gives you an interface for atomically doing increment operations and increment-and-fetch operations β atomically with respect to interrupts and multiple processors in the system. that last part is the whole point, iykyk. no sneaky race conditions, no two CPUs stepping on each other, it just works. slay.
the value referenced by the pointer p is incremented by 1. that's it. that's the job. one (1) singular unit of increase. iconic behaviour tbh β¨
πΈ CONTEXT πΈ
the good news bestie: atomic_inc_int(), atomic_inc_int_nv(), atomic_inc_long(), and atomic_inc_long_nv() can all be called during autoconf, from process context, or from interrupt context. all of them, every single one, no exceptions. flexible queens π
π RETURN VALUES π
atomic_inc_intandatomic_inc_longdo the increment and return literally nothing β no knowledge of the value atpcomes back to you. mysterious. withholding. main character energy πatomic_inc_int_nvandatomic_inc_long_nvreturn the value atpafter the increment was performed. so you get the receipts π§Ύ
π SEE ALSO π
go read these next, fr fr:
π HISTORY π
the atomic_inc functions first appeared in NetBSD 5.0 and OpenBSD 5.5. vintage but still relevant, we love a legacy icon β
ngl, it's four tiny functions that add one to a number without breaking under concurrency. that's the whole vibe check. if you're touching shared values from interrupts or multiple processors, use these and not p++ like some kind of chaotic goblin π
and that's the page, bestie! π this lil doc came from Debian, dated February 13, 2014 β so it's giving throwback, but the atomic increment stays eternally fresh β¨ππ¦
π¦ where this page comes from
- nixpkgs unstable: cvs-export
- Copyright (c) 2014 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