diff options
author | Dwight <dmerriman@gmail.com> | 2009-12-03 14:14:06 -0500 |
---|---|---|
committer | Dwight <dmerriman@gmail.com> | 2009-12-03 14:14:06 -0500 |
commit | d4bb21cc66a9be733b95a4ba0e08eb3ac875fa9a (patch) | |
tree | ff6d533683b860f06a0cab3f5c055fd2cf52307b /db/concurrency.h | |
parent | 48217c0650f603acaaab5732795119f48272af4e (diff) | |
download | mongo-d4bb21cc66a9be733b95a4ba0e08eb3ac875fa9a.tar.gz |
make MongoMutex recursive
Diffstat (limited to 'db/concurrency.h')
-rw-r--r-- | db/concurrency.h | 37 |
1 files changed, 29 insertions, 8 deletions
diff --git a/db/concurrency.h b/db/concurrency.h index 9a3ef046f09..41f284b8599 100644 --- a/db/concurrency.h +++ b/db/concurrency.h @@ -59,28 +59,49 @@ namespace mongo { public: void lock() { DEV cout << "LOCK" << endl; - DEV assert( _state.get() == 0 ); - DEV _state.set(1); + int s = _state.get(); + if( s > 0 ) { + _state.set(s+1); + return; + } + assert( s == 0 ); + _state.set(1); _m.lock(); _minfo.entered(); } void unlock() { DEV cout << "UNLOCK" << endl; - DEV assert( _state.get() == 1 ); - DEV _state.set(0); + int s = _state.get(); + if( s > 1 ) { + _state.set(s-1); + return; + } + assert( s == 1 ); + _state.set(0); _minfo.leaving(); _m.unlock(); } void lock_shared() { DEV cout << " LOCKSHARED" << endl; - DEV assert( _state.get() == 0 ); - DEV _state.set(2); + int s = _state.get(); + assert( s >= 0 ); + if( s > 0 ) { + // already in write lock - just be recursive and stay write locked + _state.set(s+1); + return; + } + _state.set(-1); _m.lock_shared(); } void unlock_shared() { DEV cout << " UNLOCKSHARED" << endl; - DEV assert( _state.get() == 2 ); - DEV _state.set(0); + int s = _state.get(); + if( s > 1 ) { + _state.set(s-1); + return; + } + assert( s == -1 ); + _state.set(0); _m.unlock_shared(); } MutexInfo& info() { return _minfo; } |