mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-04-06 13:47:56 +02:00
Merge bitcoin/bitcoin#34823: threading: never require logging from sync.h (take 2)
79467e3ec7threading: never require logging from sync.h (Cory Fields) Pull request description: This is an updated version of #34793 after stickies-v [pointed out that the approach there was broken](https://github.com/bitcoin/bitcoin/pull/34793#discussion_r2916369196). sync.h is low-level and should not require any other subsystems. Move the lone remaining logging call to the .cpp. Any cost incurred by an additional function call should be trivial compared to the logging itself. ACKs for top commit: stickies-v: re-ACK79467e3ec7, thanks for doing the modernization right away 👍 sedited: ACK79467e3ec7Tree-SHA512: 53e4b19cbf6ec81ce6aee06092a07e56877e2247a2614a148ba25c276dc5cf00d68445f9cd43dbd71e4481da74b788e3e20d3634484760d037f94d44bf13ea9c
This commit is contained in:
14
src/sync.cpp
14
src/sync.cpp
@@ -4,6 +4,7 @@
|
||||
|
||||
#include <sync.h>
|
||||
|
||||
#include <logging/timer.h>
|
||||
#include <tinyformat.h>
|
||||
#include <util/log.h>
|
||||
#include <util/strencodings.h>
|
||||
@@ -19,6 +20,19 @@
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#ifdef DEBUG_LOCKCONTENTION
|
||||
|
||||
template <typename LockType>
|
||||
void ContendedLock(std::string_view name, std::string_view file, int nLine, LockType& lock)
|
||||
{
|
||||
LOG_TIME_MICROS_WITH_CATEGORY(strprintf("lock contention %s, %s:%d", name, file, nLine), BCLog::LOCK);
|
||||
lock.lock();
|
||||
}
|
||||
template void ContendedLock(std::string_view name, std::string_view file, int nLine, std::unique_lock<std::mutex>& lock);
|
||||
template void ContendedLock(std::string_view name, std::string_view file, int nLine, std::unique_lock<std::recursive_mutex>& lock);
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef DEBUG_LOCKORDER
|
||||
//
|
||||
// Early deadlock detection.
|
||||
|
||||
22
src/sync.h
22
src/sync.h
@@ -6,10 +6,6 @@
|
||||
#ifndef BITCOIN_SYNC_H
|
||||
#define BITCOIN_SYNC_H
|
||||
|
||||
#ifdef DEBUG_LOCKCONTENTION
|
||||
#include <logging/timer.h>
|
||||
#endif
|
||||
|
||||
#include <threadsafety.h> // IWYU pragma: export
|
||||
#include <util/macros.h>
|
||||
|
||||
@@ -77,6 +73,16 @@ inline void DeleteLock(void* cs) {}
|
||||
inline bool LockStackEmpty() { return true; }
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Called when a mutex fails to lock immediately because it is held by another
|
||||
* thread, or spuriously. Responsible for locking the lock before returning.
|
||||
*/
|
||||
#ifdef DEBUG_LOCKCONTENTION
|
||||
|
||||
template <typename LockType>
|
||||
void ContendedLock(std::string_view name, std::string_view file, int nLine, LockType& lock);
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Template mixin that adds -Wthread-safety locking annotations and lock order
|
||||
* checking to a subset of the mutex API.
|
||||
@@ -151,10 +157,12 @@ private:
|
||||
{
|
||||
EnterCritical(pszName, pszFile, nLine, Base::mutex());
|
||||
#ifdef DEBUG_LOCKCONTENTION
|
||||
if (Base::try_lock()) return;
|
||||
LOG_TIME_MICROS_WITH_CATEGORY(strprintf("lock contention %s, %s:%d", pszName, pszFile, nLine), BCLog::LOCK);
|
||||
#endif
|
||||
if (!Base::try_lock()) {
|
||||
ContendedLock(pszName, pszFile, nLine, static_cast<Base&>(*this));
|
||||
}
|
||||
#else
|
||||
Base::lock();
|
||||
#endif
|
||||
}
|
||||
|
||||
bool TryEnter(const char* pszName, const char* pszFile, int nLine)
|
||||
|
||||
Reference in New Issue
Block a user