Mutex in kernel::sync::lock::mutex
This is the reference page for the Linux kernel's mutex as seen from Rust. Only one thread at a time may hold it; the others sleep until it is released, which is why it cannot be used where sleeping …
Two processes can call into your driver at the same moment on two processors, so shared state needs a lock. The kernel's Rust mutex does not sit beside the data it protects, it contains it, and the only way to reach the data is to lock and receive a guard whose existence is the proof the lock is held; when the guard goes out of scope the lock is released. That makes the two classic C bugs, touching shared data without the lock and returning from a function while still holding it, impossible to write. A mutex may put the caller to sleep, so it is only legal in normal context.
This is the reference page for the Linux kernel's mutex as seen from Rust. Only one thread at a time may hold it; the others sleep until it is released, which is why it cannot be used where sleeping …