Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into 238-implement-s3-si…
Browse files Browse the repository at this point in the history
…nks-pt-4
  • Loading branch information
aliddell committed Jul 10, 2024
2 parents 9736225 + f0f767d commit 5037a38
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/common/thread.pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ common::ThreadPool::ThreadPool(unsigned int n_threads,
{
const unsigned int one = 1;
n_threads = std::clamp(
n_threads, one, std::max(std::thread::hardware_concurrency(), one));
n_threads, 1u, std::max(std::thread::hardware_concurrency(), 1u));

for (auto i = 0; i < n_threads; ++i) {
threads_.emplace_back([this] { thread_worker_(); });
}
}

common::ThreadPool::~ThreadPool() noexcept
{
{
Expand All @@ -28,18 +29,17 @@ common::ThreadPool::~ThreadPool() noexcept

await_stop();
}

void
common::ThreadPool::push_to_job_queue(JobT&& job)
{
{
std::unique_lock lock(jobs_mutex_);
CHECK(is_accepting_jobs_);

jobs_.push(std::move(job));
}
std::unique_lock lock(jobs_mutex_);
CHECK(is_accepting_jobs_);
jobs_.push(std::move(job));

cv_.notify_one();
}

void
common::ThreadPool::await_stop() noexcept
{
Expand All @@ -48,7 +48,8 @@ common::ThreadPool::await_stop() noexcept
is_accepting_jobs_ = false;
}

cv_.notify_all();
cv_.notify_all();
}

// spin down threads
for (auto& thread : threads_) {
Expand All @@ -57,6 +58,7 @@ common::ThreadPool::await_stop() noexcept
}
}
}

std::optional<common::ThreadPool::JobT>
common::ThreadPool::pop_from_job_queue_() noexcept
{
Expand All @@ -68,11 +70,13 @@ common::ThreadPool::pop_from_job_queue_() noexcept
jobs_.pop();
return job;
}

bool
common::ThreadPool::should_stop_() const noexcept
{
return !is_accepting_jobs_ && jobs_.empty();
}

void
common::ThreadPool::thread_worker_()
{
Expand Down

0 comments on commit 5037a38

Please sign in to comment.