0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 09:32:32 +01:00

some mongomutex cleanup

This commit is contained in:
Dwight 2009-12-03 13:48:45 -05:00
parent f05b2a328e
commit 48217c0650
3 changed files with 26 additions and 21 deletions

View File

@ -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<int> _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<boost::recursive_mutex>::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<boost::recursive_mutex>::unlock(m);
#endif
}
void lock_shared() { lock(); }

View File

@ -59,6 +59,7 @@ namespace mongo {
MongoMutex &dbMutex( *(new MongoMutex) );
MutexInfo dbMutexInfo;
string dbExecCommand;
string bind_ip = "";

View File

@ -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<T> _val;