class Mutex {
private:
const char *name;
+ bool recursive;
+ bool lockdep;
+
pthread_mutex_t _m;
int nlock;
- bool recursive;
// don't allow copying.
void operator=(Mutex &M) {}
Mutex( const Mutex &M ) {}
public:
- Mutex(const char *n, bool r = true) : name(n), nlock(0), recursive(r) {
+ Mutex(const char *n, bool r = true, bool ld=true) : name(n), recursive(r), lockdep(ld), nlock(0) {
if (recursive) {
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
- pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE);
+ pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&_m,&attr);
pthread_mutexattr_destroy(&attr);
} else {
bool TryLock() {
int r = pthread_mutex_trylock(&_m);
if (r == 0) {
- if (g_lockdep) _locked();
+ if (lockdep && g_lockdep) _locked();
nlock++;
assert(nlock == 1 || recursive);
}
}
void Lock() {
- if (g_lockdep) _will_lock();
+ if (lockdep && g_lockdep) _will_lock();
int r = pthread_mutex_lock(&_m);
- if (g_lockdep) _locked();
+ if (lockdep && g_lockdep) _locked();
assert(r == 0);
nlock++;
assert(nlock == 1 || recursive);
--nlock;
int r = pthread_mutex_unlock(&_m);
assert(r == 0);
- if (g_lockdep) _unlocked();
+ if (lockdep && g_lockdep) _unlocked();
}
friend class Cond;
Mutex lock;
long nref;
public:
- atomic_t(int i=0) : lock("atomic_t::lock", false), nref(i) {}
+ atomic_t(int i=0) : lock("atomic_t::lock", false, false /* no lockdep */), nref(i) {}
atomic_t(const atomic_t& other);
void inc() {
lock.Lock();