Skip to content

Commit

Permalink
Switched sync.{cpp,h} to std threading primitives.
Browse files Browse the repository at this point in the history
(cherry picked from commit bitcoin/bitcoin@bba9bd0)
  • Loading branch information
tjps authored and str4d committed May 14, 2022
1 parent eeba670 commit 4ad38a4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 20 deletions.
17 changes: 8 additions & 9 deletions src/sync.cpp
Expand Up @@ -5,13 +5,12 @@

#include "sync.h"

#include <set>
#include "util.h"
#include "utilstrencodings.h"

#include <stdio.h>

#include <boost/thread.hpp>

#ifdef DEBUG_LOCKCONTENTION
void PrintLockContention(const char* pszName, const char* pszFile, int nLine)
{
Expand Down Expand Up @@ -46,8 +45,8 @@ struct CLockLocation {
return mutexName + " " + sourceFile + ":" + itostr(sourceLine) + (fTry ? " (TRY)" : "");
}

bool fTry;
private:
bool fTry;
std::string mutexName;
std::string sourceFile;
int sourceLine;
Expand All @@ -68,14 +67,14 @@ struct LockData {

LockOrders lockorders;
InvLockOrders invlockorders;
boost::mutex dd_mutex;
std::mutex dd_mutex;
};
LockData& GetLockData() {
static LockData lockdata;
return 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)
{
Expand Down Expand Up @@ -105,13 +104,13 @@ static void potential_deadlock_detected(const std::pair<void*, void*>& mismatch,

static void push_lock(void* c, const CLockLocation& locklocation)
{
if (lockstack.get() == NULL)
if (!lockstack)
lockstack.reset(new LockStack);

LockData& lockdata = GetLockData();
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)) {
if (i.first == c)
Expand Down Expand Up @@ -178,7 +177,7 @@ void DeleteLock(void* cs)
// We're already shutting down.
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, (void*)0);
LockOrders::iterator it = lockdata.lockorders.lower_bound(item);
while (it != lockdata.lockorders.end() && it->first.first == cs) {
Expand Down
17 changes: 6 additions & 11 deletions src/sync.h
Expand Up @@ -9,9 +9,6 @@

#include "threadsafety.h"

#include <boost/thread/condition_variable.hpp>
#include <boost/thread/locks.hpp>
#include <boost/thread/mutex.hpp>
#include <condition_variable>
#include <thread>
#include <mutex>
Expand Down Expand Up @@ -198,25 +195,23 @@ class SCOPED_LOCKABLE CCriticalBlock
class CSemaphore
{
private:
boost::condition_variable condition;
boost::mutex mutex;
std::condition_variable condition;
std::mutex mutex;
int value;

public:
CSemaphore(int init) : value(init) {}

void wait()
{
boost::unique_lock<boost::mutex> lock(mutex);
while (value < 1) {
condition.wait(lock);
}
std::unique_lock<std::mutex> lock(mutex);
condition.wait(lock, [&]() { return value >= 1; });
value--;
}

bool try_wait()
{
boost::unique_lock<boost::mutex> lock(mutex);
std::lock_guard<std::mutex> lock(mutex);
if (value < 1)
return false;
value--;
Expand All @@ -226,7 +221,7 @@ class CSemaphore
void post()
{
{
boost::unique_lock<boost::mutex> lock(mutex);
std::lock_guard<std::mutex> lock(mutex);
value++;
}
condition.notify_one();
Expand Down
1 change: 1 addition & 0 deletions src/util.h
Expand Up @@ -28,6 +28,7 @@
#include <string>
#include <vector>

#include <boost/thread/condition_variable.hpp> // for boost::thread_interrupted
#include <boost/thread/exceptions.hpp>

extern std::map<std::string, std::string> mapArgs;
Expand Down

0 comments on commit 4ad38a4

Please sign in to comment.