Skip to content

Commit 190bf62

Browse files
committed
scripted-diff: Small locking rename
Call sync.h primitives "locks" and "mutexes" instead of "blocks" and "waitable critical sections" to match current coding conventions and c++11 standard names. This PR does not rename the "CCriticalSection" class (though this could be done as a followup) because it is used everywhere and would swamp the other changes in this PR. Plain mutexes should mostly be preferred instead of recursive mutexes in new code anyway. -BEGIN VERIFY SCRIPT- set -x set -e ren() { git grep -l $1 | xargs sed -i s/$1/$2/; } ren CCriticalBlock UniqueLock ren CWaitableCriticalSection Mutex ren CConditionVariable std::condition_variable ren cs_GenesisWait g_genesis_wait_mutex ren condvar_GenesisWait g_genesis_wait_cv perl -0777 -pi -e 's/.*typedef.*condition_variable.*\n\n?//g' src/sync.h -END VERIFY SCRIPT-
1 parent 385ad11 commit 190bf62

File tree

10 files changed

+22
-25
lines changed

10 files changed

+22
-25
lines changed

src/httpserver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class WorkQueue
6969
{
7070
private:
7171
/** Mutex protects entire object */
72-
CWaitableCriticalSection cs;
72+
Mutex cs;
7373
std::condition_variable cond;
7474
std::deque<std::unique_ptr<WorkItem>> queue;
7575
bool running;

src/init.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -561,17 +561,17 @@ static void BlockNotifyCallback(bool initialSync, const CBlockIndex *pBlockIndex
561561
}
562562

563563
static bool fHaveGenesis = false;
564-
static CWaitableCriticalSection cs_GenesisWait;
565-
static CConditionVariable condvar_GenesisWait;
564+
static Mutex g_genesis_wait_mutex;
565+
static std::condition_variable g_genesis_wait_cv;
566566

567567
static void BlockNotifyGenesisWait(bool, const CBlockIndex *pBlockIndex)
568568
{
569569
if (pBlockIndex != nullptr) {
570570
{
571-
LOCK(cs_GenesisWait);
571+
LOCK(g_genesis_wait_mutex);
572572
fHaveGenesis = true;
573573
}
574-
condvar_GenesisWait.notify_all();
574+
g_genesis_wait_cv.notify_all();
575575
}
576576
}
577577

@@ -1661,12 +1661,12 @@ bool AppInitMain()
16611661

16621662
// Wait for genesis block to be processed
16631663
{
1664-
WAIT_LOCK(cs_GenesisWait, lock);
1664+
WAIT_LOCK(g_genesis_wait_mutex, lock);
16651665
// We previously could hang here if StartShutdown() is called prior to
16661666
// ThreadImport getting started, so instead we just wait on a timer to
16671667
// check ShutdownRequested() regularly.
16681668
while (!fHaveGenesis && !ShutdownRequested()) {
1669-
condvar_GenesisWait.wait_for(lock, std::chrono::milliseconds(500));
1669+
g_genesis_wait_cv.wait_for(lock, std::chrono::milliseconds(500));
16701670
}
16711671
uiInterface.NotifyBlockTip_disconnect(BlockNotifyGenesisWait);
16721672
}

src/net.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ class CConnman
427427
bool fMsgProcWake;
428428

429429
std::condition_variable condMsgProc;
430-
CWaitableCriticalSection mutexMsgProc;
430+
Mutex mutexMsgProc;
431431
std::atomic<bool> flagInterruptMsgProc;
432432

433433
CThreadInterrupt interruptNet;

src/random.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ void RandAddSeedSleep()
296296
}
297297

298298

299-
static CWaitableCriticalSection cs_rng_state;
299+
static Mutex cs_rng_state;
300300
static unsigned char rng_state[32] = {0};
301301
static uint64_t rng_counter = 0;
302302

src/rpc/blockchain.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ struct CUpdatedBlock
5050
int height;
5151
};
5252

53-
static CWaitableCriticalSection cs_blockchange;
53+
static Mutex cs_blockchange;
5454
static std::condition_variable cond_blockchange;
5555
static CUpdatedBlock latestblock;
5656

src/sync.h

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,18 +107,15 @@ class LOCKABLE AnnotatedMixin : public PARENT
107107
typedef AnnotatedMixin<std::recursive_mutex> CCriticalSection;
108108

109109
/** Wrapped mutex: supports waiting but not recursive locking */
110-
typedef AnnotatedMixin<std::mutex> CWaitableCriticalSection;
111-
112-
/** Just a typedef for std::condition_variable, can be wrapped later if desired */
113-
typedef std::condition_variable CConditionVariable;
110+
typedef AnnotatedMixin<std::mutex> Mutex;
114111

115112
#ifdef DEBUG_LOCKCONTENTION
116113
void PrintLockContention(const char* pszName, const char* pszFile, int nLine);
117114
#endif
118115

119116
/** Wrapper around std::unique_lock style lock for Mutex. */
120117
template <typename Mutex, typename Base = typename Mutex::UniqueLock>
121-
class SCOPED_LOCKABLE CCriticalBlock : public Base
118+
class SCOPED_LOCKABLE UniqueLock : public Base
122119
{
123120
private:
124121
void Enter(const char* pszName, const char* pszFile, int nLine)
@@ -144,15 +141,15 @@ class SCOPED_LOCKABLE CCriticalBlock : public Base
144141
}
145142

146143
public:
147-
CCriticalBlock(Mutex& mutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) EXCLUSIVE_LOCK_FUNCTION(mutexIn) : Base(mutexIn, std::defer_lock)
144+
UniqueLock(Mutex& mutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) EXCLUSIVE_LOCK_FUNCTION(mutexIn) : Base(mutexIn, std::defer_lock)
148145
{
149146
if (fTry)
150147
TryEnter(pszName, pszFile, nLine);
151148
else
152149
Enter(pszName, pszFile, nLine);
153150
}
154151

155-
CCriticalBlock(Mutex* pmutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) EXCLUSIVE_LOCK_FUNCTION(pmutexIn)
152+
UniqueLock(Mutex* pmutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) EXCLUSIVE_LOCK_FUNCTION(pmutexIn)
156153
{
157154
if (!pmutexIn) return;
158155

@@ -163,7 +160,7 @@ class SCOPED_LOCKABLE CCriticalBlock : public Base
163160
Enter(pszName, pszFile, nLine);
164161
}
165162

166-
~CCriticalBlock() UNLOCK_FUNCTION()
163+
~UniqueLock() UNLOCK_FUNCTION()
167164
{
168165
if (Base::owns_lock())
169166
LeaveCritical();
@@ -176,7 +173,7 @@ class SCOPED_LOCKABLE CCriticalBlock : public Base
176173
};
177174

178175
template<typename MutexArg>
179-
using DebugLock = CCriticalBlock<typename std::remove_reference<typename std::remove_pointer<MutexArg>::type>::type>;
176+
using DebugLock = UniqueLock<typename std::remove_reference<typename std::remove_pointer<MutexArg>::type>::type>;
180177

181178
#define PASTE(x, y) x ## y
182179
#define PASTE2(x, y) PASTE(x, y)

src/test/sync_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ BOOST_AUTO_TEST_CASE(potential_deadlock_detected)
4141
CCriticalSection rmutex1, rmutex2;
4242
TestPotentialDeadLockDetected(rmutex1, rmutex2);
4343

44-
CWaitableCriticalSection mutex1, mutex2;
44+
Mutex mutex1, mutex2;
4545
TestPotentialDeadLockDetected(mutex1, mutex2);
4646

4747
#ifdef DEBUG_LOCKORDER

src/threadinterrupt.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class CThreadInterrupt
3030

3131
private:
3232
std::condition_variable cond;
33-
CWaitableCriticalSection mut;
33+
Mutex mut;
3434
std::atomic<bool> flag;
3535
};
3636

src/validation.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,8 @@ CCriticalSection cs_main;
217217
BlockMap& mapBlockIndex = g_chainstate.mapBlockIndex;
218218
CChain& chainActive = g_chainstate.chainActive;
219219
CBlockIndex *pindexBestHeader = nullptr;
220-
CWaitableCriticalSection g_best_block_mutex;
221-
CConditionVariable g_best_block_cv;
220+
Mutex g_best_block_mutex;
221+
std::condition_variable g_best_block_cv;
222222
uint256 g_best_block;
223223
int nScriptCheckThreads = 0;
224224
std::atomic_bool fImporting(false);

src/validation.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ extern BlockMap& mapBlockIndex;
151151
extern uint64_t nLastBlockTx;
152152
extern uint64_t nLastBlockWeight;
153153
extern const std::string strMessageMagic;
154-
extern CWaitableCriticalSection g_best_block_mutex;
155-
extern CConditionVariable g_best_block_cv;
154+
extern Mutex g_best_block_mutex;
155+
extern std::condition_variable g_best_block_cv;
156156
extern uint256 g_best_block;
157157
extern std::atomic_bool fImporting;
158158
extern std::atomic_bool fReindex;

0 commit comments

Comments
 (0)