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

lowPriority option to RWLock

This commit is contained in:
Eliot Horowitz 2011-03-01 02:37:58 -05:00
parent 8451714774
commit 8fb3dd5492
2 changed files with 40 additions and 4 deletions

View File

@ -223,6 +223,16 @@ namespace ThreadedTests {
}
};
class RWLockTest1 {
public:
void run() {
RWLock lk( "eliot" );
{
rwlock r( lk , true , false , 1000 );
}
}
};
class All : public Suite {
public:
All() : Suite( "threading" ) {
@ -233,6 +243,7 @@ namespace ThreadedTests {
add< MVarTest >();
add< ThreadPoolTest >();
add< LockTest >();
add< RWLockTest1 >();
add< MongoMutexTest >();
}
} myall;

View File

@ -248,13 +248,38 @@ namespace mongo {
/* scoped lock for RWLock */
class rwlock {
public:
rwlock( const RWLock& lock , bool write , bool alreadyHaveLock = false )
/**
* @param lowPriority if > 0, will try to get the lock not gredily for that many # of ms
*/
rwlock( const RWLock& lock , bool write , bool alreadyHaveLock = false , int lowPriorityWaitMS = 0 )
: _lock( (RWLock&)lock ) , _write( write ) {
if ( ! alreadyHaveLock ) {
if ( _write )
_lock.lock();
else
if ( _write ) {
if ( lowPriorityWaitMS ) {
bool got = false;
for ( int i=0; i<lowPriorityWaitMS/2; i++ ) { // we divide by 2 since we sleep a bit
if ( _lock.lock_try(1) ) {
got = true;
break;
}
sleepmillis(1);
}
if ( ! got ) {
log() << "couldn't get lazy rwlock" << endl;
_lock.lock();
}
}
else {
_lock.lock();
}
}
else {
_lock.lock_shared();
}
}
}
~rwlock() {