A mutex that is not recursive is locked again by the thread that holds it
What it finds
std::lock_guard<std::mutex> lock(m_); ... refresh(); where refresh takes the same lock: a std::mutex that the calling thread already holds must not be locked again. The behaviour is undefined and in practice the thread waits for itself forever.
``cpp void Cache::put(Key k, Value v) { std::lock_guard<std::mutex> lock(mutex_); map_[k] = v; trim(); // trim() also locks mutex_ } ``
The rule looks at member functions that hold a lock of std::lock_guard, std::unique_lock or std::scoped_lock on a mutex the file declares as std::mutex, std::timed_mutex or std::shared_mutex, and reports, later in the block of the lock, a second lock of the same mutex or a call of a member function of the same class whose definition in the file locks it. Functions that unlock or that defer or adopt the lock are left alone, and so are calls inside lambdas, which may run later.
From the rule's source, mutex_locked_twice.rs.
Turn it on, off or suppress it
The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/cpp:mutex-locked-twice" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/cpp:mutex-locked-twice] reason="..."tx0 explain tx0/cpp:mutex-locked-twice shows the same in the terminal. Found a false positive? Report it.