-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: implement read write locks in threading and use them for CActiveMasternodeManager::cs #5961
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
feat: implement read write locks in threading and use them for CActiveMasternodeManager::cs #5961
Conversation
UdjinM6
left a comment
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.
pls check be16627
555b4b5 to
0692826
Compare
| typedef AnnotatedMixin<std::mutex> Mutex; | ||
| /** Wrapped shared mutex: supports read locking via .shared_lock, exlusive locking via .lock; | ||
| * does not support recursive locking */ | ||
| typedef SharedAnnotatedMixin<std::shared_mutex> SharedMutex; |
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.
nit: why won't use modern syntax such as:
using SharedMutex = SharedAnnotatedMixin<std::shared_mutex>
knst
left a comment
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.
LGTM
|
|
||
| bool TrySharedEnter(const char* pszName, const char* pszFile, int nLine) | ||
| { | ||
| EnterCritical(pszName, pszFile, nLine, Base::mutex(), true); |
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.
| EnterCritical(pszName, pszFile, nLine, Base::mutex(), true); | |
| EnterCritical(pszName, pszFile, nLine, Base::mutex(), /*fTry = */ true); |
| SharedLock(Mutex* pmutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) SHARED_LOCK_FUNCTION(pmutexIn) | ||
| { | ||
| if (!pmutexIn) return; | ||
|
|
||
| *static_cast<Base*>(this) = Base(*pmutexIn, std::defer_lock); | ||
| if (fTry) { | ||
| TrySharedEnter(pszName, pszFile, nLine); | ||
| } else { | ||
| SharedEnter(pszName, pszFile, nLine); | ||
| } | ||
| } |
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.
why we need even to consider a situation when pmutexIn can be non-null ptr?
ok it seems as copy-paste of UniqueLock
| SharedLock(Mutex* pmutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) SHARED_LOCK_FUNCTION(pmutexIn) | |
| { | |
| if (!pmutexIn) return; | |
| *static_cast<Base*>(this) = Base(*pmutexIn, std::defer_lock); | |
| if (fTry) { | |
| TrySharedEnter(pszName, pszFile, nLine); | |
| } else { | |
| SharedEnter(pszName, pszFile, nLine); | |
| } | |
| } |
| const bool is_basic_scheme_active{DeploymentActiveAfter(tip, Params().GetConsensus(), Consensus::DEPLOYMENT_V19)}; | ||
| auto pk = ::activeMasternodeManager->GetPubKey(); | ||
| const CBLSPublicKeyVersionWrapper pubKey(pk, !is_basic_scheme_active); | ||
| uint256 signHash = [&]() { |
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.
missing const:
| uint256 signHash = [&]() { | |
| const uint256 signHash = [&]() { |
|
hm, what if there would be too many reading operations which are running in many threads - is there any chance that regular lock ever be acquired? or it would wait until all reading operations are done? |
From the paper that introduces them
Based on this I don't think there is an ability for starvation |
UdjinM6
left a comment
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.
utACK
knst
left a comment
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.
utACK 0692826
, bitcoin#24830, bitcoin#24464, bitcoin#24757, bitcoin#25202, bitcoin#25217, bitcoin#25292, bitcoin#25614, partial bitcoin#22766 (logging backports) 1621696 log: restore `LogPrintLevel` messages from prior backports (Kittywhiskers Van Gogh) 52a1263 merge bitcoin#25614: Severity-based logging, step 2 (Kittywhiskers Van Gogh) 21470fd merge bitcoin#25292: Add LogPrintLevel to lint-format-strings, drop LogPrint-vs-LogPrintf section in dev notes (Kittywhiskers Van Gogh) 026409e merge bitcoin#25217: update lint-logs.py to detect LogPrintLevel, mention WalletLogPrintf (Kittywhiskers Van Gogh) b046e09 merge bitcoin#25202: Use severity-based logging for leveldb/libevent messages, reverse LogPrintLevel order (Kittywhiskers Van Gogh) 7697b73 revert dash#2794: Disable logging of libevent debug messages (Kittywhiskers Van Gogh) ff6304f merge bitcoin#24757: add `DEBUG_LOCKCONTENTION` to `--enable-debug` and CI (Kittywhiskers Van Gogh) 88592f3 merge bitcoin#24464: Add severity level to logs (Kittywhiskers Van Gogh) d3e837a merge bitcoin#24830: Allow -proxy="" setting values (Kittywhiskers Van Gogh) 0e01d5b partial bitcoin#22766: Clarify and disable unused ArgsManager flags (Kittywhiskers Van Gogh) a9cfbd1 fix: don't use non-existent `PrintLockContention` in `SharedEnter` (Kittywhiskers Van Gogh) f331cbe merge bitcoin#24770: Put lock logging behind DEBUG_LOCKCONTENTION preprocessor directive (Kittywhiskers Van Gogh) d9cc2ea merge bitcoin#23104: Avoid breaking single log lines over multiple lines in the log file (Kittywhiskers Van Gogh) 479ae82 merge bitcoin#23235: Reduce unnecessary default logging (Kittywhiskers Van Gogh) Pull request description: ## Additional Information * This pull request's primary purpose is to restore `LogPrintLevel`s from backports in [dash#6333](#6333) that were changed to `LogPrint`s as they were backported before `LogPrintLevel` was backported. * ~~`clang-format` suggestions for `LogPrintLevel` have to be ignored in order to prevent the linter from tripping due to a "missing newline" ([build](https://gitlab.com/dashpay/dash/-/jobs/8398818860#L54)).~~ Resolved by applying diff ([source](#6399 (comment))). * `SharedLock` was introduced in [dash#5961](#5961) and `PrintLockContention` was removed in [dash#6046](#6046) but the changes in the latter were not extended to the former. This has been corrected as part of this pull request. ## Breaking Changes None expected. ## Checklist - [x] I have performed a self-review of my own code - [x] I have commented my code, particularly in hard-to-understand areas **(note: N/A)** - [x] I have added or updated relevant unit/integration/functional/e2e tests - [x] I have made corresponding changes to the documentation - [x] I have assigned this pull request to a milestone _(for repository code-owners and collaborators only)_ Top commit has no ACKs. Tree-SHA512: f2d0ef8ce5cb1091c714a2169e89deb33fa71ff174ce4e6147b3ad421f57a84183d2a9e76736c0b064b2cc70fb3f2e545c42b8562cf36fdce18c3fb61307c364
Issue being fixed or feature implemented
We have some caches or other information in codebase which are read from a lot; but rarely written to. We can use a RW lock here instead of a normal Mutex
What was done?
Implement a RW lock and use them
How Has This Been Tested?
Hasn't been much; looking for review atm. Maybe should deploy this on testnet for a bit and make sure it doesn't break.
Breaking Changes
Checklist:
Go over all the following points, and put an
xin all the boxes that apply.