00001 #pragma once
00002
00003 #include "../goodies.h"
00004
00005 namespace mongo {
00006
00022 #if defined(_DEBUG)
00023
00024 class CodeBlock {
00025 volatile int n;
00026 unsigned tid;
00027 void fail() {
00028 log() << "synchronization (race condition) failure" << endl;
00029 printStackTrace();
00030 abort();
00031 }
00032 void enter() {
00033 if( ++n != 1 ) fail();
00034 #if defined(_WIN32)
00035 tid = GetCurrentThreadId();
00036 #endif
00037 }
00038 void leave() {
00039 if( --n != 0 ) fail();
00040 }
00041 public:
00042 CodeBlock() : n(0) { }
00043
00044 class Within {
00045 CodeBlock& _s;
00046 public:
00047 Within(CodeBlock& s) : _s(s) { _s.enter(); }
00048 ~Within() { _s.leave(); }
00049 };
00050
00051 void assertWithin() {
00052 assert( n == 1 );
00053 #if defined(_WIN32)
00054 assert( GetCurrentThreadId() == tid );
00055 #endif
00056 }
00057 };
00058
00059 #else
00060
00061 class CodeBlock{
00062 public:
00063 class Within {
00064 public:
00065 Within(CodeBlock&) { }
00066 };
00067 void assertWithin() { }
00068 };
00069
00070 #endif
00071
00072 }