aboutsummaryrefslogtreecommitdiff
path: root/src/util.h
diff options
context:
space:
mode:
authorGavin Andresen <gavinandresen@gmail.com>2011-11-21 11:33:14 -0800
committerGavin Andresen <gavinandresen@gmail.com>2011-11-21 11:33:14 -0800
commit42eb76a0540d8332650b34ba0f949d403fcd8a89 (patch)
treec7c8e0fef9c6914c63b3e2ae3c23717ce9724ca0 /src/util.h
parent92979f82882fbeca4e66a7b6ae5dc9b485588ca0 (diff)
parentf873b84d6e91407cb6c9ea292d16baed6ec07779 (diff)
Merge pull request #602 from wowus/master
Cleaned up critical section code.
Diffstat (limited to 'src/util.h')
-rw-r--r--src/util.h24
1 files changed, 15 insertions, 9 deletions
diff --git a/src/util.h b/src/util.h
index 178923727a..4c966486f7 100644
--- a/src/util.h
+++ b/src/util.h
@@ -243,19 +243,20 @@ public:
pcs = &csIn;
pcs->Enter(pszName, pszFile, nLine);
}
+
+ operator bool() const
+ {
+ return true;
+ }
+
~CCriticalBlock()
{
pcs->Leave();
}
};
-// WARNING: This will catch continue and break!
-// break is caught with an assertion, but there's no way to detect continue.
-// I'd rather be careful than suffer the other more error prone syntax.
-// The compiler will optimise away all this loop junk.
#define CRITICAL_BLOCK(cs) \
- for (bool fcriticalblockonce=true; fcriticalblockonce; assert(("break caught by CRITICAL_BLOCK!" && !fcriticalblockonce)), fcriticalblockonce=false) \
- for (CCriticalBlock criticalblock(cs, #cs, __FILE__, __LINE__); fcriticalblockonce; fcriticalblockonce=false)
+ if (CCriticalBlock criticalblock = CCriticalBlock(cs, #cs, __FILE__, __LINE__))
class CTryCriticalBlock
{
@@ -267,6 +268,12 @@ public:
{
pcs = (csIn.TryEnter(pszName, pszFile, nLine) ? &csIn : NULL);
}
+
+ operator bool() const
+ {
+ return Entered();
+ }
+
~CTryCriticalBlock()
{
if (pcs)
@@ -274,12 +281,11 @@ public:
pcs->Leave();
}
}
- bool Entered() { return pcs != NULL; }
+ bool Entered() const { return pcs != NULL; }
};
#define TRY_CRITICAL_BLOCK(cs) \
- for (bool fcriticalblockonce=true; fcriticalblockonce; assert(("break caught by TRY_CRITICAL_BLOCK!" && !fcriticalblockonce)), fcriticalblockonce=false) \
- for (CTryCriticalBlock criticalblock(cs, #cs, __FILE__, __LINE__); fcriticalblockonce && (fcriticalblockonce = criticalblock.Entered()); fcriticalblockonce=false)
+ if (CTryCriticalBlock criticalblock = CTryCriticalBlock(cs, #cs, __FILE__, __LINE__))