mirror of
https://github.com/jwinterm/myriadcoin.git
synced 2026-11-14 09:14:04 +01:00
Myriadcoin 0.11 port
This commit is contained in:
+62
-43
@@ -1,6 +1,6 @@
|
||||
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
||||
// Copyright (c) 2009-2013 The Bitcoin developers
|
||||
// Distributed under the MIT/X11 software license, see the accompanying
|
||||
// Copyright (c) 2009-2013 The Bitcoin Core developers
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#ifndef BITCOIN_SYNC_H
|
||||
@@ -21,9 +21,6 @@
|
||||
////////////////////////////////////////////////
|
||||
|
||||
/*
|
||||
|
||||
|
||||
|
||||
CCriticalSection mutex;
|
||||
boost::recursive_mutex mutex;
|
||||
|
||||
@@ -42,57 +39,59 @@ ENTER_CRITICAL_SECTION(mutex); // no RAII
|
||||
|
||||
LEAVE_CRITICAL_SECTION(mutex); // no RAII
|
||||
mutex.unlock();
|
||||
|
||||
|
||||
|
||||
*/
|
||||
|
||||
|
||||
|
||||
///////////////////////////////
|
||||
// //
|
||||
// THE ACTUAL IMPLEMENTATION //
|
||||
// //
|
||||
///////////////////////////////
|
||||
|
||||
// Template mixin that adds -Wthread-safety locking annotations to a
|
||||
// subset of the mutex API.
|
||||
/**
|
||||
* Template mixin that adds -Wthread-safety locking
|
||||
* annotations to a subset of the mutex API.
|
||||
*/
|
||||
template <typename PARENT>
|
||||
class LOCKABLE AnnotatedMixin : public PARENT
|
||||
{
|
||||
public:
|
||||
void lock() EXCLUSIVE_LOCK_FUNCTION()
|
||||
{
|
||||
PARENT::lock();
|
||||
PARENT::lock();
|
||||
}
|
||||
|
||||
void unlock() UNLOCK_FUNCTION()
|
||||
{
|
||||
PARENT::unlock();
|
||||
PARENT::unlock();
|
||||
}
|
||||
|
||||
bool try_lock() EXCLUSIVE_TRYLOCK_FUNCTION(true)
|
||||
{
|
||||
return PARENT::try_lock();
|
||||
return PARENT::try_lock();
|
||||
}
|
||||
};
|
||||
|
||||
/** Wrapped boost mutex: supports recursive locking, but no waiting */
|
||||
// TODO: We should move away from using the recursive lock by default.
|
||||
/**
|
||||
* Wrapped boost mutex: supports recursive locking, but no waiting
|
||||
* TODO: We should move away from using the recursive lock by default.
|
||||
*/
|
||||
typedef AnnotatedMixin<boost::recursive_mutex> CCriticalSection;
|
||||
|
||||
/** Wrapped boost mutex: supports waiting but not recursive locking */
|
||||
typedef AnnotatedMixin<boost::mutex> CWaitableCriticalSection;
|
||||
|
||||
/** Just a typedef for boost::condition_variable, can be wrapped later if desired */
|
||||
typedef boost::condition_variable CConditionVariable;
|
||||
|
||||
#ifdef DEBUG_LOCKORDER
|
||||
void EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry = false);
|
||||
void LeaveCritical();
|
||||
std::string LocksHeld();
|
||||
void AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, void *cs);
|
||||
void AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs);
|
||||
#else
|
||||
void static inline EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry = false) {}
|
||||
void static inline LeaveCritical() {}
|
||||
void static inline AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, void *cs) {}
|
||||
void static inline AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs) {}
|
||||
#endif
|
||||
#define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs)
|
||||
|
||||
@@ -101,7 +100,7 @@ void PrintLockContention(const char* pszName, const char* pszFile, int nLine);
|
||||
#endif
|
||||
|
||||
/** Wrapper around boost::unique_lock<Mutex> */
|
||||
template<typename Mutex>
|
||||
template <typename Mutex>
|
||||
class CMutexLock
|
||||
{
|
||||
private:
|
||||
@@ -111,11 +110,10 @@ private:
|
||||
{
|
||||
EnterCritical(pszName, pszFile, nLine, (void*)(lock.mutex()));
|
||||
#ifdef DEBUG_LOCKCONTENTION
|
||||
if (!lock.try_lock())
|
||||
{
|
||||
if (!lock.try_lock()) {
|
||||
PrintLockContention(pszName, pszFile, nLine);
|
||||
#endif
|
||||
lock.lock();
|
||||
lock.lock();
|
||||
#ifdef DEBUG_LOCKCONTENTION
|
||||
}
|
||||
#endif
|
||||
@@ -139,6 +137,17 @@ public:
|
||||
Enter(pszName, pszFile, nLine);
|
||||
}
|
||||
|
||||
CMutexLock(Mutex* pmutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false)
|
||||
{
|
||||
if (!pmutexIn) return;
|
||||
|
||||
lock = boost::unique_lock<Mutex>(*pmutexIn, boost::defer_lock);
|
||||
if (fTry)
|
||||
TryEnter(pszName, pszFile, nLine);
|
||||
else
|
||||
Enter(pszName, pszFile, nLine);
|
||||
}
|
||||
|
||||
~CMutexLock()
|
||||
{
|
||||
if (lock.owns_lock())
|
||||
@@ -154,19 +163,19 @@ public:
|
||||
typedef CMutexLock<CCriticalSection> CCriticalBlock;
|
||||
|
||||
#define LOCK(cs) CCriticalBlock criticalblock(cs, #cs, __FILE__, __LINE__)
|
||||
#define LOCK2(cs1,cs2) CCriticalBlock criticalblock1(cs1, #cs1, __FILE__, __LINE__),criticalblock2(cs2, #cs2, __FILE__, __LINE__)
|
||||
#define TRY_LOCK(cs,name) CCriticalBlock name(cs, #cs, __FILE__, __LINE__, true)
|
||||
#define LOCK2(cs1, cs2) CCriticalBlock criticalblock1(cs1, #cs1, __FILE__, __LINE__), criticalblock2(cs2, #cs2, __FILE__, __LINE__)
|
||||
#define TRY_LOCK(cs, name) CCriticalBlock name(cs, #cs, __FILE__, __LINE__, true)
|
||||
|
||||
#define ENTER_CRITICAL_SECTION(cs) \
|
||||
{ \
|
||||
#define ENTER_CRITICAL_SECTION(cs) \
|
||||
{ \
|
||||
EnterCritical(#cs, __FILE__, __LINE__, (void*)(&cs)); \
|
||||
(cs).lock(); \
|
||||
(cs).lock(); \
|
||||
}
|
||||
|
||||
#define LEAVE_CRITICAL_SECTION(cs) \
|
||||
{ \
|
||||
(cs).unlock(); \
|
||||
LeaveCritical(); \
|
||||
{ \
|
||||
(cs).unlock(); \
|
||||
LeaveCritical(); \
|
||||
}
|
||||
|
||||
class CSemaphore
|
||||
@@ -179,7 +188,8 @@ private:
|
||||
public:
|
||||
CSemaphore(int init) : value(init) {}
|
||||
|
||||
void wait() {
|
||||
void wait()
|
||||
{
|
||||
boost::unique_lock<boost::mutex> lock(mutex);
|
||||
while (value < 1) {
|
||||
condition.wait(lock);
|
||||
@@ -187,7 +197,8 @@ public:
|
||||
value--;
|
||||
}
|
||||
|
||||
bool try_wait() {
|
||||
bool try_wait()
|
||||
{
|
||||
boost::unique_lock<boost::mutex> lock(mutex);
|
||||
if (value < 1)
|
||||
return false;
|
||||
@@ -195,7 +206,8 @@ public:
|
||||
return true;
|
||||
}
|
||||
|
||||
void post() {
|
||||
void post()
|
||||
{
|
||||
{
|
||||
boost::unique_lock<boost::mutex> lock(mutex);
|
||||
value++;
|
||||
@@ -208,31 +220,35 @@ public:
|
||||
class CSemaphoreGrant
|
||||
{
|
||||
private:
|
||||
CSemaphore *sem;
|
||||
CSemaphore* sem;
|
||||
bool fHaveGrant;
|
||||
|
||||
public:
|
||||
void Acquire() {
|
||||
void Acquire()
|
||||
{
|
||||
if (fHaveGrant)
|
||||
return;
|
||||
sem->wait();
|
||||
fHaveGrant = true;
|
||||
}
|
||||
|
||||
void Release() {
|
||||
void Release()
|
||||
{
|
||||
if (!fHaveGrant)
|
||||
return;
|
||||
sem->post();
|
||||
fHaveGrant = false;
|
||||
}
|
||||
|
||||
bool TryAcquire() {
|
||||
bool TryAcquire()
|
||||
{
|
||||
if (!fHaveGrant && sem->try_wait())
|
||||
fHaveGrant = true;
|
||||
return fHaveGrant;
|
||||
}
|
||||
|
||||
void MoveTo(CSemaphoreGrant &grant) {
|
||||
void MoveTo(CSemaphoreGrant& grant)
|
||||
{
|
||||
grant.Release();
|
||||
grant.sem = sem;
|
||||
grant.fHaveGrant = fHaveGrant;
|
||||
@@ -242,20 +258,23 @@ public:
|
||||
|
||||
CSemaphoreGrant() : sem(NULL), fHaveGrant(false) {}
|
||||
|
||||
CSemaphoreGrant(CSemaphore &sema, bool fTry = false) : sem(&sema), fHaveGrant(false) {
|
||||
CSemaphoreGrant(CSemaphore& sema, bool fTry = false) : sem(&sema), fHaveGrant(false)
|
||||
{
|
||||
if (fTry)
|
||||
TryAcquire();
|
||||
else
|
||||
Acquire();
|
||||
}
|
||||
|
||||
~CSemaphoreGrant() {
|
||||
~CSemaphoreGrant()
|
||||
{
|
||||
Release();
|
||||
}
|
||||
|
||||
operator bool() {
|
||||
operator bool()
|
||||
{
|
||||
return fHaveGrant;
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
#endif // BITCOIN_SYNC_H
|
||||
|
||||
Reference in New Issue
Block a user