From bba9bd0d9dd06f13a6b0c89181864453cab5c858 Mon Sep 17 00:00:00 2001 From: Thomas Snider Date: Sat, 18 Nov 2017 11:35:14 -0800 Subject: [PATCH 1/2] Switched sync.{cpp,h} to std threading primitives. --- src/sync.cpp | 17 ++++++++--------- src/sync.h | 16 ++++++---------- src/util.h | 1 + 3 files changed, 15 insertions(+), 19 deletions(-) diff --git a/src/sync.cpp b/src/sync.cpp index fcc6ddc35..116533eb4 100644 --- a/src/sync.cpp +++ b/src/sync.cpp @@ -4,13 +4,12 @@ #include +#include #include #include #include -#include - #ifdef DEBUG_LOCKCONTENTION void PrintLockContention(const char* pszName, const char* pszFile, int nLine) { @@ -45,8 +44,8 @@ struct CLockLocation { return mutexName + " " + sourceFile + ":" + itostr(sourceLine) + (fTry ? " (TRY)" : ""); } - bool fTry; private: + bool fTry; std::string mutexName; std::string sourceFile; int sourceLine; @@ -67,10 +66,10 @@ struct LockData { LockOrders lockorders; InvLockOrders invlockorders; - boost::mutex dd_mutex; + std::mutex dd_mutex; } static lockdata; -boost::thread_specific_ptr lockstack; +static thread_local std::unique_ptr lockstack; static void potential_deadlock_detected(const std::pair& mismatch, const LockStack& s1, const LockStack& s2) { @@ -100,12 +99,12 @@ static void potential_deadlock_detected(const std::pair& mismatch, static void push_lock(void* c, const CLockLocation& locklocation) { - if (lockstack.get() == nullptr) + if (!lockstack) lockstack.reset(new LockStack); - boost::unique_lock lock(lockdata.dd_mutex); + std::lock_guard lock(lockdata.dd_mutex); - (*lockstack).push_back(std::make_pair(c, locklocation)); + lockstack->push_back(std::make_pair(c, locklocation)); for (const std::pair & i : (*lockstack)) { if (i.first == c) @@ -171,7 +170,7 @@ void DeleteLock(void* cs) // We're already shutting down. return; } - boost::unique_lock lock(lockdata.dd_mutex); + std::lock_guard lock(lockdata.dd_mutex); std::pair item = std::make_pair(cs, nullptr); LockOrders::iterator it = lockdata.lockorders.lower_bound(item); while (it != lockdata.lockorders.end() && it->first.first == cs) { diff --git a/src/sync.h b/src/sync.h index b0889be76..3af27c65d 100644 --- a/src/sync.h +++ b/src/sync.h @@ -8,8 +8,6 @@ #include -#include -#include #include #include #include @@ -196,8 +194,8 @@ public: class CSemaphore { private: - boost::condition_variable condition; - boost::mutex mutex; + std::condition_variable condition; + std::mutex mutex; int value; public: @@ -205,16 +203,14 @@ public: void wait() { - boost::unique_lock lock(mutex); - while (value < 1) { - condition.wait(lock); - } + std::unique_lock lock(mutex); + condition.wait(lock, [&]() { return value >= 1; }); value--; } bool try_wait() { - boost::unique_lock lock(mutex); + std::lock_guard lock(mutex); if (value < 1) return false; value--; @@ -224,7 +220,7 @@ public: void post() { { - boost::unique_lock lock(mutex); + std::lock_guard lock(mutex); value++; } condition.notify_one(); diff --git a/src/util.h b/src/util.h index be5c99567..08de43d29 100644 --- a/src/util.h +++ b/src/util.h @@ -28,6 +28,7 @@ #include #include +#include // for boost::thread_interrupted // Application startup time (used for uptime calculation) int64_t GetStartupTime(); From f7f7e2cd340c088e82d09090eb275b98b34a9812 Mon Sep 17 00:00:00 2001 From: Cory Fields Date: Mon, 27 Nov 2017 17:08:27 -0500 Subject: [PATCH 2/2] threads: add a thread_local autoconf check --- configure.ac | 22 ++++++++++++++++++++++ src/sync.cpp | 3 +++ 2 files changed, 25 insertions(+) diff --git a/configure.ac b/configure.ac index 8e5561243..a05896ede 100644 --- a/configure.ac +++ b/configure.ac @@ -659,6 +659,28 @@ AC_LINK_IFELSE([AC_LANG_SOURCE([ ] ) +TEMP_LDFLAGS="$LDFLAGS" +LDFLAGS="$TEMP_LDFLAGS $PTHREAD_CFLAGS" +AC_MSG_CHECKING([for thread_local support]) +AC_LINK_IFELSE([AC_LANG_SOURCE([ + #include + static thread_local int foo = 0; + static void run_thread() { foo++;} + int main(){ + for(int i = 0; i < 10; i++) { std::thread(run_thread).detach();} + return foo; + } + ])], + [ + AC_DEFINE(HAVE_THREAD_LOCAL,1,[Define if thread_local is supported.]) + AC_MSG_RESULT(yes) + ], + [ + AC_MSG_RESULT(no) + ] +) +LDFLAGS="$TEMP_LDFLAGS" + # Check for different ways of gathering OS randomness AC_MSG_CHECKING(for Linux getrandom syscall) AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include diff --git a/src/sync.cpp b/src/sync.cpp index 116533eb4..3f51383ea 100644 --- a/src/sync.cpp +++ b/src/sync.cpp @@ -11,6 +11,9 @@ #include #ifdef DEBUG_LOCKCONTENTION +#if !defined(HAVE_THREAD_LOCAL) +static_assert(false, "thread_local is not supported"); +#endif void PrintLockContention(const char* pszName, const char* pszFile, int nLine) { LogPrintf("LOCKCONTENTION: %s\n", pszName);