Skip to content

Commit bba9bd0

Browse files
committed
Switched sync.{cpp,h} to std threading primitives.
1 parent 99bc0b4 commit bba9bd0

File tree

3 files changed

+15
-19
lines changed

3 files changed

+15
-19
lines changed

src/sync.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@
44

55
#include <sync.h>
66

7+
#include <set>
78
#include <util.h>
89
#include <utilstrencodings.h>
910

1011
#include <stdio.h>
1112

12-
#include <boost/thread.hpp>
13-
1413
#ifdef DEBUG_LOCKCONTENTION
1514
void PrintLockContention(const char* pszName, const char* pszFile, int nLine)
1615
{
@@ -45,8 +44,8 @@ struct CLockLocation {
4544
return mutexName + " " + sourceFile + ":" + itostr(sourceLine) + (fTry ? " (TRY)" : "");
4645
}
4746

48-
bool fTry;
4947
private:
48+
bool fTry;
5049
std::string mutexName;
5150
std::string sourceFile;
5251
int sourceLine;
@@ -67,10 +66,10 @@ struct LockData {
6766

6867
LockOrders lockorders;
6968
InvLockOrders invlockorders;
70-
boost::mutex dd_mutex;
69+
std::mutex dd_mutex;
7170
} static lockdata;
7271

73-
boost::thread_specific_ptr<LockStack> lockstack;
72+
static thread_local std::unique_ptr<LockStack> lockstack;
7473

7574
static void potential_deadlock_detected(const std::pair<void*, void*>& mismatch, const LockStack& s1, const LockStack& s2)
7675
{
@@ -100,12 +99,12 @@ static void potential_deadlock_detected(const std::pair<void*, void*>& mismatch,
10099

101100
static void push_lock(void* c, const CLockLocation& locklocation)
102101
{
103-
if (lockstack.get() == nullptr)
102+
if (!lockstack)
104103
lockstack.reset(new LockStack);
105104

106-
boost::unique_lock<boost::mutex> lock(lockdata.dd_mutex);
105+
std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
107106

108-
(*lockstack).push_back(std::make_pair(c, locklocation));
107+
lockstack->push_back(std::make_pair(c, locklocation));
109108

110109
for (const std::pair<void*, CLockLocation> & i : (*lockstack)) {
111110
if (i.first == c)
@@ -171,7 +170,7 @@ void DeleteLock(void* cs)
171170
// We're already shutting down.
172171
return;
173172
}
174-
boost::unique_lock<boost::mutex> lock(lockdata.dd_mutex);
173+
std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
175174
std::pair<void*, void*> item = std::make_pair(cs, nullptr);
176175
LockOrders::iterator it = lockdata.lockorders.lower_bound(item);
177176
while (it != lockdata.lockorders.end() && it->first.first == cs) {

src/sync.h

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
#include <threadsafety.h>
1010

11-
#include <boost/thread/condition_variable.hpp>
12-
#include <boost/thread/mutex.hpp>
1311
#include <condition_variable>
1412
#include <thread>
1513
#include <mutex>
@@ -196,25 +194,23 @@ class SCOPED_LOCKABLE CCriticalBlock
196194
class CSemaphore
197195
{
198196
private:
199-
boost::condition_variable condition;
200-
boost::mutex mutex;
197+
std::condition_variable condition;
198+
std::mutex mutex;
201199
int value;
202200

203201
public:
204202
explicit CSemaphore(int init) : value(init) {}
205203

206204
void wait()
207205
{
208-
boost::unique_lock<boost::mutex> lock(mutex);
209-
while (value < 1) {
210-
condition.wait(lock);
211-
}
206+
std::unique_lock<std::mutex> lock(mutex);
207+
condition.wait(lock, [&]() { return value >= 1; });
212208
value--;
213209
}
214210

215211
bool try_wait()
216212
{
217-
boost::unique_lock<boost::mutex> lock(mutex);
213+
std::lock_guard<std::mutex> lock(mutex);
218214
if (value < 1)
219215
return false;
220216
value--;
@@ -224,7 +220,7 @@ class CSemaphore
224220
void post()
225221
{
226222
{
227-
boost::unique_lock<boost::mutex> lock(mutex);
223+
std::lock_guard<std::mutex> lock(mutex);
228224
value++;
229225
}
230226
condition.notify_one();

src/util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <vector>
2929

3030
#include <boost/signals2/signal.hpp>
31+
#include <boost/thread/condition_variable.hpp> // for boost::thread_interrupted
3132

3233
// Application startup time (used for uptime calculation)
3334
int64_t GetStartupTime();

0 commit comments

Comments
 (0)