aboutsummaryrefslogtreecommitdiff
path: root/src/sync.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/sync.h')
-rw-r--r--src/sync.h70
1 files changed, 40 insertions, 30 deletions
diff --git a/src/sync.h b/src/sync.h
index 4b81b4bd32..cd0aa7b20e 100644
--- a/src/sync.h
+++ b/src/sync.h
@@ -48,7 +48,6 @@ LEAVE_CRITICAL_SECTION(mutex); // no RAII
*/
-
///////////////////////////////
// //
// THE ACTUAL IMPLEMENTATION //
@@ -63,17 +62,17 @@ 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();
}
};
@@ -91,11 +90,13 @@ typedef boost::condition_variable CConditionVariable;
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 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)
@@ -104,7 +105,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:
@@ -114,11 +115,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
@@ -157,19 +157,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
@@ -182,7 +182,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);
@@ -190,7 +191,8 @@ public:
value--;
}
- bool try_wait() {
+ bool try_wait()
+ {
boost::unique_lock<boost::mutex> lock(mutex);
if (value < 1)
return false;
@@ -198,7 +200,8 @@ public:
return true;
}
- void post() {
+ void post()
+ {
{
boost::unique_lock<boost::mutex> lock(mutex);
value++;
@@ -211,31 +214,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;
@@ -245,18 +252,21 @@ 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;
}
};