generic___te...nd_clear_bit(9)
β¨ generic___test_and_clear_bit β bestie's guide to the kernel's little bit-flipper β¨
okay so real talk, this is page generic___te...nd_clear_bit(9) from the Kernel API Manual, and it's lowkey one of those tiny functions that does exactly what it says on the tin β but the vibes around it are the whole story. stay with me π
πΈ NAME πΈ
generic___test_and_clear_bit β it clears a bit and hands you back its old value. that's it. that's the whole personality π
π¦ SYNOPSIS π¦
bool generic___test_and_clear_bit (unsigned long nr , volatile unsigned
long *addr );
two args, one bool coming back at you. no cap, that's the whole thing β¨
π ARGUMENTS π
nrβ the bit you're clearing. yes, that bit. the one. πaddrβ the address you're counting from. π«Ά
π₯ DESCRIPTION π₯
bestie, listen to me very carefully, because this is the part that matters π
this operation is non-atomic and it can be reordered. like, it's not gonna hold your hand, it's not gonna lock anything down for you, it's just gonna go do its thing in whatever order the compiler and the CPU feel like. main character energy, but the chaotic kind.
and here's the actual tea: if two examples of this operation race, one can appear to succeed but actually fail. π i know. i KNOW. it looks like it worked and then it didn't.
so what do you do? you must protect multiple accesses with a lock. that's not a suggestion, that's the whole rule. protect your accesses, bestie, or the race will eat your logic for breakfast π
fr fr: this one is non-atomic AND reorderable AND racy. treat it like a hot stove, not a blanket. π₯
π SEE ALSO π
- Kernel file
./include/asm-generic/bitops/generic-non-atomic.hβ the whole squad lives here π generic___set_bit(9)generic___change_bit(9)generic___test_and_set_bit(9)generic_test_bit(9)generic_test_bit_acquire(9)const_test_bit(9)
go read the friends, they're all doing the same bit-flipping stuff in slightly different flavours β
π§ cute little sign-off π§
this iconic little page came out of ./include/asm-generic/bitops, dated October 2026, and it answers to the name generic___te...nd_clear_bit(9) π go clear your bits safely, mwah π¦π
π¦ 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