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

try_lock and more scoped options

This commit is contained in:
Eliot Horowitz 2010-04-22 13:32:01 -04:00
parent 3e015f17c8
commit 8e58f02568

View File

@ -65,6 +65,14 @@ namespace mongo {
until += boost::posix_time::milliseconds(millis);
return _m.timed_lock_shared( until );
}
bool lock_try( int millis ){
boost::system_time until = get_system_time();
until += boost::posix_time::milliseconds(millis);
return _m.timed_lock( until );
}
};
#else
class RWLock {
@ -102,34 +110,52 @@ namespace mongo {
void unlock_shared(){
check( pthread_rwlock_unlock( &_lock ) );
}
bool lock_shared_try( int millis ){
while ( millis-- ){
int x = pthread_rwlock_tryrdlock( &_lock );
if ( x == 0 )
return true;
return _try( millis , false );
}
bool lock_try( int millis ){
return _try( millis , true );
}
bool _try( int millis , bool write ){
while ( true ) {
int x = write ?
pthread_rwlock_trywrlock( &_lock ) :
pthread_rwlock_tryrdlock( &_lock );
if ( x <= 0 )
return true;
if ( millis-- <= 0 )
return false;
if ( x == EBUSY ){
sleepmillis(1);
continue;
}
check(x);
}
}
return false;
}
};
#endif
struct rwlock {
rwlock( RWLock& lock , bool write )
rwlock( RWLock& lock , bool write , bool alreadyHaveLock = false )
: _lock( lock ) , _write( write ){
if ( _write )
_lock.lock();
else
_lock.lock_shared();
if ( ! alreadyHaveLock ){
if ( _write )
_lock.lock();
else
_lock.lock_shared();
}
}
~rwlock(){