Skip to content

RwLock::read() should be reentrant so its behavior will be the same as borrow checker #114770

Open
@ultimaweapon

Description

@ultimaweapon

Let's say I have the following code:

struct Foo {
    items: RwLock<Vec<Item>>,
}

impl Foo {
    pub fn bar(&self) {
        for i in self.items.read().unwrap().deref() {
            self.some_method_that_will_endup_calling_baz(i);
        }
    }

    fn baz(&self, id: u32) {
        self.items.read().unwrap().iter().find(|i| i.id() == id);
    }
}

If someone call bar it may cause a panic in baz because the items is already locked by bar. The above code will be working if items is not putting behind RwLock but I will not be able to push the item into items. If I lock the Foo instead of its field it will solve this problem but it will cumbersome to work with Foo if most of it fields is immutable.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-atomicArea: Atomics, barriers, and sync primitivesT-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions