Skip to content

Commit 26efc22

Browse files
committed
Merge bitcoin#11722: Switched sync.{cpp,h} to std threading primitives.
f7f7e2c threads: add a thread_local autoconf check (Cory Fields) bba9bd0 Switched sync.{cpp,h} to std threading primitives. (Thomas Snider) Pull request description: Replaced boost threading primitives with the std equivalents. Tree-SHA512: 72d10f9e48bfcf1db87e4a88bc698ef98eba0b29fe904570391b34a6ea1ffad474b7f192e70e3588a30e448f70f244eb4ddc5f24412a0bde2b564e76274160a5
2 parents a892218 + f7f7e2c commit 26efc22

File tree

4 files changed

+40
-19
lines changed

4 files changed

+40
-19
lines changed

configure.ac

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,28 @@ AC_LINK_IFELSE([AC_LANG_SOURCE([
659659
]
660660
)
661661

662+
TEMP_LDFLAGS="$LDFLAGS"
663+
LDFLAGS="$TEMP_LDFLAGS $PTHREAD_CFLAGS"
664+
AC_MSG_CHECKING([for thread_local support])
665+
AC_LINK_IFELSE([AC_LANG_SOURCE([
666+
#include <thread>
667+
static thread_local int foo = 0;
668+
static void run_thread() { foo++;}
669+
int main(){
670+
for(int i = 0; i < 10; i++) { std::thread(run_thread).detach();}
671+
return foo;
672+
}
673+
])],
674+
[
675+
AC_DEFINE(HAVE_THREAD_LOCAL,1,[Define if thread_local is supported.])
676+
AC_MSG_RESULT(yes)
677+
],
678+
[
679+
AC_MSG_RESULT(no)
680+
]
681+
)
682+
LDFLAGS="$TEMP_LDFLAGS"
683+
662684
# Check for different ways of gathering OS randomness
663685
AC_MSG_CHECKING(for Linux getrandom syscall)
664686
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <unistd.h>

src/sync.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
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
14+
#if !defined(HAVE_THREAD_LOCAL)
15+
static_assert(false, "thread_local is not supported");
16+
#endif
1517
void PrintLockContention(const char* pszName, const char* pszFile, int nLine)
1618
{
1719
LogPrintf("LOCKCONTENTION: %s\n", pszName);
@@ -45,8 +47,8 @@ struct CLockLocation {
4547
return mutexName + " " + sourceFile + ":" + itostr(sourceLine) + (fTry ? " (TRY)" : "");
4648
}
4749

48-
bool fTry;
4950
private:
51+
bool fTry;
5052
std::string mutexName;
5153
std::string sourceFile;
5254
int sourceLine;
@@ -67,10 +69,10 @@ struct LockData {
6769

6870
LockOrders lockorders;
6971
InvLockOrders invlockorders;
70-
boost::mutex dd_mutex;
72+
std::mutex dd_mutex;
7173
} static lockdata;
7274

73-
boost::thread_specific_ptr<LockStack> lockstack;
75+
static thread_local std::unique_ptr<LockStack> lockstack;
7476

7577
static void potential_deadlock_detected(const std::pair<void*, void*>& mismatch, const LockStack& s1, const LockStack& s2)
7678
{
@@ -100,12 +102,12 @@ static void potential_deadlock_detected(const std::pair<void*, void*>& mismatch,
100102

101103
static void push_lock(void* c, const CLockLocation& locklocation)
102104
{
103-
if (lockstack.get() == nullptr)
105+
if (!lockstack)
104106
lockstack.reset(new LockStack);
105107

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

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

110112
for (const std::pair<void*, CLockLocation> & i : (*lockstack)) {
111113
if (i.first == c)
@@ -171,7 +173,7 @@ void DeleteLock(void* cs)
171173
// We're already shutting down.
172174
return;
173175
}
174-
boost::unique_lock<boost::mutex> lock(lockdata.dd_mutex);
176+
std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
175177
std::pair<void*, void*> item = std::make_pair(cs, nullptr);
176178
LockOrders::iterator it = lockdata.lockorders.lower_bound(item);
177179
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)