Locking
Locking
Posted Sep 23, 2024 19:02 UTC (Mon) by atnot (guest, #124910)In reply to: Locking by tialaramex
Parent article: Resources for learning Rust for kernel development
This is one reason I'd personally love to see true linear ("must move") types[1] in Rust. Not really for locks, I never have issues with those (although I do understand being scared of the thought as a C developer, especially one that is used to protecting code instead of data[2]). But there's a bunch of scenarios where you need a certain function to be called for correctness and things quickly get hairy when ordering, io, async and extra contextual info gets involved. In those cases you currently just have to resort to a whole lot of dirty hacks like stashing data in the struct just to have it available on drop, using thread locals, using must_use use lints, contorting the whole design to be robust to anything being dropped at any time, or doing things that are technically safe but non-desirable like leaking memory or panicking. There's just a whole lot of things that could be a lot nicer with some sort of "here's a thing, please give it back to me when you're done with it" mechanism.
And as a bonus, people can have their lock functions in a useful way too if they really want.
[1] for the unitiated: affine: at most once (Rust has this), linear: exactly once. I don't know why the math people use those words.
[2] https://blog.ffwll.ch/2022/07/locking-engineering.html
