down_trylock(9)
π down_trylock(9) β bestie edition
okay so you opened the Kernel API Manual page for down_trylock(9) and honestly? this one is short and sweet and the plot twist is at the end, so stay with me π«Ά
β¨ NAME β¨
down_trylock β try to grab the semaphore, but likeβ¦ without waiting. no queuing, no standing in line, no "give me one sec". it's giving attempt and dip π¨
πΈ SYNOPSIS πΈ
int down_trylock (struct semaphore *sem );
π ARGUMENTS π
semβ the semaphore you're trying to acquire. that's it. that's the whole argument list. iconic minimalism.
π¦ DESCRIPTION π¦
so the whole bit here is: try to acquire the semaphore atomically. like, one clean indivisible moment, no weird half-states, no "almost got it" energy.
what does it return? literally just two possibilities:
- returns
0if the semaphore got acquired successfully β slay, you got it π - returns
1if it cannot be acquired β no luck, moving on π
that's it. it does not wait. it does not block. it just checks and reports back like a very honest friend.
β οΈ NOTE β οΈ
okay so this is the part where you need to actually pay attention bestie, because the return value is inverted from both spin_trylock and mutex_trylock π
like,0means success and1means failure here. yes, really. no cap. be so careful about this when you're converting code, because that flip will absolutely bite you.
also, unlike mutex_trylock, this function can be used from interrupt context, and the semaphore can be released by any task or interrupt. so it's way more flexible about who does the freeing. very main character energy, very "i don't gatekeep" π
π₯ SEE ALSO π₯
- Kernel file
./kernel/locking/semaphore.c down(9)down_interruptible(9)down_killable(9)down_timeout(9)up(9)
k that's the whole page, you're welcome π this guide lives in the ./kernel/locking directory, dated October 2026, and if you made it this far β yes you get a gold star β and the title down_trylock(9), which is also just the name of the function, because manpages love a callback π
π¦ 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