atomic_xchg(9)

πŸ“¦ version 7.3.1, in nixpkgs unstable

πŸ’… atomic_xchg(9) β€” Kernel API Manual, but make it make sense

okay so real talk, bestie β€” you found yourself in the atomic_xchg(9) page of the Kernel API Manual, and it's genuinely one of the shortest baddies in the whole atomic family, so let's go ✨


🌸 NAME 🌸

atomic_xchg β€” atomic exchange with full ordering πŸŽ€

like, it swaps a value in and gives you back the old one, and it does it atomically, no cap πŸ‘‘


πŸ¦‹ SYNOPSIS πŸ¦‹

int atomic_xchg (atomic_t *v , int new );

πŸ’œ ARGUMENTS πŸ’œ

  • v β€” pointer to atomic_t. this is your victim, the thing getting swapped 🫢
  • new β€” int value to assign. this is the glow-up going in ✨

πŸ”₯ DESCRIPTION πŸ”₯

Atomically updates v to new with full ordering.

that's it. that's the whole thing. it's giving "short and powerful" πŸ’… the swap happens as one indivisible unit, and it's got full ordering, so the memory barriers are already handled for you, you don't gotta do a thing 🧚

🚨 unsafe alert 🚨 β€” this one is unsafe to use in noinstr code; use raw_atomic_xchg there instead. like, do not try it, i'm begging 😭

πŸ‘‘ RETURN πŸ‘‘

The original value of v. 🌈

so you hand over the new value, and the kernel hands back what was there before β€” iconic exchange behaviour, mutual, respectful, consensual πŸŽ€


⭐ SEE ALSO ⭐

bestie there are so many of these, grab a snack πŸ“ β€” here's the whole atomic cinematic universe, all (9) sections, all present and accounted for:

atomic_* β€” the classics πŸ¦‹

atomic64_* β€” the 64-bit edition πŸ’€

atomic_long_* β€” long boi era 🌈

πŸ“ kernel file: ./include/linux/atomic/atomic-instrumented.h β€” that's the one that declares this whole crew, iykyk 🧚


and that's the page, bestie πŸ’– ok bye, go swap something atomically, you're gonna slay ✨

source ./include/linux/atomic Β· dated October 2026 Β· page atomic_xchg(9) β€” mwah, catch you in the next manpage πŸ’…πŸŽ€

πŸ“¦ where this page comes from