|
|
Log in / Subscribe / Register

Locking

Locking

Posted Sep 24, 2024 17:31 UTC (Tue) by daroc (editor, #160859)
In reply to: Locking by viro
Parent article: Resources for learning Rust for kernel development

The parent-before-child order is actually fairly easy to enforce in Rust without using the technique from the talk (which was focused on imposing a global order). For the tree case, imagine a node looks like this:

    struct Node {
        some_unprotected_data: usize,
        protected_data: Mutex<NodeInner>,
    }
    struct NodeInner {
        more_protected_data: usize,
        children: Vec<Node>,
    }

By putting all the data that should only be accessed with a Mutex held inside an inner structure like that, we can enforce that the program can only have references to it that live as long as the lock is held. So if I had a reference to a Node, I couldn't access the children until I locked the Mutex. And then I could only hold on for references to them until I unlocked the Mutex, at which point the compiler would no longer let me use them.


to post comments


Copyright © 2026, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds