-
Notifications
You must be signed in to change notification settings - Fork 37.7k
More thread safety annotation coverage #16127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. ConflictsReviewers, this pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. |
Strong concept ACK: thanks for fixing this! |
Can you explain why? |
@promag Our "default" mutexes are recursive mutexes (which you can enter multiple times from the same thread). They're easier to work with as you don't need to worry about whether to call a lock-grabbing function from either inside or outside the lock. However, recursive mutexes can't be used for condition variable waiting, so for those, we use std::mutex (which is not recursive) directly. Longer term we should try to get rid of recursive mutexes entirely; code is much cleaner without them. |
How is this different from using E.g. Mutex m;
LOCK(m); |
@sipa thanks, I was aware of that. I wasn't sure why "In a few cases we need to use |
If you do
There's some cases where we currently use I think that means it makes sense to:
I've bumped this PR to replace a couple of the |
I don't think this is true (that we can't allow a circular dependency), but maybe you can explain more. I think ideally, we would just use |
Maybe; There's also the case if you compile with
If we can switch all the uses of std::mutex to Mutex, I don't think there's a need for MutexGuard. |
36de8e5
to
165d3ed
Compare
Rebased; updated to only use Remaining uses of |
Rebased for the ToString changes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
re-ACK d61883d, the only changes since the previous review are addressed nits:
- dropped redundant capture in
RenameThisThread
lambda - dropped redundant
fScanningWallet = true;
assignment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code review ACK d61883d. Would call MutexGuard class LockGuard to make it an obvious replacement for std::lock_guard
and use standard names for things where Mutex is the thing being locked, and Lock is the object locking and unlocking it
Adds LockGuard helper in threadsafety.h to replace lock_guard<mutex> when LOCK(Mutex) isn't available for use.
Renamed MutexGuard to LockGuard, and dropped util_threadnames_tests changes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK 5478d6c 🗾 Show signature and timestampSignature:
Timestamp of file with hash |
5478d6c logging: thread safety annotations (Anthony Towns) e685ca1 util/system.cpp: add thread safety annotations for dir_locks (Anthony Towns) a788789 test/checkqueue_tests: thread safety annotations (Anthony Towns) 479c584 rpc/blockchain.cpp: thread safety annotations for latestblock (Anthony Towns) 8b5af3d net: fMsgProcWake use LOCK instead of lock_guard (Anthony Towns) de7c5f4 wallet/wallet.h: Remove mutexScanning which was only protecting a single atomic bool (Anthony Towns) c3cf2f5 rpc/blockchain.cpp: Remove g_utxosetscan mutex that is only protecting a single atomic variable (Anthony Towns) Pull request description: In a few cases we need to use `std::mutex` rather than the sync.h primitives. But `std::lock_guard<std::mutex>` doesn't include the clang thread safety annotations unless you also use clang's C library, which means you can't indicate when variables should be guarded by `std::mutex` mutexes. This adds an annotated version of `std::lock_guard<std::mutex>` to threadsafety.h to fix that, and modifies places where `std::mutex` is used to take advantage of the annotations. It's based on top of bitcoin#16112, and turns the thread safety comments included there into annotations. It also changes the RAII classes in wallet/wallet.h and rpc/blockchain.cpp to just use the atomic<bool> flag for synchronisation rather than having a mutex that doesn't actually guard anything as well. ACKs for top commit: MarcoFalke: ACK 5478d6c 🗾 hebasto: re-ACK 5478d6c, only renamed s/`MutexGuard`/`LockGuard`/, and dropped the commit "test/util_threadnames_tests: add thread safety annotations" since the [previous](bitcoin#16127 (review)) review. ryanofsky: Code review ACK 5478d6c. Thanks for taking suggestions! Only changes since last review are dropping thread rename test commit d53072e and renaming mutex guard to lock guard Tree-SHA512: 7b00d31f6f2b5a222ec69431eb810a74abf0542db3a65d1bbad54e354c40df2857ec89c00b4a5e466c81ba223267ca95f3f98d5fbc1a1d052a2c3a7d2209790a
Summary: * rpc/blockchain.cpp: Remove g_utxosetscan mutex that is only protecting a single atomic variable * wallet/wallet.h: Remove mutexScanning which was only protecting a single atomic bool * net: fMsgProcWake use LOCK instead of lock_guard * rpc/blockchain.cpp: thread safety annotations for latestblock * test/checkqueue_tests: thread safety annotations * util/system.cpp: add thread safety annotations for dir_locks * logging: thread safety annotations Adds LockGuard helper in threadsafety.h to replace lock_guard<mutex> when LOCK(Mutex) isn't available for use. This is a backport of Core [[bitcoin/bitcoin#16127 | PR16127]] Test Plan: ninja all check Using clang Reviewers: #bitcoin_abc, majcosta Reviewed By: #bitcoin_abc, majcosta Differential Revision: https://reviews.bitcoinabc.org/D7523
merge bitcoin#11640, bitcoin#11599, bitcoin#16112, bitcoin#16127, bitcoin#18635, bitcoin#19249: thread safety and locking improvements
In a few cases we need to use
std::mutex
rather than the sync.h primitives. Butstd::lock_guard<std::mutex>
doesn't include the clang thread safety annotations unless you also use clang's C library, which means you can't indicate when variables should be guarded bystd::mutex
mutexes.This adds an annotated version of
std::lock_guard<std::mutex>
to threadsafety.h to fix that, and modifies places wherestd::mutex
is used to take advantage of the annotations.It's based on top of #16112, and turns the thread safety comments included there into annotations.
It also changes the RAII classes in wallet/wallet.h and rpc/blockchain.cpp to just use the atomic flag for synchronisation rather than having a mutex that doesn't actually guard anything as well.