0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-11-30 09:06:21 +01:00
mongodb/db/concurrency.h

225 lines
5.4 KiB
C
Raw Normal View History

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-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;
2009-11-28 19:57:30 +01:00
public:
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-03 22:41:12 +01:00
massert("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
}
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;
}
assert( s == 1 );
_state.set(0);
2009-12-03 19:12:51 +01:00
_minfo.leaving();
2009-12-03 19:48:45 +01:00
_m.unlock();
}
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();
assert( s >= 0 );
if( s > 0 ) {
// already in write lock - just be recursive and stay write locked
_state.set(s+1);
return;
}
_state.set(-1);
2009-12-03 19:48:45 +01:00
_m.lock_shared();
}
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();
if( s > 1 ) {
_state.set(s-1);
return;
}
assert( s == -1 );
_state.set(0);
2009-12-03 19:48:45 +01:00
_m.unlock_shared();
}
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;
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
}
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
}
void lock_shared() { lock(); }
void unlock_shared() { unlock(); }
2009-12-03 19:12:51 +01:00
MutexInfo& info() { return _minfo; }
2009-11-28 17:50:46 +01:00
};
#endif
extern MongoMutex &dbMutex;
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();
}
~writelock() {
2009-11-27 22:40:58 +01:00
dbunlocking_write();
2009-11-28 17:50:46 +01:00
dbMutex.unlock();
}
};
2009-11-28 17:50:46 +01:00
struct readlock {
readlock(const string& ns) {
dbMutex.lock_shared();
}
~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-27 22:40:58 +01:00
/* a scoped release of a mutex temporarily -- like a scopedlock but reversed.
2009-08-25 22:14:55 +02:00
*/
2009-11-28 17:50:46 +01:00
/*
2009-08-25 22:14:55 +02:00
struct temprelease {
boost::mutex& m;
temprelease(boost::mutex& _m) : m(_m) {
#if BOOST_VERSION >= 103500
m.unlock();
#else
boost::detail::thread::lock_ops<boost::mutex>::unlock(m);
#endif
}
~temprelease() {
#if BOOST_VERSION >= 103500
m.lock();
#else
boost::detail::thread::lock_ops<boost::mutex>::lock(m);
#endif
}
};
2009-11-28 17:50:46 +01:00
*/
2009-08-25 22:14:55 +02:00
2009-10-25 05:47:31 +01:00
inline void assertInWriteLock() {
/* TEMP assert( dbMutexInfo.isLocked() );
*/
2009-10-25 05:47:31 +01:00
}
}