简体   繁体   English

为什么互斥锁会在 LevelDB 中锁定两次?

[英]Why does the mutex lock twice in LevelDB?

I'm learning the levelDB C++ project, here is the situation, Status s = Write(WriteOptions(), nullptr) triggers a compaction work, and then enter the while loop to wait the signal, the compaction thread goes to BackgroundCall method, and it also needs to lock the mutex_ , I'm not sure the TEST_CompactMemTable still holds the mutex, so I print the debug message in the Lock and Unlock method.本人在学习levelDB C++项目,情况如下, Status s = Write(WriteOptions(), nullptr)触发compaction工作,然后进入while循环等待信号,compaction线程去BackgroundCall方法,它还需要锁定mutex_ ,我不确定TEST_CompactMemTable仍然持有互斥锁,所以我在Lock and Unlock方法中打印了调试消息。 The output is just like:输出就像:

TEST_CompactMemTable mutex lock
BackgroundCallmutex lock
BackgroundCallmutex unlock
TEST_CompactMemTable mutex unlock

I'm confused why does the mutex lock twice?我很困惑为什么互斥锁会锁定两次? Am i missing something, any help would be greatly appreciated.我错过了什么,任何帮助将不胜感激。


Status DBImpl::TEST_CompactMemTable() {
  Status s = Write(WriteOptions(), nullptr);
  if (s.ok()) {
    // lock the mutex first
    MutexLock l(&mutex_);
    while (imm_ != nullptr && bg_error_.ok()) {
      background_work_finished_signal_.Wait();
    }
    if (imm_ != nullptr) {
      s = bg_error_;
    }
  }
  return s;
}
void DBImpl::BackgroundCall() {
  // lock the mutex twice
  MutexLock l(&mutex_);
  assert(background_compaction_scheduled_);
  if (shutting_down_.load(std::memory_order_acquire)) {
  } else if (!bg_error_.ok()) {
  } else {
    BackgroundCompaction();
  }

  background_compaction_scheduled_ = false;

  MaybeScheduleCompaction();
  background_work_finished_signal_.SignalAll();
}

This is simple background_work_finished_signal_ is conditional variable which is associated with a mutex_ .这是简单的background_work_finished_signal_是与mutex_关联的条件变量。

This is done during construction: see DBImpl::DBImpl这是在构建过程中完成的: 参见 DBImpl::DBImpl

DBImpl::DBImpl(const Options& raw_options, const std::string& dbname)
    : env_(raw_options.env),
      internal_comparator_(raw_options.comparator),
      internal_filter_policy_(raw_options.filter_policy),
      options_(SanitizeOptions(dbname, &internal_comparator_,
                               &internal_filter_policy_, raw_options)),
      owns_info_log_(options_.info_log != raw_options.info_log),
      owns_cache_(options_.block_cache != raw_options.block_cache),
      dbname_(dbname),
      table_cache_(new TableCache(dbname_, options_, TableCacheSize(options_))),
      db_lock_(nullptr),
      shutting_down_(false),
      background_work_finished_signal_(&mutex_),
      mem_(nullptr),
      imm_(nullptr),
      has_imm_(false),
      logfile_(nullptr),
      logfile_number_(0),
      log_(nullptr),
      seed_(0),
      tmp_batch_(new WriteBatch),
      background_compaction_scheduled_(false),
      manual_compaction_(nullptr),
      versions_(new VersionSet(dbname_, &options_, table_cache_,
                               &internal_comparator_)) {}

Now when background_work_finished_signal_.Wait();现在当background_work_finished_signal_.Wait(); is called mutex have to be released, so other thread can lock it and send notifications.被称为 mutex 必须被释放,所以其他线程可以锁定它并发送通知。 When notification is received lock is restored before background_work_finished_signal_.Wait();background_work_finished_signal_.Wait();之前收到通知时恢复锁background_work_finished_signal_.Wait(); returns control.返回控制。

So basically those logs of yours are from different threads and mutex is unlock and lock by background_work_finished_signal_.Wait();所以基本上你的那些日志来自不同的线程,互斥锁由background_work_finished_signal_.Wait();解锁和锁定background_work_finished_signal_.Wait(); and your logs do not spot it.并且您的日志没有发现它。

So infact your logs should print something like this:所以事实上你的日志应该打印如下内容:

TEST_CompactMemTable mutex lock
TEST_CompactMemTable_wait_cv mutex unlock
BackgroundCallmutex lock
BackgroundCallmutex unlock
TEST_CompactMemTable_wait_cv mutex lock
TEST_CompactMemTable mutex unlock

When the code execution goes to background_work_finished_signal_.Wait() the mutex gets unlocked and gets locked again after the signal raised.当代码执行到background_work_finished_signal_.Wait() ,互斥锁被解锁并在信号发出后再次被锁定。 You don't see it, because you don't log messages in the mutex methods.您看不到它,因为您没有在互斥方法中记录消息。 The real flow should be真正的流量应该是

TEST_CompactMemTable mutex lock
signal wait
TEST_CompactMemTable mutex unlock
BackgroundCallmutex mutex lock
signal raised
BackgroundCallmutex mutex unlock
TEST_CompactMemTable mutex lock
TEST_CompactMemTable mutex unlock

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM