Skip to content

Commit 954679b

Browse files
committed
AssertHeld() should do things
Summary: AssertHeld() was a no-op before. Now it does things. Also, this change caught a bad bug in SuperVersion::Init(). The method is calling db->mutex.AssertHeld(), but db variable is not initialized yet! I also fixed that issue. Test Plan: make check Reviewers: dhruba, haobo, ljin, sdong, yhchiang Reviewed By: haobo CC: leveldb Differential Revision: https://reviews.facebook.net/D17193
1 parent ad9a39c commit 954679b

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

db/db_impl.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -3331,10 +3331,10 @@ void DBImpl::InstallSuperVersion(DeletionState& deletion_state) {
33313331
DBImpl::SuperVersion* DBImpl::InstallSuperVersion(
33323332
SuperVersion* new_superversion) {
33333333
mutex_.AssertHeld();
3334+
new_superversion->db = this;
33343335
new_superversion->Init(mem_, imm_.current(), versions_->current());
33353336
SuperVersion* old_superversion = super_version_;
33363337
super_version_ = new_superversion;
3337-
super_version_->db = this;
33383338
++super_version_number_;
33393339
super_version_->version_number = super_version_number_;
33403340

port/port_posix.cc

+25-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include <cstdlib>
1313
#include <stdio.h>
14+
#include <assert.h>
1415
#include <string.h>
1516
#include "util/logging.h"
1617

@@ -45,9 +46,25 @@ Mutex::Mutex(bool adaptive) {
4546

4647
Mutex::~Mutex() { PthreadCall("destroy mutex", pthread_mutex_destroy(&mu_)); }
4748

48-
void Mutex::Lock() { PthreadCall("lock", pthread_mutex_lock(&mu_)); }
49+
void Mutex::Lock() {
50+
PthreadCall("lock", pthread_mutex_lock(&mu_));
51+
#ifndef NDEBUG
52+
locked_ = true;
53+
#endif
54+
}
55+
56+
void Mutex::Unlock() {
57+
#ifndef NDEBUG
58+
locked_ = false;
59+
#endif
60+
PthreadCall("unlock", pthread_mutex_unlock(&mu_));
61+
}
4962

50-
void Mutex::Unlock() { PthreadCall("unlock", pthread_mutex_unlock(&mu_)); }
63+
void Mutex::AssertHeld() {
64+
#ifndef NDEBUG
65+
assert(locked_);
66+
#endif
67+
}
5168

5269
CondVar::CondVar(Mutex* mu)
5370
: mu_(mu) {
@@ -57,7 +74,13 @@ CondVar::CondVar(Mutex* mu)
5774
CondVar::~CondVar() { PthreadCall("destroy cv", pthread_cond_destroy(&cv_)); }
5875

5976
void CondVar::Wait() {
77+
#ifndef NDEBUG
78+
mu_->locked_ = false;
79+
#endif
6080
PthreadCall("wait", pthread_cond_wait(&cv_, &mu_->mu_));
81+
#ifndef NDEBUG
82+
mu_->locked_ = true;
83+
#endif
6184
}
6285

6386
void CondVar::Signal() {

port/port_posix.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,16 @@ class Mutex {
9797

9898
void Lock();
9999
void Unlock();
100-
void AssertHeld() { }
100+
// this will assert if the mutex is not locked
101+
// it does NOT verify that mutex is held by a calling thread
102+
void AssertHeld();
101103

102104
private:
103105
friend class CondVar;
104106
pthread_mutex_t mu_;
107+
#ifndef NDEBUG
108+
bool locked_;
109+
#endif
105110

106111
// No copying
107112
Mutex(const Mutex&);

0 commit comments

Comments
 (0)