0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-11-30 00:56:44 +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();
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() {
//DEV cout << " UNLOCKSHARED" << endl;
int s = _state.get();
@ -177,6 +194,7 @@ namespace mongo {
_state.set(0);
_m.unlock_shared();
}
MutexInfo& info() { return _minfo; }
};
#else
@ -220,6 +238,18 @@ namespace mongo {
}
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(); }
MutexInfo& info() { return _minfo; }
void assertWriteLocked() {
@ -261,6 +291,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 {
bool _writelock;

View File

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