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

readlocktry - use in webserver so more reliabliy works

This commit is contained in:
Eliot Horowitz 2010-02-09 12:35:26 -05:00
parent 12a8eee52c
commit dc9f26e421
2 changed files with 54 additions and 13 deletions

View File

@ -161,6 +161,23 @@ namespace mongo {
_m.lock_shared(); _m.lock_shared();
curopGotLock(); curopGotLock();
} }
bool lock_shared_try( int millis ) {
int s = _state.get();
if ( s ){
// we already have a lock, so no need to try
lock_shared();
return true;
}
boost::system_time until = get_system_time();
until += boost::posix_time::milliseconds(2);
bool got = _m.timed_lock_shared( until );
if ( got )
_state.set(-1);
return got;
}
void unlock_shared() { void unlock_shared() {
//DEV cout << " UNLOCKSHARED" << endl; //DEV cout << " UNLOCKSHARED" << endl;
int s = _state.get(); int s = _state.get();
@ -177,6 +194,7 @@ namespace mongo {
_state.set(0); _state.set(0);
_m.unlock_shared(); _m.unlock_shared();
} }
MutexInfo& info() { return _minfo; } MutexInfo& info() { return _minfo; }
}; };
#else #else
@ -220,6 +238,18 @@ namespace mongo {
} }
void lock_shared() { lock(); } void lock_shared() { lock(); }
bool lock_shared_try( int millis ) {
while ( millis-- ){
if ( getState() ){
sleepmillis(1);
continue;
}
lock_shared();
return true;
}
return false;
}
void unlock_shared() { unlock(); } void unlock_shared() { unlock(); }
MutexInfo& info() { return _minfo; } MutexInfo& info() { return _minfo; }
void assertWriteLocked() { void assertWriteLocked() {
@ -262,6 +292,22 @@ namespace mongo {
} }
}; };
struct readlocktry {
readlocktry( const string&ns , int tryms ){
_got = dbMutex.lock_shared_try( tryms );
}
~readlocktry() {
if ( _got ){
dbunlocking_read();
dbMutex.unlock_shared();
}
}
bool got(){
return _got;
}
bool _got;
};
class mongolock { class mongolock {
bool _writelock; bool _writelock;
public: public:

View File

@ -316,24 +316,19 @@ namespace mongo {
doUnlockedStuff(ss); doUnlockedStuff(ss);
int n = 2000; {
Timer t; Timer t;
while ( 1 ) { readlocktry lk( "" , 2000 );
if ( !dbMutex.info().isLocked() ) { if ( lk.got() ){
{ ss << "time to get dblock: " << t.millis() << "ms\n";
readlock lk(""); doLockedStuff(ss);
ss << "time to get dblock: " << t.millis() << "ms\n";
doLockedStuff(ss);
}
break;
} }
sleepmillis(1); else {
if ( --n < 0 ) {
ss << "\n<b>timed out getting dblock</b>\n"; ss << "\n<b>timed out getting dblock</b>\n";
break;
} }
} }
ss << "</pre></body></html>"; ss << "</pre></body></html>";
responseMsg = ss.str(); responseMsg = ss.str();