Skip to content

Commit

Permalink
Refactor PriorityThreadPool loop slightly
Browse files Browse the repository at this point in the history
Summary: Addresses previous comment.

Reviewed By: NTillmann

Differential Revision: D44852490

fbshipit-source-id: 7e7369a0c88078b1437ae078952155d052a827cc
  • Loading branch information
agampe authored and facebook-github-bot committed Apr 13, 2023
1 parent 2e7b1bc commit 8e68fa3
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions libredex/PriorityThreadPool.h
Expand Up @@ -130,10 +130,20 @@ class PriorityThreadPool {

private:
void run() {
for (;;) {
for (bool first = true;; first = false) {
auto highest_priority_f =
[&]() -> boost::optional<std::function<void()>> {
std::unique_lock<std::mutex> lock{m_mutex};

// Notify when *all* work is done, i.e. nothing is running or pending.
//
// Moving this check here from the end of the loop avoids
// potential repeated lock acquisition.
if (!first && m_pending_work_items.empty() &&
m_running_work_items == 0) {
m_done_condition.notify_one();
}

// Wait for work or shutdown.
m_work_condition.wait(lock, [&]() {
return !m_pending_work_items.empty() || m_shutdown;
Expand Down Expand Up @@ -167,15 +177,7 @@ class PriorityThreadPool {
throw;
}

// Notify when *all* work is done, i.e. nothing is running or pending.
{
if (--m_running_work_items == 0) {
std::unique_lock<std::mutex> lock{m_mutex};
if (m_running_work_items == 0 && m_pending_work_items.empty()) {
m_done_condition.notify_one();
}
}
}
--m_running_work_items;
}
}
};

0 comments on commit 8e68fa3

Please sign in to comment.