Skip to content

Commit 2e77dff

Browse files
committed
Merge bitcoin/bitcoin#25676: sync: simplify and remove unused code from sync.h
75c3f9f sync: rename AnnotatedMixin::UniqueLock to AnnotatedMixin::unique_lock (Vasil Dimov) 8d9ee8e sync: remove DebugLock alias template (Vasil Dimov) 4b2e167 sync: avoid confusing name overlap (Mutex) (Vasil Dimov) 9d7ae4b sync: remove unused template parameter from ::UniqueLock (Vasil Dimov) 11c190e sync: simplify MaybeCheckNotHeld() definitions by using a template (Vasil Dimov) Pull request description: Summary: * Reduce 4 of the `MaybeCheckNotHeld()` definitions to 2 by using a template. * Remove unused template parameter from `::UniqueLock`. * Use `MutexType` instead of `Mutex` for a template parameter name to avoid overlap/confusion with the `Mutex` class. * Rename `AnnotatedMixin::UniqueLock` to `AnnotatedMixin::unique_lock` to avoid overlap/confusion with the global `UniqueLock` and for consistency with `UniqueLock::reverse_lock`. The first commit `sync: simplify MaybeCheckNotHeld() definitions by using a template` is also part of bitcoin/bitcoin#25390 ACKs for top commit: aureleoules: ACK 75c3f9f - LGTM ryanofsky: Code review ACK 75c3f9f. Nice cleanups! Just suggested changes since last review: keeping UniqueLock name and fixing a missed rename in a code comment Tree-SHA512: ec261f6a444bdfe4f06e844b57b3606fdd9b2f842647cae15266d9729970d87585c808d482fbba0b31c33a4aa03527c36e282c92b28d9052711f75a7048c96f1
2 parents 9ca39d6 + 75c3f9f commit 2e77dff

File tree

1 file changed

+19
-22
lines changed

1 file changed

+19
-22
lines changed

src/sync.h

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ class LOCKABLE AnnotatedMixin : public PARENT
111111
return PARENT::try_lock();
112112
}
113113

114-
using UniqueLock = std::unique_lock<PARENT>;
114+
using unique_lock = std::unique_lock<PARENT>;
115115
#ifdef __clang__
116116
//! For negative capabilities in the Clang Thread Safety Analysis.
117117
//! A negative requirement uses the EXCLUSIVE_LOCKS_REQUIRED attribute, in conjunction
@@ -147,11 +147,13 @@ inline void AssertLockNotHeldInline(const char* name, const char* file, int line
147147
inline void AssertLockNotHeldInline(const char* name, const char* file, int line, GlobalMutex* cs) LOCKS_EXCLUDED(cs) { AssertLockNotHeldInternal(name, file, line, cs); }
148148
#define AssertLockNotHeld(cs) AssertLockNotHeldInline(#cs, __FILE__, __LINE__, &cs)
149149

150-
/** Wrapper around std::unique_lock style lock for Mutex. */
151-
template <typename Mutex, typename Base = typename Mutex::UniqueLock>
152-
class SCOPED_LOCKABLE UniqueLock : public Base
150+
/** Wrapper around std::unique_lock style lock for MutexType. */
151+
template <typename MutexType>
152+
class SCOPED_LOCKABLE UniqueLock : public MutexType::unique_lock
153153
{
154154
private:
155+
using Base = typename MutexType::unique_lock;
156+
155157
void Enter(const char* pszName, const char* pszFile, int nLine)
156158
{
157159
EnterCritical(pszName, pszFile, nLine, Base::mutex());
@@ -173,15 +175,15 @@ class SCOPED_LOCKABLE UniqueLock : public Base
173175
}
174176

175177
public:
176-
UniqueLock(Mutex& mutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) EXCLUSIVE_LOCK_FUNCTION(mutexIn) : Base(mutexIn, std::defer_lock)
178+
UniqueLock(MutexType& mutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) EXCLUSIVE_LOCK_FUNCTION(mutexIn) : Base(mutexIn, std::defer_lock)
177179
{
178180
if (fTry)
179181
TryEnter(pszName, pszFile, nLine);
180182
else
181183
Enter(pszName, pszFile, nLine);
182184
}
183185

184-
UniqueLock(Mutex* pmutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) EXCLUSIVE_LOCK_FUNCTION(pmutexIn)
186+
UniqueLock(MutexType* pmutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) EXCLUSIVE_LOCK_FUNCTION(pmutexIn)
185187
{
186188
if (!pmutexIn) return;
187189

@@ -241,29 +243,24 @@ class SCOPED_LOCKABLE UniqueLock : public Base
241243

242244
#define REVERSE_LOCK(g) typename std::decay<decltype(g)>::type::reverse_lock UNIQUE_NAME(revlock)(g, #g, __FILE__, __LINE__)
243245

244-
template<typename MutexArg>
245-
using DebugLock = UniqueLock<typename std::remove_reference<typename std::remove_pointer<MutexArg>::type>::type>;
246-
247246
// When locking a Mutex, require negative capability to ensure the lock
248247
// is not already held
249248
inline Mutex& MaybeCheckNotHeld(Mutex& cs) EXCLUSIVE_LOCKS_REQUIRED(!cs) LOCK_RETURNED(cs) { return cs; }
250249
inline Mutex* MaybeCheckNotHeld(Mutex* cs) EXCLUSIVE_LOCKS_REQUIRED(!cs) LOCK_RETURNED(cs) { return cs; }
251250

252-
// When locking a GlobalMutex, just check it is not locked in the surrounding scope
253-
inline GlobalMutex& MaybeCheckNotHeld(GlobalMutex& cs) LOCKS_EXCLUDED(cs) LOCK_RETURNED(cs) { return cs; }
254-
inline GlobalMutex* MaybeCheckNotHeld(GlobalMutex* cs) LOCKS_EXCLUDED(cs) LOCK_RETURNED(cs) { return cs; }
255-
256-
// When locking a RecursiveMutex, it's okay to already hold the lock
257-
// but check that it is not known to be locked in the surrounding scope anyway
258-
inline RecursiveMutex& MaybeCheckNotHeld(RecursiveMutex& cs) LOCKS_EXCLUDED(cs) LOCK_RETURNED(cs) { return cs; }
259-
inline RecursiveMutex* MaybeCheckNotHeld(RecursiveMutex* cs) LOCKS_EXCLUDED(cs) LOCK_RETURNED(cs) { return cs; }
251+
// When locking a GlobalMutex or RecursiveMutex, just check it is not
252+
// locked in the surrounding scope.
253+
template <typename MutexType>
254+
inline MutexType& MaybeCheckNotHeld(MutexType& m) LOCKS_EXCLUDED(m) LOCK_RETURNED(m) { return m; }
255+
template <typename MutexType>
256+
inline MutexType* MaybeCheckNotHeld(MutexType* m) LOCKS_EXCLUDED(m) LOCK_RETURNED(m) { return m; }
260257

261-
#define LOCK(cs) DebugLock<decltype(cs)> UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
258+
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
262259
#define LOCK2(cs1, cs2) \
263-
DebugLock<decltype(cs1)> criticalblock1(MaybeCheckNotHeld(cs1), #cs1, __FILE__, __LINE__); \
264-
DebugLock<decltype(cs2)> criticalblock2(MaybeCheckNotHeld(cs2), #cs2, __FILE__, __LINE__)
265-
#define TRY_LOCK(cs, name) DebugLock<decltype(cs)> name(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__, true)
266-
#define WAIT_LOCK(cs, name) DebugLock<decltype(cs)> name(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
260+
UniqueLock criticalblock1(MaybeCheckNotHeld(cs1), #cs1, __FILE__, __LINE__); \
261+
UniqueLock criticalblock2(MaybeCheckNotHeld(cs2), #cs2, __FILE__, __LINE__)
262+
#define TRY_LOCK(cs, name) UniqueLock name(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__, true)
263+
#define WAIT_LOCK(cs, name) UniqueLock name(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
267264

268265
#define ENTER_CRITICAL_SECTION(cs) \
269266
{ \

0 commit comments

Comments
 (0)