Avoid locking mutexes that are already held by the same thread
Summary:
Avoid locking mutexes that are already held by the same thread. These are reentrant mutexes, but still no need to lock them more than once per thread :-)
Backport of Bitcoin Core PR11762
https://github.com/bitcoin/bitcoin/pull/11762
Test Plan:
- Build with Clang in Debug mode:
CXX=clang++ CC=clang cmake .. -GNinja -DCMAKE_BUILD_TYPE=Debug ninja check
- Verify that the compiler has not emitted a thread-safety warning.
- Run the node: ./src/bitcoind -regtest
- Verify that text similar to "Assertion failed: lock ... not held ..." is not printed on stderr.
Reviewers: Fabien, #bitcoin_abc, deadalnix
Reviewed By: Fabien, #bitcoin_abc, deadalnix
Differential Revision: https://reviews.bitcoinabc.org/D4145