atomic_try_cmpxchg_relaxed(9)

πŸ“¦ version 7.3.1, in nixpkgs unstable

✨ atomic_try_cmpxchg_relaxed ✨

okay so bestie, this is a page from the Kernel API Manual β€” yes, the Kernel API Manual, we're doing real kernel hours πŸ’… β€” and it's all about one lil function called atomic_try_cmpxchg_relaxed. it does a compare-and-exchange on an atomic variable, but ~chill~ about the ordering. let's get into it 🫢


🌸 NAME 🌸

atomic_try_cmpxchg_relaxed β€” the atomic compare and exchange that's got relaxed ordering vibes ✨


πŸ¦‹ SYNOPSIS πŸ¦‹

bool atomic_try_cmpxchg_relaxed (atomic_t *v , int *old , int new );

like, look at her. returns a bool, takes three things. iconic signature honestly πŸ‘‘


πŸŽ€ ARGUMENTS πŸŽ€

  • v β€” a pointer to atomic_t. this is the variable we're doing the whole operation on, no big deal πŸ’–
  • old β€” a pointer to an int value, and it's the thing we compare against. heads up, she's also an out param later, so she's booked and busy 😭
  • new β€” the int value we're gonna assign. the new kid on the block πŸ¦‹

πŸ”₯ DESCRIPTION πŸ”₯

okay so here's the tea β˜• β€” if (v == old), then v gets atomically updated to new with relaxed ordering. slay, done, exchange complete ✨

but if that comparison does not hold? then v is not modified at all (she stays exactly as she was, main character energy πŸ’…), and instead old gets updated to the current value of v. and relaxed ordering is provided either way, so that part's consistent, no cap.

⚠️ important warning bestie, do not skip this: it is unsafe to use in noinstr code! if you're in noinstr land, use raw_atomic_try_cmpxchg_relaxed there instead. i'm literally begging you πŸ™ˆ

πŸ’€ RETURN πŸ’€

  • true β€” the exchange actually happened πŸŽ‰
  • false β€” it did not, better luck next time 😭

🌈 SEE ALSO 🌈

the whole extended family, fr fr. these all live in the kernel file ./include/linux/atomic/atomic-instrumented.h by the way 🧚

πŸ’œ the 32-bit girlies β€” atomic_*

πŸ¦‹ the 64-bit icons β€” atomic64_*

⭐ the long-distance besties β€” atomic_long_*


and that's the whole page bestie, you ate that πŸ’…βœ¨ go forth and compare-and-exchange responsibly (and remember: not in noinstr code, use raw_atomic_try_cmpxchg_relaxed there, i'm watching πŸ‘€).

mwah 🫢 this lil tour came to you from ./include/linux/atomic, dated October 2026, and yes the page is literally called atomic_try_cmpxchg_relaxed(9) β€” she's booked, she's titled, she's the moment πŸ’–

πŸ“¦ where this page comes from