ATOMIC_CAS_UINT(9)

πŸ“¦ in nixpkgs unstable

πŸ’… ATOMIC_CAS_UINT(9) πŸ’…

okay so bestie, welcome to the Kernel Developer's Manual ✨ β€” yes that's the manual section we're in, 9, kernel-land, the cool section, no cap πŸŽ€


🌸 NAME 🌸

atomic_cas_uint, atomic_cas_ulong, atomic_cas_ptr β€” atomic compare-and-swap operations πŸ¦‹

like literally the three of them, one little squad, all doing the swap-and-compare thing πŸ’–


✨ SYNOPSIS ✨

#include <sys/atomic.h>

unsigned int
atomic_cas_uint(volatile unsigned int *p, unsigned int expected,
    unsigned int new);

unsigned long
atomic_cas_ulong(volatile unsigned long *p, unsigned long expected,
    unsigned long new);

void *
atomic_cas_ptr(volatile void *p, void *expected, void *new);

that's it, that's the whole vibe check πŸ‘‘ include the header, call the function, do the atomic thing πŸ’«


🫢 DESCRIPTION 🫢

okay so the atomic_cas set of functions gives you an interface for atomically performing compare-and-swap operations β€” and by atomically we mean with respect to interrupts and multiple processors in the system, which is the whole point, this is the main character energy right here πŸ”₯

so here's the tea β˜•: the value referenced by the pointer p gets compared against expected. if those two values are equal? then new replaces the value stored at p. that's the swap. that's the whole bit. slay πŸ’…

if they're not equal, nothing gets swapped (that's the compare part doing its job, iykyk) 🧚


🌈 CONTEXT 🌈

atomic_cas_uint(), atomic_cas_ulong(), and atomic_cas_ptr() can all be called during autoconf, from process context, or from interrupt context πŸ’œ

like all three of them, all three contexts, no restrictions, go off bestie ⭐


πŸŽ€ RETURN VALUES πŸŽ€

these functions return the value at p as it was before the attempt to swap it πŸ™ˆ

so you always get to see what was there first β€” even if the swap didn't happen, you get the original value back, which is genuinely so useful fr fr πŸ’€


πŸ¦‹ SEE ALSO πŸ¦‹

go read that one too, it's the cousin πŸ“


πŸ’– HISTORY πŸ’–

the atomic_cas functions first appeared in NetBSD 5.0 and OpenBSD 5.5 β€” iconic from day one, honestly 🌸


and that's the page bestie!! this one's from Debian, dated February 6, 2019, and the page title is ATOMIC_CAS_UINT(9) β€” now go forth and swap atomically, 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