Locking
Locking
Posted Sep 23, 2024 18:45 UTC (Mon) by intelfx (subscriber, #130118)In reply to: Locking by tialaramex
Parent article: Resources for learning Rust for kernel development
> The lack of "unlock" has led to proposals to give Rust's Mutex<T> type an unlock method.
>
> The way you'd do that is your unlock function takes the guard as a parameter. Since Rust has the destructive move semantic, the unlock doesn't need to actually "do" anything, it can return immediately - the guard was moved into the function and then not returned, it's gone - it was dropped, which gives effect to the programmer's intent.
>
> The way you'd do that is your unlock function takes the guard as a parameter. Since Rust has the destructive move semantic, the unlock doesn't need to actually "do" anything, it can return immediately - the guard was moved into the function and then not returned, it's gone - it was dropped, which gives effect to the programmer's intent.
And then you will have half of the code using this no-op `.unlock()` and the other half of the code relying on the implicit Drop of the guard in the function scope itself. Sounds like a recipe for "disaster" levels of inconsistency.
You can, of course, mandate one way or the other via a code style document and/or linters. But that would add friction and strike another blow to the "it compiles, ergo it works" ideal.
