Skip to content

[libc++] missing thread safety annotations for std::unique_lock and std::shared_lockΒ #117480

@tamird

Description

@tamird
#include <mutex>
#include <shared_mutex>

class Foo {
    int guard() {
        std::lock_guard l(lock_);
        return state_; // no warning
    }
    int unique() {
        std::unique_lock l(lock_);
        return state_; // warning: reading variable 'state_' requires holding shared_mutex 'lock_' [-Wthread-safety-analysis]
    }
    int shared() {
        std::shared_lock l(lock_);
        return state_; // warning: reading variable 'state_' requires holding shared_mutex 'lock_' [-Wthread-safety-analysis]
    }
    private:
    int state_ __attribute__((guarded_by(lock_)));
    std::shared_mutex lock_;
};

https://godbolt.org/z/hq7f9xT9K

Is there a reason these can't be annotated?

Metadata

Metadata

Assignees

No one assigned

    Labels

    libc++libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions