Changeset View
Changeset View
Standalone View
Standalone View
src/sync.h
Show First 20 Lines • Show All 123 Lines • ▼ Show 20 Lines | |||||
#ifdef DEBUG_LOCKCONTENTION | #ifdef DEBUG_LOCKCONTENTION | ||||
} | } | ||||
#endif | #endif | ||||
} | } | ||||
bool TryEnter(const char *pszName, const char *pszFile, int nLine) { | bool TryEnter(const char *pszName, const char *pszFile, int nLine) { | ||||
EnterCritical(pszName, pszFile, nLine, (void *)(Base::mutex()), true); | EnterCritical(pszName, pszFile, nLine, (void *)(Base::mutex()), true); | ||||
Base::try_lock(); | Base::try_lock(); | ||||
if (!Base::owns_lock()) LeaveCritical(); | if (!Base::owns_lock()) { | ||||
LeaveCritical(); | |||||
} | |||||
return Base::owns_lock(); | return Base::owns_lock(); | ||||
} | } | ||||
public: | public: | ||||
UniqueLock(Mutex &mutexIn, const char *pszName, const char *pszFile, | UniqueLock(Mutex &mutexIn, const char *pszName, const char *pszFile, | ||||
int nLine, bool fTry = false) EXCLUSIVE_LOCK_FUNCTION(mutexIn) | int nLine, bool fTry = false) EXCLUSIVE_LOCK_FUNCTION(mutexIn) | ||||
: Base(mutexIn, std::defer_lock) { | : Base(mutexIn, std::defer_lock) { | ||||
if (fTry) { | if (fTry) { | ||||
▲ Show 20 Lines • Show All 92 Lines • ▼ Show 20 Lines | |||||
/** RAII-style semaphore lock */ | /** RAII-style semaphore lock */ | ||||
class CSemaphoreGrant { | class CSemaphoreGrant { | ||||
private: | private: | ||||
CSemaphore *sem; | CSemaphore *sem; | ||||
bool fHaveGrant; | bool fHaveGrant; | ||||
public: | public: | ||||
void Acquire() { | void Acquire() { | ||||
if (fHaveGrant) return; | if (fHaveGrant) { | ||||
return; | |||||
} | |||||
sem->wait(); | sem->wait(); | ||||
fHaveGrant = true; | fHaveGrant = true; | ||||
} | } | ||||
void Release() { | void Release() { | ||||
if (!fHaveGrant) return; | if (!fHaveGrant) { | ||||
return; | |||||
} | |||||
sem->post(); | sem->post(); | ||||
fHaveGrant = false; | fHaveGrant = false; | ||||
} | } | ||||
bool TryAcquire() { | bool TryAcquire() { | ||||
if (!fHaveGrant && sem->try_wait()) fHaveGrant = true; | if (!fHaveGrant && sem->try_wait()) { | ||||
fHaveGrant = true; | |||||
} | |||||
return fHaveGrant; | return fHaveGrant; | ||||
} | } | ||||
void MoveTo(CSemaphoreGrant &grant) { | void MoveTo(CSemaphoreGrant &grant) { | ||||
grant.Release(); | grant.Release(); | ||||
grant.sem = sem; | grant.sem = sem; | ||||
grant.fHaveGrant = fHaveGrant; | grant.fHaveGrant = fHaveGrant; | ||||
fHaveGrant = false; | fHaveGrant = false; | ||||
} | } | ||||
CSemaphoreGrant() : sem(nullptr), fHaveGrant(false) {} | CSemaphoreGrant() : sem(nullptr), fHaveGrant(false) {} | ||||
explicit CSemaphoreGrant(CSemaphore &sema, bool fTry = false) | explicit CSemaphoreGrant(CSemaphore &sema, bool fTry = false) | ||||
: sem(&sema), fHaveGrant(false) { | : sem(&sema), fHaveGrant(false) { | ||||
if (fTry) | if (fTry) { | ||||
TryAcquire(); | TryAcquire(); | ||||
else | } else { | ||||
Acquire(); | Acquire(); | ||||
} | } | ||||
} | |||||
~CSemaphoreGrant() { Release(); } | ~CSemaphoreGrant() { Release(); } | ||||
operator bool() const { return fHaveGrant; } | operator bool() const { return fHaveGrant; } | ||||
}; | }; | ||||
#endif // BITCOIN_SYNC_H | #endif // BITCOIN_SYNC_H |