2010-10-07 06:12:32 +02:00
|
|
|
// @file mongomutex.h
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 10gen Inc.
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2010-12-13 01:47:10 +01:00
|
|
|
// note: include concurrency.h, not this.
|
|
|
|
|
2010-10-07 06:12:32 +02:00
|
|
|
namespace mongo {
|
|
|
|
|
2010-11-27 21:25:08 +01:00
|
|
|
/** the 'big lock' we use for most operations. a read/write lock.
|
|
|
|
there is one of these, dbMutex.
|
2010-10-19 19:01:46 +02:00
|
|
|
|
2010-11-27 21:25:08 +01:00
|
|
|
generally if you need to declare a mutex use the right primitive class, not this.
|
|
|
|
|
|
|
|
use readlock and writelock classes for scoped locks on this rather than direct
|
|
|
|
manipulation.
|
2010-10-19 19:01:46 +02:00
|
|
|
*/
|
2010-10-07 06:12:32 +02:00
|
|
|
class MongoMutex {
|
|
|
|
public:
|
2010-10-07 06:56:12 +02:00
|
|
|
MongoMutex(const char * name);
|
2010-10-07 06:12:32 +02:00
|
|
|
|
|
|
|
/** @return
|
|
|
|
* > 0 write lock
|
|
|
|
* = 0 no lock
|
|
|
|
* < 0 read lock
|
|
|
|
*/
|
|
|
|
int getState() const { return _state.get(); }
|
2010-10-19 19:01:46 +02:00
|
|
|
|
|
|
|
bool atLeastReadLocked() const { return _state.get() != 0; }
|
|
|
|
void assertAtLeastReadLocked() const { assert(atLeastReadLocked()); }
|
2010-10-07 06:12:32 +02:00
|
|
|
bool isWriteLocked() const { return getState() > 0; }
|
|
|
|
void assertWriteLocked() const {
|
|
|
|
assert( getState() > 0 );
|
|
|
|
DEV assert( !_releasedEarly.get() );
|
|
|
|
}
|
|
|
|
|
2010-11-27 21:25:08 +01:00
|
|
|
// write lock. use the writelock scoped lock class, not this directly.
|
2010-10-07 06:12:32 +02:00
|
|
|
void lock() {
|
2010-10-19 19:01:46 +02:00
|
|
|
if ( _writeLockedAlready() )
|
2010-10-07 06:12:32 +02:00
|
|
|
return;
|
2010-11-27 21:25:08 +01:00
|
|
|
|
2010-10-07 06:12:32 +02:00
|
|
|
_state.set(1);
|
|
|
|
|
2010-10-19 19:25:23 +02:00
|
|
|
Client *c = curopWaitingForLock( 1 ); // stats
|
2010-10-07 06:12:32 +02:00
|
|
|
_m.lock();
|
2010-10-19 19:25:23 +02:00
|
|
|
curopGotLock(c);
|
2010-10-07 06:12:32 +02:00
|
|
|
|
|
|
|
_minfo.entered();
|
|
|
|
|
2010-12-07 02:25:20 +01:00
|
|
|
MongoFile::markAllWritable(); // for _DEBUG validation -- a no op for release build
|
2010-11-27 21:25:08 +01:00
|
|
|
|
|
|
|
_acquiredWriteLock();
|
2010-10-07 06:12:32 +02:00
|
|
|
}
|
|
|
|
|
2010-10-19 19:01:46 +02:00
|
|
|
// try write lock
|
2010-10-07 06:12:32 +02:00
|
|
|
bool lock_try( int millis ) {
|
2010-10-19 19:01:46 +02:00
|
|
|
if ( _writeLockedAlready() )
|
2010-10-07 06:12:32 +02:00
|
|
|
return true;
|
|
|
|
|
2010-10-19 19:25:23 +02:00
|
|
|
Client *c = curopWaitingForLock( 1 );
|
2010-10-07 06:12:32 +02:00
|
|
|
bool got = _m.lock_try( millis );
|
2010-10-19 19:25:23 +02:00
|
|
|
curopGotLock(c);
|
2010-10-07 06:12:32 +02:00
|
|
|
|
2010-11-27 21:25:08 +01:00
|
|
|
if ( got ) {
|
2010-10-07 06:12:32 +02:00
|
|
|
_minfo.entered();
|
|
|
|
_state.set(1);
|
2010-12-07 02:25:20 +01:00
|
|
|
MongoFile::markAllWritable(); // for _DEBUG validation -- a no op for release build
|
2010-11-27 21:25:08 +01:00
|
|
|
_acquiredWriteLock();
|
2010-10-07 06:12:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return got;
|
|
|
|
}
|
|
|
|
|
2010-10-19 19:01:46 +02:00
|
|
|
// un write lock
|
2010-10-07 06:12:32 +02:00
|
|
|
void unlock() {
|
2010-12-16 22:34:05 +01:00
|
|
|
_releasingWriteLock();
|
2010-10-07 06:12:32 +02:00
|
|
|
int s = _state.get();
|
|
|
|
if( s > 1 ) {
|
2010-11-27 21:25:08 +01:00
|
|
|
_state.set(s-1); // recursive lock case
|
2010-10-07 06:12:32 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if( s != 1 ) {
|
|
|
|
if( _releasedEarly.get() ) {
|
|
|
|
_releasedEarly.set(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
massert( 12599, "internal error: attempt to unlock when wasn't in a write lock", false);
|
|
|
|
}
|
2010-12-07 02:25:20 +01:00
|
|
|
MongoFile::unmarkAllWritable(); // _DEBUG validation
|
2010-10-07 06:12:32 +02:00
|
|
|
_state.set(0);
|
|
|
|
_minfo.leaving();
|
|
|
|
_m.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* unlock (write lock), and when unlock() is called later,
|
|
|
|
be smart then and don't unlock it again.
|
|
|
|
*/
|
|
|
|
void releaseEarly() {
|
|
|
|
assert( getState() == 1 ); // must not be recursive
|
|
|
|
assert( !_releasedEarly.get() );
|
|
|
|
_releasedEarly.set(true);
|
|
|
|
unlock();
|
|
|
|
}
|
|
|
|
|
2010-11-27 21:25:08 +01:00
|
|
|
// read lock. don't call directly, use readlock.
|
2010-10-07 06:12:32 +02:00
|
|
|
void lock_shared() {
|
|
|
|
int s = _state.get();
|
|
|
|
if( s ) {
|
|
|
|
if( s > 0 ) {
|
|
|
|
// already in write lock - just be recursive and stay write locked
|
|
|
|
_state.set(s+1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// already in read lock - recurse
|
|
|
|
_state.set(s-1);
|
|
|
|
}
|
|
|
|
}
|
2010-11-27 21:25:08 +01:00
|
|
|
else {
|
|
|
|
_state.set(-1);
|
|
|
|
Client *c = curopWaitingForLock( -1 );
|
|
|
|
_m.lock_shared();
|
|
|
|
curopGotLock(c);
|
|
|
|
}
|
2010-10-07 06:12:32 +02:00
|
|
|
}
|
|
|
|
|
2010-10-19 19:01:46 +02:00
|
|
|
// try read lock
|
2010-10-07 06:12:32 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-10-19 19:25:23 +02:00
|
|
|
/* [dm] should there be
|
|
|
|
Client *c = curopWaitingForLock( 1 );
|
|
|
|
here? i think so. seems to be missing.
|
|
|
|
*/
|
2010-10-07 06:12:32 +02:00
|
|
|
bool got = _m.lock_shared_try( millis );
|
|
|
|
if ( got )
|
|
|
|
_state.set(-1);
|
|
|
|
return got;
|
|
|
|
}
|
|
|
|
|
|
|
|
void unlock_shared() {
|
|
|
|
int s = _state.get();
|
|
|
|
if( s > 0 ) {
|
|
|
|
assert( s > 1 ); /* we must have done a lock write first to have s > 1 */
|
|
|
|
_state.set(s-1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if( s < -1 ) {
|
|
|
|
_state.set(s+1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
assert( s == -1 );
|
|
|
|
_state.set(0);
|
|
|
|
_m.unlock_shared();
|
|
|
|
}
|
|
|
|
|
|
|
|
MutexInfo& info() { return _minfo; }
|
|
|
|
|
|
|
|
private:
|
2010-11-27 21:25:08 +01:00
|
|
|
void _acquiredWriteLock();
|
2010-12-16 22:34:05 +01:00
|
|
|
void _releasingWriteLock();
|
2010-11-27 21:25:08 +01:00
|
|
|
|
2010-10-19 19:01:46 +02:00
|
|
|
/* @return true if was already write locked. increments recursive lock count. */
|
2010-11-27 21:25:08 +01:00
|
|
|
bool _writeLockedAlready();
|
2010-10-19 19:01:46 +02:00
|
|
|
|
2010-10-07 06:12:32 +02:00
|
|
|
RWLock _m;
|
|
|
|
|
|
|
|
/* > 0 write lock with recurse count
|
|
|
|
< 0 read lock
|
|
|
|
*/
|
|
|
|
ThreadLocalValue<int> _state;
|
|
|
|
|
2010-10-07 06:56:12 +02:00
|
|
|
MutexInfo _minfo;
|
|
|
|
|
2010-11-27 21:25:08 +01:00
|
|
|
public:
|
|
|
|
// indicates we need to call dur::REMAPPRIVATEVIEW on the next write lock
|
|
|
|
bool _remapPrivateViewRequested;
|
|
|
|
|
|
|
|
private:
|
2010-10-07 06:12:32 +02:00
|
|
|
/* See the releaseEarly() method.
|
|
|
|
we use a separate TLS value for releasedEarly - that is ok as
|
|
|
|
our normal/common code path, we never even touch it */
|
|
|
|
ThreadLocalValue<bool> _releasedEarly;
|
2010-10-19 19:01:46 +02:00
|
|
|
|
|
|
|
/* this is for fsyncAndLock command. otherwise write lock's greediness will
|
|
|
|
make us block on any attempted write lock the the fsync's lock.
|
|
|
|
*/
|
|
|
|
//volatile bool _blockWrites;
|
2010-10-07 06:12:32 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
extern MongoMutex &dbMutex;
|
|
|
|
|
2010-11-27 21:25:08 +01:00
|
|
|
namespace dur {
|
|
|
|
void REMAPPRIVATEVIEW();
|
2010-12-16 22:34:05 +01:00
|
|
|
void releasingWriteLock(); // because it's hard to include dur.h here
|
2010-11-27 23:30:51 +01:00
|
|
|
}
|
|
|
|
|
2010-12-16 22:34:05 +01:00
|
|
|
inline void MongoMutex::_releasingWriteLock() {
|
2010-12-18 00:59:50 +01:00
|
|
|
dur::releasingWriteLock();
|
2010-11-27 21:25:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void MongoMutex::_acquiredWriteLock() {
|
2010-12-18 00:59:50 +01:00
|
|
|
if( _remapPrivateViewRequested ) {
|
2010-11-27 21:25:08 +01:00
|
|
|
dur::REMAPPRIVATEVIEW();
|
|
|
|
dassert( !_remapPrivateViewRequested );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* @return true if was already write locked. increments recursive lock count. */
|
|
|
|
inline bool MongoMutex::_writeLockedAlready() {
|
|
|
|
dassert( haveClient() );
|
|
|
|
int s = _state.get();
|
|
|
|
if( s > 0 ) {
|
|
|
|
_state.set(s+1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
massert( 10293 , string("internal error: locks are not upgradeable: ") + sayClientState() , s == 0 );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-10-07 06:12:32 +02:00
|
|
|
}
|