atomic_try_cmpxchg_relaxed(9)
β¨ 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 toatomic_t. this is the variable we're doing the whole operation on, no big deal πoldβ a pointer to anintvalue, and it's the thing we compare against. heads up, she's also an out param later, so she's booked and busy πnewβ theintvalue 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 innoinstrcode! if you're innoinstrland, useraw_atomic_try_cmpxchg_relaxedthere 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_*
- read & set:
atomic_read(9),atomic_read_acquire(9),atomic_set(9),atomic_set_release(9) - add:
atomic_add(9),atomic_add_return(9),atomic_add_return_acquire(9),atomic_add_return_release(9),atomic_add_return_relaxed(9),atomic_fetch_add(9),atomic_fetch_add_acquire(9),atomic_fetch_add_release(9),atomic_fetch_add_relaxed(9) - sub:
atomic_sub(9),atomic_sub_return(9),atomic_sub_return_acquire(9),atomic_sub_return_release(9),atomic_sub_return_relaxed(9),atomic_fetch_sub(9),atomic_fetch_sub_acquire(9),atomic_fetch_sub_release(9),atomic_fetch_sub_relaxed(9) - inc:
atomic_inc(9),atomic_inc_return(9),atomic_inc_return_acquire(9),atomic_inc_return_release(9),atomic_inc_return_relaxed(9),atomic_fetch_inc(9),atomic_fetch_inc_acquire(9),atomic_fetch_inc_release(9),atomic_fetch_inc_relaxed(9) - dec:
atomic_dec(9),atomic_dec_return(9),atomic_dec_return_acquire(9),atomic_dec_return_release(9),atomic_dec_return_relaxed(9),atomic_fetch_dec(9),atomic_fetch_dec_acquire(9),atomic_fetch_dec_release(9),atomic_fetch_dec_relaxed(9) - and:
atomic_and(9),atomic_fetch_and(9),atomic_fetch_and_acquire(9),atomic_fetch_and_release(9),atomic_fetch_and_relaxed(9) - andnot:
atomic_andnot(9),atomic_fetch_andnot(9),atomic_fetch_andnot_acquire(9),atomic_fetch_andnot_release(9),atomic_fetch_andnot_relaxed(9) - or:
atomic_or(9),atomic_fetch_or(9),atomic_fetch_or_acquire(9),atomic_fetch_or_release(9),atomic_fetch_or_relaxed(9) - xor:
atomic_xor(9),atomic_fetch_xor(9),atomic_fetch_xor_acquire(9),atomic_fetch_xor_release(9),atomic_fetch_xor_relaxed(9) - xchg:
atomic_xchg(9),atomic_xchg_acquire(9),atomic_xchg_release(9),atomic_xchg_relaxed(9) - cmpxchg & friends (our girl's immediate family π«Ά):
atomic_cmpxchg(9),atomic_cmpxchg_acquire(9),atomic_cmpxchg_release(9),atomic_cmpxchg_relaxed(9),atomic_try_cmpxchg(9),atomic_try_cmpxchg_acquire(9),atomic_try_cmpxchg_release(9) - the test girlies:
atomic_sub_and_test(9),atomic_dec_and_test(9),atomic_inc_and_test(9) - add_negative squad:
atomic_add_negative(9),atomic_add_negative_acquire(9),atomic_add_negative_release(9),atomic_add_negative_relaxed(9) - the "unless" collection:
atomic_fetch_add_unless(9),atomic_add_unless(9),atomic_inc_not_zero(9),atomic_inc_unless_negative(9),atomic_dec_unless_positive(9),atomic_dec_if_positive(9)
π¦ the 64-bit icons β atomic64_*
- read & set:
atomic64_read(9),atomic64_read_acquire(9),atomic64_set(9),atomic64_set_release(9) - add:
atomic64_add(9),atomic64_add_return(9),atomic64_add_return_acquire(9),atomic64_add_return_release(9),atomic64_add_return_relaxed(9),atomic64_fetch_add(9),atomic64_fetch_add_acquire(9),atomic64_fetch_add_release(9),atomic64_fetch_add_relaxed(9) - sub:
atomic64_sub(9),atomic64_sub_return(9),atomic64_sub_return_acquire(9),atomic64_sub_return_release(9),atomic64_sub_return_relaxed(9),atomic64_fetch_sub(9),atomic64_fetch_sub_acquire(9),atomic64_fetch_sub_release(9),atomic64_fetch_sub_relaxed(9) - inc:
atomic64_inc(9),atomic64_inc_return(9),atomic64_inc_return_acquire(9),atomic64_inc_return_release(9),atomic64_inc_return_relaxed(9),atomic64_fetch_inc(9),atomic64_fetch_inc_acquire(9),atomic64_fetch_inc_release(9),atomic64_fetch_inc_relaxed(9) - dec:
atomic64_dec(9),atomic64_dec_return(9),atomic64_dec_return_acquire(9),atomic64_dec_return_release(9),atomic64_dec_return_relaxed(9),atomic64_fetch_dec(9),atomic64_fetch_dec_acquire(9),atomic64_fetch_dec_release(9),atomic64_fetch_dec_relaxed(9) - and:
atomic64_and(9),atomic64_fetch_and(9),atomic64_fetch_and_acquire(9),atomic64_fetch_and_release(9),atomic64_fetch_and_relaxed(9) - andnot:
atomic64_andnot(9),atomic64_fetch_andnot(9),atomic64_fetch_andnot_acquire(9),atomic64_fetch_andnot_release(9),atomic64_fetch_andnot_relaxed(9) - or:
atomic64_or(9),atomic64_fetch_or(9),atomic64_fetch_or_acquire(9),atomic64_fetch_or_release(9),atomic64_fetch_or_relaxed(9) - xor:
atomic64_xor(9),atomic64_fetch_xor(9),atomic64_fetch_xor_acquire(9),atomic64_fetch_xor_release(9),atomic64_fetch_xor_relaxed(9) - xchg:
atomic64_xchg(9),atomic64_xchg_acquire(9),atomic64_xchg_release(9),atomic64_xchg_relaxed(9) - cmpxchg crew:
atomic64_cmpxchg(9),atomic64_cmpxchg_acquire(9),atomic64_cmpxchg_release(9),atomic64_cmpxchg_relaxed(9),atomic64_try_cmpxchg(9),atomic64_try_cmpxchg_acquire(9),atomic64_try_cmpxchg_release(9),atomic64_try_cmpxchg_relaxed(9) - the test girlies:
atomic64_sub_and_test(9),atomic64_dec_and_test(9),atomic64_inc_and_test(9) - add_negative squad:
atomic64_add_negative(9),atomic64_add_negative_acquire(9),atomic64_add_negative_release(9),atomic64_add_negative_relaxed(9) - the "unless" collection:
atomic64_fetch_add_unless(9),atomic64_add_unless(9),atomic64_inc_not_zero(9),atomic64_inc_unless_negative(9),atomic64_dec_unless_positive(9),atomic64_dec_if_positive(9)
β the long-distance besties β atomic_long_*
- read & set:
atomic_long_read(9),atomic_long_read_acquire(9),atomic_long_set(9),atomic_long_set_release(9) - add:
atomic_long_add(9),atomic_long_add_return(9),atomic_long_add_return_acquire(9),atomic_long_add_return_release(9),atomic_long_add_return_relaxed(9),atomic_long_fetch_add(9),atomic_long_fetch_add_acquire(9),atomic_long_fetch_add_release(9),atomic_long_fetch_add_relaxed(9) - sub:
atomic_long_sub(9),atomic_long_sub_return(9),atomic_long_sub_return_acquire(9),atomic_long_sub_return_release(9),atomic_long_sub_return_relaxed(9),atomic_long_fetch_sub(9),atomic_long_fetch_sub_acquire(9),atomic_long_fetch_sub_release(9),atomic_long_fetch_sub_relaxed(9) - inc:
atomic_long_inc(9),atomic_long_inc_return(9),atomic_long_inc_return_acquire(9),atomic_long_inc_return_release(9),atomic_long_inc_return_relaxed(9),atomic_long_fetch_inc(9),atomic_long_fetch_inc_acquire(9),atomic_long_fetch_inc_release(9),atomic_long_fetch_inc_relaxed(9) - dec:
atomic_long_dec(9),atomic_long_dec_return(9),atomic_long_dec_return_acquire(9),atomic_long_dec_return_release(9),atomic_long_dec_return_relaxed(9),atomic_long_fetch_dec(9),atomic_long_fetch_dec_acquire(9),atomic_long_fetch_dec_release(9),atomic_long_fetch_dec_relaxed(9) - and:
atomic_long_and(9),atomic_long_fetch_and(9),atomic_long_fetch_and_acquire(9),atomic_long_fetch_and_release(9),atomic_long_fetch_and_relaxed(9) - andnot:
atomic_long_andnot(9),atomic_long_fetch_andnot(9),atomic_long_fetch_andnot_acquire(9),atomic_long_fetch_andnot_release(9),atomic_long_fetch_andnot_relaxed(9) - or:
atomic_long_or(9),atomic_long_fetch_or(9),atomic_long_fetch_or_acquire(9),atomic_long_fetch_or_release(9),atomic_long_fetch_or_relaxed(9) - xor:
atomic_long_xor(9),atomic_long_fetch_xor(9),atomic_long_fetch_xor_acquire(9),atomic_long_fetch_xor_release(9),atomic_long_fetch_xor_relaxed(9) - xchg:
atomic_long_xchg(9),atomic_long_xchg_acquire(9),atomic_long_xchg_release(9),atomic_long_xchg_relaxed(9) - cmpxchg crew:
atomic_long_cmpxchg(9),atomic_long_cmpxchg_acquire(9),atomic_long_cmpxchg_release(9),atomic_long_cmpxchg_relaxed(9),atomic_long_try_cmpxchg(9),atomic_long_try_cmpxchg_acquire(9),atomic_long_try_cmpxchg_release(9),atomic_long_try_cmpxchg_relaxed(9) - the test girlies:
atomic_long_sub_and_test(9),atomic_long_dec_and_test(9),atomic_long_inc_and_test(9) - add_negative squad:
atomic_long_add_negative(9),atomic_long_add_negative_acquire(9),atomic_long_add_negative_release(9),atomic_long_add_negative_relaxed(9) - the "unless" collection:
atomic_long_fetch_add_unless(9),atomic_long_add_unless(9),atomic_long_inc_not_zero(9),atomic_long_inc_unless_negative(9),atomic_long_dec_unless_positive(9),atomic_long_dec_if_positive(9)
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
- nixpkgs unstable: linux-manual 7.3.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms