ATOMIC_SUB_INT(9)

πŸ“¦ in nixpkgs unstable

πŸ’… ATOMIC_SUB_INT(9) β€” the kernel developer's manual, bestie edition πŸŽ€

ok so picture this: it's a page from the Kernel Developer's Manual, section 9, and it's all about atomic subtraction. yes bestie, subtraction that no other CPU or interrupt can mess with mid-flight. icon behavior.


✨ NAME ✨

atomic_sub_int, atomic_sub_int_nv, atomic_sub_long, atomic_sub_long_nv β€” the atomic subtraction operations, aka "minus but make it safe" πŸ’–


🌸 SYNOPSIS 🌸

first you gotta bring in the header, obviously:

#include <sys/atomic.h>

then the four besties themselves, in all their glory:

void
atomic_sub_int(volatile unsigned int *p, unsigned int v);

unsigned int
atomic_sub_int_nv(volatile unsigned int *p, unsigned int v);

void
atomic_sub_long(volatile unsigned long *p, unsigned long v);

unsigned long
atomic_sub_long_nv(volatile unsigned long *p, unsigned long v);

look at them. matching outfits. slay. πŸ‘‘


πŸ¦‹ DESCRIPTION πŸ¦‹

the atomic_sub set of functions is literally an interface for atomically performing sub and sub-and-fetch operations β€” and the whole point is that they're atomic with respect to interrupts and multiple processors in the system. so like, no race conditions, no chaos, no "wait which CPU got there first" drama. 🫢

here's the actual deal: the value referenced by the pointer p gets decremented by the value v. that's it. that's the whole vibe.

ngl the volatile on p is doing a lot of emotional labor here, so be nice to it. ✨


⭐ CONTEXT ⭐

good news bestie, all four of them β€” atomic_sub_int(), atomic_sub_int_nv(), atomic_sub_long(), and atomic_sub_long_nv() β€” can be called during autoconf, from process context, or from interrupt context. all three. no caveats. she's flexible. main character energy fr fr πŸ”₯


πŸŽ€ RETURN VALUES πŸŽ€

ok this is the part where the _nv girlies separate from the regular ones:

  • atomic_sub_int and atomic_sub_long β€” they do the subtraction and then dip. no knowledge of the value at p is returned. zero notes. void. they said "not my job" πŸ’€
  • atomic_sub_int_nv and atomic_sub_long_nv β€” these two come back and tell you what's at p after the subtraction was performed. so you get the receipt. the "nv" stands for new value, iykyk πŸ’…

🌈 SEE ALSO 🌈

go read these next, they're in the same friend group:


🧚 HISTORY 🧚

the atomic_sub functions first appeared in OpenBSD 5.5. vintage. she's been iconic since day one, no cap. πŸ“


anyway bestie, that's the whole page β€” sending you off with love from the Debian edition of the Kernel Developer's Manual, last touched July 23, 2014. mwah πŸ’–βœ¨πŸŽ€πŸ¦‹

πŸ“¦ where this page comes from
  • nixpkgs unstable: cvs-export
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms