HomePhabricator

sync: detect double lock from the same thread

Description

sync: detect double lock from the same thread

Summary:

sync: make EnterCritical() & push_lock() type safe

The functions EnterCritical() and push_lock() take a pointer to a
mutex, but that pointer used to be of type void* because we use a few
different types for mutexes. This void* argument was not type safe
because somebody could have send a pointer to anything that is not a
mutex. Furthermore it wouldn't allow to check whether the passed mutex
is recursive or not.

Thus, change the functions to templated ones so that we can implement
stricter checks for non-recursive mutexes. This also simplifies the
callers of EnterCritical().

sync: detect double lock from the same thread

Double lock of the same (non-recursive) mutex from the same thread
is producing an undefined behavior. Detect this from DEBUG_LOCKORDER
and react similarly to the deadlock detection.

This is a backport of core#19337 and core#20507

Note that boost::mutex is no longer in use since D13966

Test Plan:

cmake .. -GNinja -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++
ninja all check-all

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Subscribers: Fabien

Differential Revision: https://reviews.bitcoinabc.org/D14388

Details

Provenance
Vasil Dimov <vd@FreeBSD.org>Authored on Jun 19 2020, 18:51
PiRKCommitted on Aug 21 2023, 10:07
PiRKPushed on Aug 21 2023, 10:07
Reviewer
Restricted Project
Differential Revision
D14388: sync: detect double lock from the same thread
Parents
rABCb8ca9a127777: build: Drop unneeded ApplicationServices framework dependency
Branches
Unknown
Tags
Unknown