Skip to content

Commit

Permalink
Fix Semaphore
Browse files Browse the repository at this point in the history
  • Loading branch information
nathansenn committed Sep 6, 2020
1 parent 21fb7ac commit ee425f8
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 25 deletions.
10 changes: 5 additions & 5 deletions src/leveldb/port/port_win.cc
Expand Up @@ -70,8 +70,8 @@ void Mutex::AssertHeld() {
CondVar::CondVar(Mutex* mu) :
waiting_(0),
mu_(mu),
sem1_(::CreateSemahelix(NULL, 0, 10000, NULL)),
sem2_(::CreateSemahelix(NULL, 0, 10000, NULL)) {
sem1_(::CreateSemaphore(NULL, 0, 10000, NULL)),
sem2_(::CreateSemaphore(NULL, 0, 10000, NULL)) {
assert(mu_);
}

Expand All @@ -91,7 +91,7 @@ void CondVar::Wait() {

// initiate handshake
::WaitForSingleObject(sem1_, INFINITE);
::ReleaseSemahelix(sem2_, 1, NULL);
::ReleaseSemaphore(sem2_, 1, NULL);
mu_->Lock();
}

Expand All @@ -101,15 +101,15 @@ void CondVar::Signal() {
--waiting_;

// finalize handshake
::ReleaseSemahelix(sem1_, 1, NULL);
::ReleaseSemaphore(sem1_, 1, NULL);
::WaitForSingleObject(sem2_, INFINITE);
}
wait_mtx_.Unlock();
}

void CondVar::SignalAll() {
wait_mtx_.Lock();
::ReleaseSemahelix(sem1_, waiting_, NULL);
::ReleaseSemaphore(sem1_, waiting_, NULL);
while(waiting_ > 0) {
--waiting_;
::WaitForSingleObject(sem2_, INFINITE);
Expand Down
2 changes: 1 addition & 1 deletion src/leveldb/port/port_win.h
Expand Up @@ -74,7 +74,7 @@ class Mutex {

// the Win32 API offers a dependable condition variable mechanism, but only starting with
// Windows 2008 and Vista
// no matter what we will implement our own condition variable with a semahelix
// no matter what we will implement our own condition variable with a semaphore
// implementation as described in a paper written by Andrew D. Birrell in 2003
class CondVar {
public:
Expand Down
16 changes: 8 additions & 8 deletions src/net.cpp
Expand Up @@ -112,7 +112,7 @@ CCriticalSection cs_vAddedNodes;
NodeId nLastNodeId = 0;
CCriticalSection cs_nLastNodeId;

static CSemahelix* semOutbound = NULL;
static CSemaphore* semOutbound = NULL;
boost::condition_variable messageHandlerCondition;

// Signals for message handling
Expand Down Expand Up @@ -1302,7 +1302,7 @@ void static ProcessOneShot()
vOneShots.pop_front();
}
CAddress addr;
CSemahelixGrant grant(*semOutbound, true);
CSemaphoreGrant grant(*semOutbound, true);
if (grant) {
if (!OpenNetworkConnection(addr, &grant, strDest.c_str(), true))
AddOneShot(strDest);
Expand Down Expand Up @@ -1333,7 +1333,7 @@ void ThreadOpenConnections()

MilliSleep(500);

CSemahelixGrant grant(*semOutbound);
CSemaphoreGrant grant(*semOutbound);
boost::this_thread::interruption_point();

// Add seed nodes if DNS seeds are all down (an infrastructure attack?).
Expand Down Expand Up @@ -1427,7 +1427,7 @@ void ThreadOpenAddedConnections()
}
for (string& strAddNode : lAddresses) {
CAddress addr;
CSemahelixGrant grant(*semOutbound);
CSemaphoreGrant grant(*semOutbound);
OpenNetworkConnection(addr, &grant, strAddNode.c_str());
MilliSleep(500);
}
Expand Down Expand Up @@ -1469,7 +1469,7 @@ void ThreadOpenAddedConnections()
}
}
for (vector<CService>& vserv : lservAddressesToAdd) {
CSemahelixGrant grant(*semOutbound);
CSemaphoreGrant grant(*semOutbound);
OpenNetworkConnection(CAddress(vserv[i % vserv.size()]), &grant);
MilliSleep(500);
}
Expand All @@ -1478,7 +1478,7 @@ void ThreadOpenAddedConnections()
}

// if successful, this moves the passed grant to the constructed node
bool OpenNetworkConnection(const CAddress& addrConnect, CSemahelixGrant* grantOutbound, const char* pszDest, bool fOneShot)
bool OpenNetworkConnection(const CAddress& addrConnect, CSemaphoreGrant* grantOutbound, const char* pszDest, bool fOneShot)
{
//
// Initiate outbound network connection
Expand Down Expand Up @@ -1728,9 +1728,9 @@ void StartNode(boost::thread_group& threadGroup, CScheduler& scheduler)
fAddressesInitialized = true;

if (semOutbound == NULL) {
// initialize semahelix
// initialize semaphore
int nMaxOutbound = min(MAX_OUTBOUND_CONNECTIONS, nMaxConnections);
semOutbound = new CSemahelix(nMaxOutbound);
semOutbound = new CSemaphore(nMaxOutbound);
}

if (pnodeLocalHost == NULL)
Expand Down
4 changes: 2 additions & 2 deletions src/net.h
Expand Up @@ -69,7 +69,7 @@ CNode* FindNode(const CSubNet& subNet);
CNode* FindNode(const std::string& addrName);
CNode* FindNode(const CService& ip);
CNode* ConnectNode(CAddress addrConnect, const char* pszDest = NULL, bool obfuScationMaster = false);
bool OpenNetworkConnection(const CAddress& addrConnect, CSemahelixGrant* grantOutbound = NULL, const char* strDest = NULL, bool fOneShot = false);
bool OpenNetworkConnection(const CAddress& addrConnect, CSemaphoreGrant* grantOutbound = NULL, const char* strDest = NULL, bool fOneShot = false);
void MapPort(bool fUseUPnP);
unsigned short GetListenPort();
bool BindListenPort(const CService& bindAddr, std::string& strError, bool fWhitelisted = false);
Expand Down Expand Up @@ -325,7 +325,7 @@ class CNode
// (even if it's relative to mixing e.g. for blinding) should NOT set this to 'true'.
// For such cases node should be released manually (preferably right after corresponding code).
bool fObfuScationMaster;
CSemahelixGrant grantOutbound;
CSemaphoreGrant grantOutbound;
CCriticalSection cs_filter;
CBloomFilter* pfilter;
int nRefCount;
Expand Down
18 changes: 9 additions & 9 deletions src/sync.h
Expand Up @@ -212,15 +212,15 @@ typedef CMutexLock<CCriticalSection> CCriticalBlock;
LeaveCritical(); \
}

class CSemahelix
class CSemaphore
{
private:
boost::condition_variable condition;
boost::mutex mutex;
int value;

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

void wait()
{
Expand Down Expand Up @@ -250,11 +250,11 @@ class CSemahelix
}
};

/** RAII-style semahelix lock */
class CSemahelixGrant
/** RAII-style semaphore lock */
class CSemaphoreGrant
{
private:
CSemahelix* sem;
CSemaphore* sem;
bool fHaveGrant;

public:
Expand All @@ -281,7 +281,7 @@ class CSemahelixGrant
return fHaveGrant;
}

void MoveTo(CSemahelixGrant& grant)
void MoveTo(CSemaphoreGrant& grant)
{
grant.Release();
grant.sem = sem;
Expand All @@ -290,17 +290,17 @@ class CSemahelixGrant
fHaveGrant = false;
}

CSemahelixGrant() : sem(NULL), fHaveGrant(false) {}
CSemaphoreGrant() : sem(NULL), fHaveGrant(false) {}

CSemahelixGrant(CSemahelix& sema, bool fTry = false) : sem(&sema), fHaveGrant(false)
CSemaphoreGrant(CSemaphore& sema, bool fTry = false) : sem(&sema), fHaveGrant(false)
{
if (fTry)
TryAcquire();
else
Acquire();
}

~CSemahelixGrant()
~CSemaphoreGrant()
{
Release();
}
Expand Down

0 comments on commit ee425f8

Please sign in to comment.