2009-10-25 05:47:31 +01:00
|
|
|
/* concurrency.h
|
|
|
|
|
|
|
|
mongod concurrency rules & notes will be placed here.
|
|
|
|
|
|
|
|
Mutex heirarchy (1 = "leaf")
|
|
|
|
name level
|
|
|
|
Logstream::mutex 1
|
|
|
|
ClientCursor::ccmutex 2
|
|
|
|
dblock 3
|
|
|
|
|
|
|
|
End func name with _inlock to indicate "caller must lock before calling".
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2009-11-28 21:49:16 +01:00
|
|
|
#if BOOST_VERSION >= 103500
|
2009-11-28 17:50:46 +01:00
|
|
|
#include <boost/thread/shared_mutex.hpp>
|
2009-11-30 21:12:22 +01:00
|
|
|
#undef assert
|
|
|
|
#define assert xassert
|
2009-12-30 21:58:16 +01:00
|
|
|
#else
|
|
|
|
#warning built with boost version 1.34 or older limited concurrency
|
2009-11-28 21:49:16 +01:00
|
|
|
#endif
|
2009-11-28 17:50:46 +01:00
|
|
|
|
2009-10-25 05:47:31 +01:00
|
|
|
namespace mongo {
|
|
|
|
|
2009-12-03 19:12:51 +01:00
|
|
|
/* mutex time stats */
|
|
|
|
class MutexInfo {
|
|
|
|
unsigned long long start, enter, timeLocked; // all in microseconds
|
|
|
|
int locked;
|
|
|
|
|
|
|
|
public:
|
|
|
|
MutexInfo() : locked(0) {
|
|
|
|
start = curTimeMicros64();
|
|
|
|
}
|
|
|
|
void entered() {
|
|
|
|
if ( locked == 0 )
|
|
|
|
enter = curTimeMicros64();
|
|
|
|
locked++;
|
2009-12-03 21:24:16 +01:00
|
|
|
assert( locked >= 1 );
|
2009-12-03 19:12:51 +01:00
|
|
|
}
|
|
|
|
void leaving() {
|
|
|
|
locked--;
|
2009-12-03 21:24:16 +01:00
|
|
|
assert( locked >= 0 );
|
2009-12-03 19:12:51 +01:00
|
|
|
if ( locked == 0 )
|
|
|
|
timeLocked += curTimeMicros64() - enter;
|
|
|
|
}
|
|
|
|
int isLocked() const {
|
|
|
|
return locked;
|
|
|
|
}
|
|
|
|
void getTimingInfo(unsigned long long &s, unsigned long long &tl) const {
|
|
|
|
s = start;
|
|
|
|
tl = timeLocked;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-12-03 17:50:09 +01:00
|
|
|
#if BOOST_VERSION >= 103500
|
2009-12-03 21:24:16 +01:00
|
|
|
//#if 0
|
2009-12-03 19:48:45 +01:00
|
|
|
class MongoMutex {
|
2009-12-03 19:12:51 +01:00
|
|
|
MutexInfo _minfo;
|
2009-12-03 19:48:45 +01:00
|
|
|
boost::shared_mutex _m;
|
|
|
|
ThreadLocalValue<int> _state;
|
2010-01-02 17:01:47 +01:00
|
|
|
|
|
|
|
/* 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;
|
2009-11-28 19:57:30 +01:00
|
|
|
public:
|
2009-12-06 05:46:00 +01:00
|
|
|
/**
|
|
|
|
* @return
|
|
|
|
* > 0 write lock
|
|
|
|
* = 0 no lock
|
|
|
|
* < 0 read lock
|
|
|
|
*/
|
|
|
|
int getState(){ return _state.get(); }
|
2010-01-02 17:01:47 +01:00
|
|
|
void assertWriteLocked() {
|
|
|
|
assert( getState() > 0 );
|
|
|
|
DEV assert( !_releasedEarly.get() );
|
|
|
|
}
|
2009-12-04 22:45:44 +01:00
|
|
|
bool atLeastReadLocked() { return _state.get() != 0; }
|
|
|
|
void assertAtLeastReadLocked() { assert(atLeastReadLocked()); }
|
2010-01-02 17:01:47 +01:00
|
|
|
|
2009-11-28 19:57:30 +01:00
|
|
|
void lock() {
|
2009-12-03 19:48:45 +01:00
|
|
|
DEV cout << "LOCK" << endl;
|
2009-12-03 20:14:06 +01:00
|
|
|
int s = _state.get();
|
|
|
|
if( s > 0 ) {
|
|
|
|
_state.set(s+1);
|
|
|
|
return;
|
|
|
|
}
|
2009-12-28 22:43:43 +01:00
|
|
|
massert( 10293 , "internal error: locks are not upgradeable", s == 0 );
|
2009-12-03 20:14:06 +01:00
|
|
|
_state.set(1);
|
2009-12-03 19:48:45 +01:00
|
|
|
_m.lock();
|
2009-12-03 19:12:51 +01:00
|
|
|
_minfo.entered();
|
2009-11-28 19:57:30 +01:00
|
|
|
}
|
2009-12-02 15:39:17 +01:00
|
|
|
void unlock() {
|
2009-12-03 19:48:45 +01:00
|
|
|
DEV cout << "UNLOCK" << endl;
|
2009-12-03 20:14:06 +01:00
|
|
|
int s = _state.get();
|
|
|
|
if( s > 1 ) {
|
|
|
|
_state.set(s-1);
|
|
|
|
return;
|
|
|
|
}
|
2010-01-02 17:01:47 +01:00
|
|
|
if( s != 1 ) {
|
|
|
|
if( _releasedEarly.get() ) {
|
|
|
|
_releasedEarly.set(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
assert(false); // attempt to unlock when wasn't in a write lock
|
|
|
|
}
|
2009-12-03 20:14:06 +01:00
|
|
|
_state.set(0);
|
2009-12-03 19:12:51 +01:00
|
|
|
_minfo.leaving();
|
2009-12-03 19:48:45 +01:00
|
|
|
_m.unlock();
|
2009-12-02 15:39:17 +01:00
|
|
|
}
|
2010-01-02 17:01:47 +01:00
|
|
|
|
|
|
|
/* 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();
|
|
|
|
}
|
|
|
|
|
2009-12-02 15:39:17 +01:00
|
|
|
void lock_shared() {
|
2009-12-03 19:48:45 +01:00
|
|
|
DEV cout << " LOCKSHARED" << endl;
|
2009-12-03 20:14:06 +01:00
|
|
|
int s = _state.get();
|
2009-12-04 17:18:42 +01:00
|
|
|
if( s ) {
|
|
|
|
if( s > 0 ) {
|
|
|
|
// already in write lock - just be recursive and stay write locked
|
|
|
|
_state.set(s+1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// already in read lock - recurse
|
|
|
|
_state.set(s-1);
|
|
|
|
return;
|
|
|
|
}
|
2009-12-03 20:14:06 +01:00
|
|
|
}
|
|
|
|
_state.set(-1);
|
2009-12-03 19:48:45 +01:00
|
|
|
_m.lock_shared();
|
2009-12-02 15:39:17 +01:00
|
|
|
}
|
|
|
|
void unlock_shared() {
|
2009-12-03 19:48:45 +01:00
|
|
|
DEV cout << " UNLOCKSHARED" << endl;
|
2009-12-03 20:14:06 +01:00
|
|
|
int s = _state.get();
|
2009-12-04 17:18:42 +01:00
|
|
|
if( s > 0 ) {
|
|
|
|
assert( s > 1 ); /* we must have done a lock write first to have s > 1 */
|
2009-12-03 20:14:06 +01:00
|
|
|
_state.set(s-1);
|
|
|
|
return;
|
|
|
|
}
|
2009-12-04 17:18:42 +01:00
|
|
|
if( s < -1 ) {
|
|
|
|
_state.set(s+1);
|
|
|
|
return;
|
|
|
|
}
|
2009-12-03 20:14:06 +01:00
|
|
|
assert( s == -1 );
|
|
|
|
_state.set(0);
|
2009-12-03 19:48:45 +01:00
|
|
|
_m.unlock_shared();
|
2009-12-02 15:39:17 +01:00
|
|
|
}
|
2009-12-03 19:12:51 +01:00
|
|
|
MutexInfo& info() { return _minfo; }
|
2009-11-28 19:57:30 +01:00
|
|
|
};
|
2009-11-28 17:50:46 +01:00
|
|
|
#else
|
|
|
|
/* this will be for old versions of boost */
|
2009-12-03 17:50:09 +01:00
|
|
|
class MongoMutex {
|
2009-12-03 19:14:41 +01:00
|
|
|
MutexInfo _minfo;
|
2009-11-28 17:50:46 +01:00
|
|
|
boost::recursive_mutex m;
|
2010-01-02 17:01:47 +01:00
|
|
|
ThreadLocalValue<bool> _releasedEarly;
|
2009-11-28 17:50:46 +01:00
|
|
|
public:
|
2009-12-03 19:14:41 +01:00
|
|
|
MongoMutex() { }
|
2009-11-28 17:50:46 +01:00
|
|
|
void lock() {
|
2009-12-03 21:24:16 +01:00
|
|
|
#if BOOST_VERSION >= 103500
|
|
|
|
m.lock();
|
|
|
|
#else
|
2009-11-28 17:50:46 +01:00
|
|
|
boost::detail::thread::lock_ops<boost::recursive_mutex>::lock(m);
|
2009-12-03 21:24:16 +01:00
|
|
|
#endif
|
2009-12-03 19:12:51 +01:00
|
|
|
_minfo.entered();
|
2009-11-28 17:50:46 +01:00
|
|
|
}
|
|
|
|
|
2010-01-02 17:01:47 +01:00
|
|
|
void releaseEarly() {
|
|
|
|
assertWriteLocked(); // aso must not be recursive, although we don't verify that in the old boost version
|
|
|
|
assert( !_releasedEarly.get() );
|
|
|
|
_releasedEarly.set(true);
|
|
|
|
_unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void _unlock() {
|
2009-12-03 19:12:51 +01:00
|
|
|
_minfo.leaving();
|
2009-12-03 21:24:16 +01:00
|
|
|
#if BOOST_VERSION >= 103500
|
|
|
|
m.unlock();
|
|
|
|
#else
|
2009-11-28 17:50:46 +01:00
|
|
|
boost::detail::thread::lock_ops<boost::recursive_mutex>::unlock(m);
|
2009-12-03 21:24:16 +01:00
|
|
|
#endif
|
2009-11-28 17:50:46 +01:00
|
|
|
}
|
2010-01-02 17:01:47 +01:00
|
|
|
void unlock() {
|
|
|
|
if( _releasedEarly.get() ) {
|
|
|
|
_releasedEarly.set(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_unlock();
|
|
|
|
}
|
2009-11-28 17:50:46 +01:00
|
|
|
|
|
|
|
void lock_shared() { lock(); }
|
|
|
|
void unlock_shared() { unlock(); }
|
2009-12-03 19:12:51 +01:00
|
|
|
MutexInfo& info() { return _minfo; }
|
2009-12-04 17:18:42 +01:00
|
|
|
void assertWriteLocked() {
|
|
|
|
assert( info().isLocked() );
|
|
|
|
}
|
|
|
|
void assertAtLeastReadLocked() {
|
|
|
|
assert( info().isLocked() );
|
|
|
|
}
|
2009-12-04 22:45:44 +01:00
|
|
|
bool atLeastReadLocked() { return info().isLocked(); }
|
2009-12-06 05:47:24 +01:00
|
|
|
int getState(){ return info().isLocked() ? 1 : 0; }
|
2009-11-28 17:50:46 +01:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
extern MongoMutex &dbMutex;
|
2009-12-02 15:39:17 +01:00
|
|
|
|
2009-11-27 22:40:58 +01:00
|
|
|
void dbunlocking_write();
|
|
|
|
void dbunlocking_read();
|
2009-08-25 22:14:55 +02:00
|
|
|
|
2009-11-28 17:50:46 +01:00
|
|
|
struct writelock {
|
|
|
|
writelock(const string& ns) {
|
|
|
|
dbMutex.lock();
|
2009-11-09 21:21:16 +01:00
|
|
|
}
|
|
|
|
~writelock() {
|
2009-11-27 22:40:58 +01:00
|
|
|
dbunlocking_write();
|
2009-11-28 17:50:46 +01:00
|
|
|
dbMutex.unlock();
|
2009-11-09 21:21:16 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-11-28 17:50:46 +01:00
|
|
|
struct readlock {
|
|
|
|
readlock(const string& ns) {
|
|
|
|
dbMutex.lock_shared();
|
2009-11-09 21:21:16 +01:00
|
|
|
}
|
|
|
|
~readlock() {
|
2009-11-27 22:40:58 +01:00
|
|
|
dbunlocking_read();
|
2009-11-28 17:50:46 +01:00
|
|
|
dbMutex.unlock_shared();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-11-28 19:57:30 +01:00
|
|
|
class mongolock {
|
|
|
|
bool _writelock;
|
|
|
|
public:
|
|
|
|
mongolock(bool write) : _writelock(write) {
|
2009-11-28 20:08:31 +01:00
|
|
|
if( _writelock ) {
|
2009-11-28 19:57:30 +01:00
|
|
|
dbMutex.lock();
|
2009-11-28 20:08:31 +01:00
|
|
|
}
|
2009-11-28 19:57:30 +01:00
|
|
|
else
|
|
|
|
dbMutex.lock_shared();
|
|
|
|
}
|
|
|
|
~mongolock() {
|
|
|
|
if( _writelock ) {
|
|
|
|
dbunlocking_write();
|
|
|
|
dbMutex.unlock();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
dbunlocking_read();
|
|
|
|
dbMutex.unlock_shared();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* this unlocks, does NOT upgrade. that works for our current usage */
|
2009-12-03 17:50:09 +01:00
|
|
|
void releaseAndWriteLock();
|
2009-11-28 19:57:30 +01:00
|
|
|
};
|
|
|
|
|
2009-11-28 17:50:46 +01:00
|
|
|
/* use writelock and readlock instead */
|
|
|
|
struct dblock : public writelock {
|
|
|
|
dblock() : writelock("") { }
|
|
|
|
~dblock() {
|
2009-11-09 21:21:16 +01:00
|
|
|
}
|
|
|
|
};
|
2009-08-25 22:14:55 +02:00
|
|
|
|
2009-12-04 17:18:42 +01:00
|
|
|
// eliminate
|
|
|
|
inline void assertInWriteLock() { dbMutex.assertWriteLocked(); }
|
2009-10-25 05:47:31 +01:00
|
|
|
|
|
|
|
}
|