ATOMIC_CAS_UINT(9)
π 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
- 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