From 9c4dc597ddc66acfd58a945a5ab11f833731abba Mon Sep 17 00:00:00 2001
From: Russell Yanofsky <russ@yanofsky.org>
Date: Wed, 8 Nov 2017 17:07:40 -0500
Subject: [PATCH] Use LOCK macros for non-recursive locks

Instead of std::unique_lock.
---
 src/httpserver.cpp      | 8 ++++----
 src/init.cpp            | 4 ++--
 src/net.cpp             | 4 ++--
 src/net.h               | 2 +-
 src/random.cpp          | 7 ++++---
 src/rpc/blockchain.cpp  | 8 ++++----
 src/rpc/mining.cpp      | 2 +-
 src/sync.h              | 3 ---
 src/threadinterrupt.cpp | 6 ++++--
 src/threadinterrupt.h   | 4 +++-
 src/validation.cpp      | 2 +-
 11 files changed, 26 insertions(+), 24 deletions(-)

diff --git a/src/httpserver.cpp b/src/httpserver.cpp
index 9daf3d196..ceddc7c2c 100644
--- a/src/httpserver.cpp
+++ b/src/httpserver.cpp
@@ -69,7 +69,7 @@ class WorkQueue
 {
 private:
     /** Mutex protects entire object */
-    std::mutex cs;
+    CWaitableCriticalSection cs;
     std::condition_variable cond;
     std::deque<std::unique_ptr<WorkItem>> queue;
     bool running;
@@ -88,7 +88,7 @@ public:
     /** Enqueue a work item */
     bool Enqueue(WorkItem* item)
     {
-        std::unique_lock<std::mutex> lock(cs);
+        LOCK(cs);
         if (queue.size() >= maxDepth) {
             return false;
         }
@@ -102,7 +102,7 @@ public:
         while (true) {
             std::unique_ptr<WorkItem> i;
             {
-                std::unique_lock<std::mutex> lock(cs);
+                WAIT_LOCK(cs, lock);
                 while (running && queue.empty())
                     cond.wait(lock);
                 if (!running)
@@ -116,7 +116,7 @@ public:
     /** Interrupt and exit loops */
     void Interrupt()
     {
-        std::unique_lock<std::mutex> lock(cs);
+        LOCK(cs);
         running = false;
         cond.notify_all();
     }
diff --git a/src/init.cpp b/src/init.cpp
index 21445929d..8ab5790ae 100644
--- a/src/init.cpp
+++ b/src/init.cpp
@@ -566,7 +566,7 @@ static void BlockNotifyGenesisWait(bool, const CBlockIndex *pBlockIndex)
 {
     if (pBlockIndex != nullptr) {
         {
-            WaitableLock lock_GenesisWait(cs_GenesisWait);
+            LOCK(cs_GenesisWait);
             fHaveGenesis = true;
         }
         condvar_GenesisWait.notify_all();
@@ -1660,7 +1660,7 @@ bool AppInitMain()
 
     // Wait for genesis block to be processed
     {
-        WaitableLock lock(cs_GenesisWait);
+        WAIT_LOCK(cs_GenesisWait, lock);
         // We previously could hang here if StartShutdown() is called prior to
         // ThreadImport getting started, so instead we just wait on a timer to
         // check ShutdownRequested() regularly.
diff --git a/src/net.cpp b/src/net.cpp
index 5be91fd4f..37b452a71 100644
--- a/src/net.cpp
+++ b/src/net.cpp
@@ -2064,7 +2064,7 @@ void CConnman::ThreadMessageHandler()
                 pnode->Release();
         }
 
-        std::unique_lock<std::mutex> lock(mutexMsgProc);
+        WAIT_LOCK(mutexMsgProc, lock);
         if (!fMoreWork) {
             condMsgProc.wait_until(lock, std::chrono::steady_clock::now() + std::chrono::milliseconds(100), [this] { return fMsgProcWake; });
         }
@@ -2346,7 +2346,7 @@ bool CConnman::Start(CScheduler& scheduler, const Options& connOptions)
     flagInterruptMsgProc = false;
 
     {
-        std::unique_lock<std::mutex> lock(mutexMsgProc);
+        LOCK(mutexMsgProc);
         fMsgProcWake = false;
     }
 
diff --git a/src/net.h b/src/net.h
index 36c2a4b8f..5c47f9695 100644
--- a/src/net.h
+++ b/src/net.h
@@ -425,7 +425,7 @@ private:
     bool fMsgProcWake;
 
     std::condition_variable condMsgProc;
-    std::mutex mutexMsgProc;
+    CWaitableCriticalSection mutexMsgProc;
     std::atomic<bool> flagInterruptMsgProc;
 
     CThreadInterrupt interruptNet;
diff --git a/src/random.cpp b/src/random.cpp
index fee6c2d92..7f05bcf9a 100644
--- a/src/random.cpp
+++ b/src/random.cpp
@@ -12,6 +12,7 @@
 #include <wincrypt.h>
 #endif
 #include <logging.h>  // for LogPrint()
+#include <sync.h>     // for WAIT_LOCK
 #include <utiltime.h> // for GetTime()
 
 #include <stdlib.h>
@@ -295,7 +296,7 @@ void RandAddSeedSleep()
 }
 
 
-static std::mutex cs_rng_state;
+static CWaitableCriticalSection cs_rng_state;
 static unsigned char rng_state[32] = {0};
 static uint64_t rng_counter = 0;
 
@@ -305,7 +306,7 @@ static void AddDataToRng(void* data, size_t len) {
     hasher.Write((const unsigned char*)data, len);
     unsigned char buf[64];
     {
-        std::unique_lock<std::mutex> lock(cs_rng_state);
+        WAIT_LOCK(cs_rng_state, lock);
         hasher.Write(rng_state, sizeof(rng_state));
         hasher.Write((const unsigned char*)&rng_counter, sizeof(rng_counter));
         ++rng_counter;
@@ -337,7 +338,7 @@ void GetStrongRandBytes(unsigned char* out, int num)
 
     // Combine with and update state
     {
-        std::unique_lock<std::mutex> lock(cs_rng_state);
+        WAIT_LOCK(cs_rng_state, lock);
         hasher.Write(rng_state, sizeof(rng_state));
         hasher.Write((const unsigned char*)&rng_counter, sizeof(rng_counter));
         ++rng_counter;
diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp
index 46dec4ca6..3aa01914a 100644
--- a/src/rpc/blockchain.cpp
+++ b/src/rpc/blockchain.cpp
@@ -49,7 +49,7 @@ struct CUpdatedBlock
     int height;
 };
 
-static std::mutex cs_blockchange;
+static CWaitableCriticalSection cs_blockchange;
 static std::condition_variable cond_blockchange;
 static CUpdatedBlock latestblock;
 
@@ -224,7 +224,7 @@ static UniValue waitfornewblock(const JSONRPCRequest& request)
 
     CUpdatedBlock block;
     {
-        std::unique_lock<std::mutex> lock(cs_blockchange);
+        WAIT_LOCK(cs_blockchange, lock);
         block = latestblock;
         if(timeout)
             cond_blockchange.wait_for(lock, std::chrono::milliseconds(timeout), [&block]{return latestblock.height != block.height || latestblock.hash != block.hash || !IsRPCRunning(); });
@@ -266,7 +266,7 @@ static UniValue waitforblock(const JSONRPCRequest& request)
 
     CUpdatedBlock block;
     {
-        std::unique_lock<std::mutex> lock(cs_blockchange);
+        WAIT_LOCK(cs_blockchange, lock);
         if(timeout)
             cond_blockchange.wait_for(lock, std::chrono::milliseconds(timeout), [&hash]{return latestblock.hash == hash || !IsRPCRunning();});
         else
@@ -309,7 +309,7 @@ static UniValue waitforblockheight(const JSONRPCRequest& request)
 
     CUpdatedBlock block;
     {
-        std::unique_lock<std::mutex> lock(cs_blockchange);
+        WAIT_LOCK(cs_blockchange, lock);
         if(timeout)
             cond_blockchange.wait_for(lock, std::chrono::milliseconds(timeout), [&height]{return latestblock.height >= height || !IsRPCRunning();});
         else
diff --git a/src/rpc/mining.cpp b/src/rpc/mining.cpp
index e751587dc..59bf0c98a 100644
--- a/src/rpc/mining.cpp
+++ b/src/rpc/mining.cpp
@@ -470,7 +470,7 @@ static UniValue getblocktemplate(const JSONRPCRequest& request)
         {
             checktxtime = std::chrono::steady_clock::now() + std::chrono::minutes(1);
 
-            WaitableLock lock(g_best_block_mutex);
+            WAIT_LOCK(g_best_block_mutex, lock);
             while (g_best_block == hashWatchedChain && IsRPCRunning())
             {
                 if (g_best_block_cv.wait_until(lock, checktxtime) == std::cv_status::timeout)
diff --git a/src/sync.h b/src/sync.h
index 339a7e2c1..7306e7285 100644
--- a/src/sync.h
+++ b/src/sync.h
@@ -112,9 +112,6 @@ typedef AnnotatedMixin<std::mutex> CWaitableCriticalSection;
 /** Just a typedef for std::condition_variable, can be wrapped later if desired */
 typedef std::condition_variable CConditionVariable;
 
-/** Just a typedef for std::unique_lock, can be wrapped later if desired */
-typedef std::unique_lock<std::mutex> WaitableLock;
-
 #ifdef DEBUG_LOCKCONTENTION
 void PrintLockContention(const char* pszName, const char* pszFile, int nLine);
 #endif
diff --git a/src/threadinterrupt.cpp b/src/threadinterrupt.cpp
index 7da4e136e..1efc6f311 100644
--- a/src/threadinterrupt.cpp
+++ b/src/threadinterrupt.cpp
@@ -5,6 +5,8 @@
 
 #include <threadinterrupt.h>
 
+#include <sync.h>
+
 CThreadInterrupt::CThreadInterrupt() : flag(false) {}
 
 CThreadInterrupt::operator bool() const
@@ -20,7 +22,7 @@ void CThreadInterrupt::reset()
 void CThreadInterrupt::operator()()
 {
     {
-        std::unique_lock<std::mutex> lock(mut);
+        LOCK(mut);
         flag.store(true, std::memory_order_release);
     }
     cond.notify_all();
@@ -28,7 +30,7 @@ void CThreadInterrupt::operator()()
 
 bool CThreadInterrupt::sleep_for(std::chrono::milliseconds rel_time)
 {
-    std::unique_lock<std::mutex> lock(mut);
+    WAIT_LOCK(mut, lock);
     return !cond.wait_for(lock, rel_time, [this]() { return flag.load(std::memory_order_acquire); });
 }
 
diff --git a/src/threadinterrupt.h b/src/threadinterrupt.h
index d373e3c37..8ba6b1236 100644
--- a/src/threadinterrupt.h
+++ b/src/threadinterrupt.h
@@ -5,6 +5,8 @@
 #ifndef BITCOIN_THREADINTERRUPT_H
 #define BITCOIN_THREADINTERRUPT_H
 
+#include <sync.h>
+
 #include <atomic>
 #include <chrono>
 #include <condition_variable>
@@ -28,7 +30,7 @@ public:
 
 private:
     std::condition_variable cond;
-    std::mutex mut;
+    CWaitableCriticalSection mut;
     std::atomic<bool> flag;
 };
 
diff --git a/src/validation.cpp b/src/validation.cpp
index 702a8d7e0..2d6ac4b23 100644
--- a/src/validation.cpp
+++ b/src/validation.cpp
@@ -2254,7 +2254,7 @@ void static UpdateTip(const CBlockIndex *pindexNew, const CChainParams& chainPar
     mempool.AddTransactionsUpdated(1);
 
     {
-        WaitableLock lock(g_best_block_mutex);
+        LOCK(g_best_block_mutex);
         g_best_block = pindexNew->GetBlockHash();
         g_best_block_cv.notify_all();
     }