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:
parent
8451714774
commit
8fb3dd5492
@ -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;
|
||||
|
@ -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() {
|
||||
|
Loading…
Reference in New Issue
Block a user