diff --git a/include/dpp/dpp.h b/include/dpp/dpp.h index 88a5f41370..bbafecf9dd 100644 --- a/include/dpp/dpp.h +++ b/include/dpp/dpp.h @@ -76,3 +76,4 @@ #include #include #include +#include diff --git a/include/dpp/thread_pool.h b/include/dpp/thread_pool.h new file mode 100644 index 0000000000..65c7b9fc71 --- /dev/null +++ b/include/dpp/thread_pool.h @@ -0,0 +1,50 @@ +/************************************************************************************ + * + * D++, A Lightweight C++ library for Discord + * + * SPDX-License-Identifier: Apache-2.0 + * Copyright 2021 Craig Edwards and D++ contributors + * (https://github.com/brainboxdotcc/DPP/graphs/contributors) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ************************************************************************************/ + +#pragma once +#include +#include +#include +#include +#include +#include + +/** + * A task within a thread pool. A simple lambda that accepts no parameters and returns void. + */ +using thread_pool_task = std::function; + +/** + * @brief A thread pool contains 1 or more worker threads which accept thread_pool_task lambadas + * into a queue, which is processed in-order by whichever thread is free. + */ +struct thread_pool { + std::vector threads; + std::queue tasks; + std::mutex queue_mutex; + std::condition_variable cv; + bool stop{false}; + + explicit thread_pool(size_t num_threads = std::thread::hardware_concurrency()); + ~thread_pool(); + void enqueue(thread_pool_task task); +}; diff --git a/src/dpp/thread_pool.cpp b/src/dpp/thread_pool.cpp new file mode 100644 index 0000000000..b05f3a1df3 --- /dev/null +++ b/src/dpp/thread_pool.cpp @@ -0,0 +1,74 @@ +/************************************************************************************ + * + * D++, A Lightweight C++ library for Discord + * + * SPDX-License-Identifier: Apache-2.0 + * Copyright 2021 Craig Edwards and D++ contributors + * (https://github.com/brainboxdotcc/DPP/graphs/contributors) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ************************************************************************************/ + +#include +#include +#include + +thread_pool::thread_pool(size_t num_threads) { + for (size_t i = 0; i < num_threads; ++i) { + threads.emplace_back([this, i]() { + dpp::utility::set_thread_name("pool/exec/" + std::to_string(i)); + while (true) { + thread_pool_task task; + { + std::unique_lock lock(queue_mutex); + + cv.wait(lock, [this] { + return !tasks.empty() || stop; + }); + + if (stop && tasks.empty()) { + return; + } + + task = std::move(tasks.front()); + tasks.pop(); + } + + task(); + } + }); + } +} + +thread_pool::~thread_pool() +{ + { + std::unique_lock lock(queue_mutex); + stop = true; + } + + cv.notify_all(); + for (auto& thread : threads) { + thread.join(); + } +} + +void thread_pool::enqueue(thread_pool_task task) +{ + { + std::unique_lock lock(queue_mutex); + tasks.emplace(std::move(task)); + } + cv.notify_one(); +}