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:
parent
f05b2a328e
commit
48217c0650
@ -34,11 +34,11 @@ namespace mongo {
|
|||||||
if ( locked == 0 )
|
if ( locked == 0 )
|
||||||
enter = curTimeMicros64();
|
enter = curTimeMicros64();
|
||||||
locked++;
|
locked++;
|
||||||
assert( locked >= 1 );
|
assert( locked == 1 );
|
||||||
}
|
}
|
||||||
void leaving() {
|
void leaving() {
|
||||||
locked--;
|
locked--;
|
||||||
assert( locked >= 0 );
|
assert( locked == 0 );
|
||||||
if ( locked == 0 )
|
if ( locked == 0 )
|
||||||
timeLocked += curTimeMicros64() - enter;
|
timeLocked += curTimeMicros64() - enter;
|
||||||
}
|
}
|
||||||
@ -52,27 +52,36 @@ namespace mongo {
|
|||||||
};
|
};
|
||||||
|
|
||||||
#if BOOST_VERSION >= 103500
|
#if BOOST_VERSION >= 103500
|
||||||
class MongoMutex {
|
class MongoMutex {
|
||||||
MutexInfo _minfo;
|
MutexInfo _minfo;
|
||||||
boost::shared_mutex m;
|
boost::shared_mutex _m;
|
||||||
|
ThreadLocalValue<int> _state;
|
||||||
public:
|
public:
|
||||||
void lock() {
|
void lock() {
|
||||||
cout << "LOCK" << endl;
|
DEV cout << "LOCK" << endl;
|
||||||
m.lock();
|
DEV assert( _state.get() == 0 );
|
||||||
|
DEV _state.set(1);
|
||||||
|
_m.lock();
|
||||||
_minfo.entered();
|
_minfo.entered();
|
||||||
}
|
}
|
||||||
void unlock() {
|
void unlock() {
|
||||||
cout << "UNLOCK" << endl;
|
DEV cout << "UNLOCK" << endl;
|
||||||
|
DEV assert( _state.get() == 1 );
|
||||||
|
DEV _state.set(0);
|
||||||
_minfo.leaving();
|
_minfo.leaving();
|
||||||
m.unlock();
|
_m.unlock();
|
||||||
}
|
}
|
||||||
void lock_shared() {
|
void lock_shared() {
|
||||||
cout << " LOCKSHARED" << endl;
|
DEV cout << " LOCKSHARED" << endl;
|
||||||
m.lock_shared();
|
DEV assert( _state.get() == 0 );
|
||||||
|
DEV _state.set(2);
|
||||||
|
_m.lock_shared();
|
||||||
}
|
}
|
||||||
void unlock_shared() {
|
void unlock_shared() {
|
||||||
cout << " UNLOCKSHARED" << endl;
|
DEV cout << " UNLOCKSHARED" << endl;
|
||||||
m.unlock_shared();
|
DEV assert( _state.get() == 2 );
|
||||||
|
DEV _state.set(0);
|
||||||
|
_m.unlock_shared();
|
||||||
}
|
}
|
||||||
MutexInfo& info() { return _minfo; }
|
MutexInfo& info() { return _minfo; }
|
||||||
};
|
};
|
||||||
@ -84,21 +93,14 @@ cout << "LOCK" << endl;
|
|||||||
public:
|
public:
|
||||||
MongoMutex() { }
|
MongoMutex() { }
|
||||||
void lock() {
|
void lock() {
|
||||||
#if BOOST_VERSION >= 103500
|
|
||||||
m.lock();
|
|
||||||
#else
|
|
||||||
boost::detail::thread::lock_ops<boost::recursive_mutex>::lock(m);
|
boost::detail::thread::lock_ops<boost::recursive_mutex>::lock(m);
|
||||||
#endif
|
|
||||||
_minfo.entered();
|
_minfo.entered();
|
||||||
}
|
}
|
||||||
|
|
||||||
void unlock() {
|
void unlock() {
|
||||||
_minfo.leaving();
|
_minfo.leaving();
|
||||||
#if BOOST_VERSION >= 103500
|
// boost >1.35 would be: m.unlock();
|
||||||
m.unlock();
|
|
||||||
#else
|
|
||||||
boost::detail::thread::lock_ops<boost::recursive_mutex>::unlock(m);
|
boost::detail::thread::lock_ops<boost::recursive_mutex>::unlock(m);
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void lock_shared() { lock(); }
|
void lock_shared() { lock(); }
|
||||||
|
@ -59,6 +59,7 @@ namespace mongo {
|
|||||||
MongoMutex &dbMutex( *(new MongoMutex) );
|
MongoMutex &dbMutex( *(new MongoMutex) );
|
||||||
MutexInfo dbMutexInfo;
|
MutexInfo dbMutexInfo;
|
||||||
|
|
||||||
|
|
||||||
string dbExecCommand;
|
string dbExecCommand;
|
||||||
|
|
||||||
string bind_ip = "";
|
string bind_ip = "";
|
||||||
|
@ -352,12 +352,13 @@ namespace mongo {
|
|||||||
public:
|
public:
|
||||||
ThreadLocalValue( T def = 0 ) : _default( def ) { }
|
ThreadLocalValue( T def = 0 ) : _default( def ) { }
|
||||||
|
|
||||||
int get() {
|
T get() {
|
||||||
T * val = _val.get();
|
T * val = _val.get();
|
||||||
if ( val )
|
if ( val )
|
||||||
return *val;
|
return *val;
|
||||||
return _default;
|
return _default;
|
||||||
}
|
}
|
||||||
|
|
||||||
void set( const T& i ) {
|
void set( const T& i ) {
|
||||||
T *v = _val.get();
|
T *v = _val.get();
|
||||||
if( v ) {
|
if( v ) {
|
||||||
@ -367,6 +368,7 @@ namespace mongo {
|
|||||||
v = new T(i);
|
v = new T(i);
|
||||||
_val.reset( v );
|
_val.reset( v );
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
T _default;
|
T _default;
|
||||||
boost::thread_specific_ptr<T> _val;
|
boost::thread_specific_ptr<T> _val;
|
||||||
|
Loading…
Reference in New Issue
Block a user