From d28da66e40a06f6616ad14b957bccaa8868679db Mon Sep 17 00:00:00 2001 From: seelabs Date: Mon, 18 Jul 2022 11:59:08 -0400 Subject: [PATCH] [fold] minor cleanups in database stopping --- src/ripple/nodestore/impl/Database.cpp | 37 +++++++++++++------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/src/ripple/nodestore/impl/Database.cpp b/src/ripple/nodestore/impl/Database.cpp index ba07fa8a5e1..dab0ca3e2a3 100644 --- a/src/ripple/nodestore/impl/Database.cpp +++ b/src/ripple/nodestore/impl/Database.cpp @@ -68,31 +68,33 @@ Database::Database( decltype(read_) read; - while (!isStopping()) + while (1) { { std::unique_lock lock(readLock_); + if (isStopping()) + break; + // We need to check whether we're stopping again to // avoid a race condition that can stall this thread // during shutdown. - if (read_.empty() && !isStopping()) + if (read_.empty()) { runningThreads_--; readCondVar_.wait(lock); runningThreads_++; } - // If we are not stopping then extract multiple object - // at a time to minimize the overhead of acquiring the - // mutex. - if (!isStopping()) - { - for (int cnt = 0; - !read_.empty() && cnt != requestBundle_; - ++cnt) - read.insert(read_.extract(read_.begin())); - } + if (isStopping()) + break; + + // extract multiple object at a time to minimize the + // overhead of acquiring the mutex. + for (int cnt = 0; + !read_.empty() && cnt != requestBundle_; + ++cnt) + read.insert(read_.extract(read_.begin())); } for (auto it = read.begin(); it != read.end(); ++it) @@ -201,15 +203,12 @@ Database::asyncFetch( std::uint32_t ledgerSeq, std::function const&)>&& cb) { + std::lock_guard lock(readLock_); + if (!isStopping()) { - std::lock_guard lock(readLock_); - - if (!isStopping()) - { - read_[hash].emplace_back(ledgerSeq, std::move(cb)); - readCondVar_.notify_one(); - } + read_[hash].emplace_back(ledgerSeq, std::move(cb)); + readCondVar_.notify_one(); } }