void AsyncConnectionPool::shutdown()

in squangle/mysql_client/AsyncConnectionPool.cpp [112:137]


void AsyncConnectionPool::shutdown() {
  VLOG(2) << "Shutting down";
  std::unique_lock<std::mutex> lock(shutdown_mutex_);
  // Will block adding anything to the pool
  shutting_down_ = true;

  // cancelTimeout can only be ran in the tevent thread
  if (std::this_thread::get_id() == mysql_client_->threadId()) {
    cleanup_timer_.cancelTimeout();
    conn_storage_.clearAll();
    finished_shutdown_.store(true, std::memory_order_relaxed);
    VLOG(1) << "Shutting down in tevent thread";
  } else {
    mysql_client_->runInThread([this]() {
      cleanup_timer_.cancelTimeout();
      conn_storage_.clearAll();
      // Reacquire lock
      std::unique_lock<std::mutex> shutdown_lock(shutdown_mutex_);
      finished_shutdown_.store(true, std::memory_order_relaxed);
      this->shutdown_condvar_.notify_one();
    });
    shutdown_condvar_.wait(lock, [this] {
      return finished_shutdown_.load(std::memory_order_acquire);
    });
  }
}