Browse Source

Merge #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
0.16
Wladimir J. van der Laan 7 years ago
parent
commit
26efc220a1
No known key found for this signature in database
GPG Key ID: 1E4AED62986CD25D
  1. 22
      configure.ac
  2. 20
      src/sync.cpp
  3. 16
      src/sync.h
  4. 1
      src/util.h

22
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 <thread>
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 # Check for different ways of gathering OS randomness
AC_MSG_CHECKING(for Linux getrandom syscall) AC_MSG_CHECKING(for Linux getrandom syscall)
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <unistd.h> AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <unistd.h>

20
src/sync.cpp

@ -4,14 +4,16 @@
#include <sync.h> #include <sync.h>
#include <set>
#include <util.h> #include <util.h>
#include <utilstrencodings.h> #include <utilstrencodings.h>
#include <stdio.h> #include <stdio.h>
#include <boost/thread.hpp>
#ifdef DEBUG_LOCKCONTENTION #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) void PrintLockContention(const char* pszName, const char* pszFile, int nLine)
{ {
LogPrintf("LOCKCONTENTION: %s\n", pszName); LogPrintf("LOCKCONTENTION: %s\n", pszName);
@ -45,8 +47,8 @@ struct CLockLocation {
return mutexName + " " + sourceFile + ":" + itostr(sourceLine) + (fTry ? " (TRY)" : ""); return mutexName + " " + sourceFile + ":" + itostr(sourceLine) + (fTry ? " (TRY)" : "");
} }
bool fTry;
private: private:
bool fTry;
std::string mutexName; std::string mutexName;
std::string sourceFile; std::string sourceFile;
int sourceLine; int sourceLine;
@ -67,10 +69,10 @@ struct LockData {
LockOrders lockorders; LockOrders lockorders;
InvLockOrders invlockorders; InvLockOrders invlockorders;
boost::mutex dd_mutex; std::mutex dd_mutex;
} static lockdata; } static lockdata;
boost::thread_specific_ptr<LockStack> lockstack; static thread_local std::unique_ptr<LockStack> lockstack;
static void potential_deadlock_detected(const std::pair<void*, void*>& mismatch, const LockStack& s1, const LockStack& s2) static void potential_deadlock_detected(const std::pair<void*, void*>& mismatch, const LockStack& s1, const LockStack& s2)
{ {
@ -100,12 +102,12 @@ static void potential_deadlock_detected(const std::pair<void*, void*>& mismatch,
static void push_lock(void* c, const CLockLocation& locklocation) static void push_lock(void* c, const CLockLocation& locklocation)
{ {
if (lockstack.get() == nullptr) if (!lockstack)
lockstack.reset(new LockStack); lockstack.reset(new LockStack);
boost::unique_lock<boost::mutex> lock(lockdata.dd_mutex); std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
(*lockstack).push_back(std::make_pair(c, locklocation)); lockstack->push_back(std::make_pair(c, locklocation));
for (const std::pair<void*, CLockLocation> & i : (*lockstack)) { for (const std::pair<void*, CLockLocation> & i : (*lockstack)) {
if (i.first == c) if (i.first == c)
@ -171,7 +173,7 @@ void DeleteLock(void* cs)
// We're already shutting down. // We're already shutting down.
return; return;
} }
boost::unique_lock<boost::mutex> lock(lockdata.dd_mutex); std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
std::pair<void*, void*> item = std::make_pair(cs, nullptr); std::pair<void*, void*> item = std::make_pair(cs, nullptr);
LockOrders::iterator it = lockdata.lockorders.lower_bound(item); LockOrders::iterator it = lockdata.lockorders.lower_bound(item);
while (it != lockdata.lockorders.end() && it->first.first == cs) { while (it != lockdata.lockorders.end() && it->first.first == cs) {

16
src/sync.h

@ -8,8 +8,6 @@
#include <threadsafety.h> #include <threadsafety.h>
#include <boost/thread/condition_variable.hpp>
#include <boost/thread/mutex.hpp>
#include <condition_variable> #include <condition_variable>
#include <thread> #include <thread>
#include <mutex> #include <mutex>
@ -196,8 +194,8 @@ public:
class CSemaphore class CSemaphore
{ {
private: private:
boost::condition_variable condition; std::condition_variable condition;
boost::mutex mutex; std::mutex mutex;
int value; int value;
public: public:
@ -205,16 +203,14 @@ public:
void wait() void wait()
{ {
boost::unique_lock<boost::mutex> lock(mutex); std::unique_lock<std::mutex> lock(mutex);
while (value < 1) { condition.wait(lock, [&]() { return value >= 1; });
condition.wait(lock);
}
value--; value--;
} }
bool try_wait() bool try_wait()
{ {
boost::unique_lock<boost::mutex> lock(mutex); std::lock_guard<std::mutex> lock(mutex);
if (value < 1) if (value < 1)
return false; return false;
value--; value--;
@ -224,7 +220,7 @@ public:
void post() void post()
{ {
{ {
boost::unique_lock<boost::mutex> lock(mutex); std::lock_guard<std::mutex> lock(mutex);
value++; value++;
} }
condition.notify_one(); condition.notify_one();

1
src/util.h

@ -28,6 +28,7 @@
#include <vector> #include <vector>
#include <boost/signals2/signal.hpp> #include <boost/signals2/signal.hpp>
#include <boost/thread/condition_variable.hpp> // for boost::thread_interrupted
// Application startup time (used for uptime calculation) // Application startup time (used for uptime calculation)
int64_t GetStartupTime(); int64_t GetStartupTime();

Loading…
Cancel
Save