From 48217c0650f603acaaab5732795119f48272af4e Mon Sep 17 00:00:00 2001 From: Dwight Date: Thu, 3 Dec 2009 13:48:45 -0500 Subject: [PATCH] some mongomutex cleanup --- db/concurrency.h | 42 ++++++++++++++++++++++-------------------- db/instance.cpp | 1 + util/goodies.h | 4 +++- 3 files changed, 26 insertions(+), 21 deletions(-) diff --git a/db/concurrency.h b/db/concurrency.h index 4b38b41f37f..9a3ef046f09 100644 --- a/db/concurrency.h +++ b/db/concurrency.h @@ -34,11 +34,11 @@ namespace mongo { if ( locked == 0 ) enter = curTimeMicros64(); locked++; - assert( locked >= 1 ); + assert( locked == 1 ); } void leaving() { locked--; - assert( locked >= 0 ); + assert( locked == 0 ); if ( locked == 0 ) timeLocked += curTimeMicros64() - enter; } @@ -52,27 +52,36 @@ namespace mongo { }; #if BOOST_VERSION >= 103500 - class MongoMutex { + class MongoMutex { MutexInfo _minfo; - boost::shared_mutex m; + boost::shared_mutex _m; + ThreadLocalValue _state; public: void lock() { -cout << "LOCK" << endl; - m.lock(); + DEV cout << "LOCK" << endl; + DEV assert( _state.get() == 0 ); + DEV _state.set(1); + _m.lock(); _minfo.entered(); } void unlock() { - cout << "UNLOCK" << endl; + DEV cout << "UNLOCK" << endl; + DEV assert( _state.get() == 1 ); + DEV _state.set(0); _minfo.leaving(); - m.unlock(); + _m.unlock(); } void lock_shared() { - cout << " LOCKSHARED" << endl; - m.lock_shared(); + DEV cout << " LOCKSHARED" << endl; + DEV assert( _state.get() == 0 ); + DEV _state.set(2); + _m.lock_shared(); } void unlock_shared() { - cout << " UNLOCKSHARED" << endl; - m.unlock_shared(); + DEV cout << " UNLOCKSHARED" << endl; + DEV assert( _state.get() == 2 ); + DEV _state.set(0); + _m.unlock_shared(); } MutexInfo& info() { return _minfo; } }; @@ -84,21 +93,14 @@ cout << "LOCK" << endl; public: MongoMutex() { } void lock() { -#if BOOST_VERSION >= 103500 - m.lock(); -#else boost::detail::thread::lock_ops::lock(m); -#endif _minfo.entered(); } void unlock() { _minfo.leaving(); -#if BOOST_VERSION >= 103500 - m.unlock(); -#else + // boost >1.35 would be: m.unlock(); boost::detail::thread::lock_ops::unlock(m); -#endif } void lock_shared() { lock(); } diff --git a/db/instance.cpp b/db/instance.cpp index a9008ba6cbc..a9effefb355 100644 --- a/db/instance.cpp +++ b/db/instance.cpp @@ -59,6 +59,7 @@ namespace mongo { MongoMutex &dbMutex( *(new MongoMutex) ); MutexInfo dbMutexInfo; + string dbExecCommand; string bind_ip = ""; diff --git a/util/goodies.h b/util/goodies.h index 0002dab662e..3e169f7b195 100644 --- a/util/goodies.h +++ b/util/goodies.h @@ -352,12 +352,13 @@ namespace mongo { public: ThreadLocalValue( T def = 0 ) : _default( def ) { } - int get() { + T get() { T * val = _val.get(); if ( val ) return *val; return _default; } + void set( const T& i ) { T *v = _val.get(); if( v ) { @@ -367,6 +368,7 @@ namespace mongo { v = new T(i); _val.reset( v ); } + private: T _default; boost::thread_specific_ptr _val;