From: Joel Rosdahl Date: Tue, 26 May 2020 19:57:42 +0000 (+0200) Subject: Move ThreadPool methods to .cpp file X-Git-Tag: v4.0~429 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=487eb73d73698819fcfe320e3e280a7ee529f03d;p=thirdparty%2Fccache.git Move ThreadPool methods to .cpp file --- diff --git a/Makefile.in b/Makefile.in index f7ff91ad8..8831ddebc 100644 --- a/Makefile.in +++ b/Makefile.in @@ -49,6 +49,7 @@ non_third_party_sources = \ src/ProgressBar.cpp \ src/SignalHandler.cpp \ src/Stat.cpp \ + src/ThreadPool.cpp \ src/Util.cpp \ src/ZstdCompressor.cpp \ src/ZstdDecompressor.cpp \ diff --git a/src/ThreadPool.cpp b/src/ThreadPool.cpp new file mode 100644 index 000000000..a1d931a33 --- /dev/null +++ b/src/ThreadPool.cpp @@ -0,0 +1,84 @@ +// Copyright (C) 2019-2020 Joel Rosdahl and other contributors +// +// See doc/AUTHORS.adoc for a complete list of contributors. +// +// This program is free software; you can redistribute it and/or modify it +// under the terms of the GNU General Public License as published by the Free +// Software Foundation; either version 3 of the License, or (at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +// more details. +// +// You should have received a copy of the GNU General Public License along with +// this program; if not, write to the Free Software Foundation, Inc., 51 +// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +#include "ThreadPool.hpp" + +ThreadPool::ThreadPool(size_t number_of_threads, size_t task_queue_max_size) + : m_task_queue_max_size(task_queue_max_size) +{ + m_worker_threads.reserve(number_of_threads); + for (size_t i = 0; i < number_of_threads; ++i) { + m_worker_threads.emplace_back(&ThreadPool::worker_thread_main, this); + } +} + +ThreadPool::~ThreadPool() +{ + shut_down(); +} + +void +ThreadPool::enqueue(std::function function) +{ + { + std::unique_lock lock(m_mutex); + if (m_task_queue.size() >= m_task_queue_max_size) { + m_task_popped_condition.wait( + lock, [this] { return m_task_queue.size() < m_task_queue_max_size; }); + } + m_task_queue.emplace(function); + } + m_task_enqueued_or_shutting_down_condition.notify_one(); +} + +void +ThreadPool::shut_down() +{ + { + std::unique_lock lock(m_mutex); + m_shutting_down = true; + } + m_task_enqueued_or_shutting_down_condition.notify_all(); + for (auto& thread : m_worker_threads) { + if (thread.joinable()) { + thread.join(); + } + } +} + +void +ThreadPool::worker_thread_main() +{ + while (true) { + std::function task; + + { + std::unique_lock lock(m_mutex); + m_task_enqueued_or_shutting_down_condition.wait( + lock, [this] { return m_shutting_down || !m_task_queue.empty(); }); + if (m_shutting_down && m_task_queue.empty()) { + return; + } + task = std::move(m_task_queue.front()); + m_task_queue.pop(); + } + + m_task_popped_condition.notify_all(); + task(); + } +} diff --git a/src/ThreadPool.hpp b/src/ThreadPool.hpp index ea1f41a44..5eee38182 100644 --- a/src/ThreadPool.hpp +++ b/src/ThreadPool.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2019 Joel Rosdahl and other contributors +// Copyright (C) 2019-2020 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -49,68 +49,3 @@ private: void worker_thread_main(); }; - -ThreadPool::ThreadPool(size_t number_of_threads, size_t task_queue_max_size) - : m_task_queue_max_size(task_queue_max_size) -{ - m_worker_threads.reserve(number_of_threads); - for (size_t i = 0; i < number_of_threads; ++i) { - m_worker_threads.emplace_back(&ThreadPool::worker_thread_main, this); - } -} - -ThreadPool::~ThreadPool() -{ - shut_down(); -} - -void -ThreadPool::enqueue(std::function function) -{ - { - std::unique_lock lock(m_mutex); - if (m_task_queue.size() >= m_task_queue_max_size) { - m_task_popped_condition.wait( - lock, [this] { return m_task_queue.size() < m_task_queue_max_size; }); - } - m_task_queue.emplace(function); - } - m_task_enqueued_or_shutting_down_condition.notify_one(); -} - -void -ThreadPool::shut_down() -{ - { - std::unique_lock lock(m_mutex); - m_shutting_down = true; - } - m_task_enqueued_or_shutting_down_condition.notify_all(); - for (auto& thread : m_worker_threads) { - if (thread.joinable()) { - thread.join(); - } - } -} - -void -ThreadPool::worker_thread_main() -{ - while (true) { - std::function task; - - { - std::unique_lock lock(m_mutex); - m_task_enqueued_or_shutting_down_condition.wait( - lock, [this] { return m_shutting_down || !m_task_queue.empty(); }); - if (m_shutting_down && m_task_queue.empty()) { - return; - } - task = std::move(m_task_queue.front()); - m_task_queue.pop(); - } - - m_task_popped_condition.notify_all(); - task(); - } -}