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

Why does the mutex lock twice in LevelDB?

我正在学习levelDB C++项目,这里是这样的情况,Status s = Write(WriteOptions(), nullptr)触发一个compaction工作,然后进入while循环等待信号,compaction线程去BackgroundCall 方法,它还需要锁定 mutex_,我不确定 TEST_CompactMemTable 是否仍然持有互斥锁,所以我在 LockUnlock方法。 输出就像:

TEST_CompactMemTable mutex lock
BackgroundCallmutex lock
BackgroundCallmutex unlock
TEST_CompactMemTable mutex unlock

我很困惑为什么互斥量会锁定两次?我是否遗漏了什么,将不胜感激。


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();
}

这很简单 background_work_finished_signal_ 是与 mutex_ 关联的条件变量。

这是在构建过程中完成的:see 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_)) {}

现在,当 background_work_finished_signal_.Wait(); 被调用时,必须释放互斥量,以便其他线程可以锁定它并发送通知。 收到通知后,锁会在 background_work_finished_signal_.Wait(); returns 控制之前恢复。

所以基本上你的那些日志来自不同的线程,互斥体被 background_work_finished_signal_.Wait(); 解锁和锁定,你的日志没有发现它。

所以事实上你的日志应该打印这样的东西:

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

当代码执行到 background_work_finished_signal_.Wait() 时,互斥锁被解锁并在信号发出后再次被锁定。您看不到它,因为您没有在互斥方法中记录消息。真实流量应该是

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