diff --git a/CMakeLists.txt b/CMakeLists.txt index 9b4b9ca..edd1fc8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,6 +25,7 @@ add_library(wormhole_lib src/config/config_db.cpp src/config/config_loader.cpp src/config/config_instance.cpp + src/type/thread_pool.cpp src/user.cpp src/control.cpp src/logger.cpp) diff --git a/src/http/request_sender.h b/src/http/request_sender.h index 559f397..df154a8 100644 --- a/src/http/request_sender.h +++ b/src/http/request_sender.h @@ -26,6 +26,9 @@ namespace HTTP { + /** + * @brief A request sender wrapping Curl Multi + */ class RequestSender { public: diff --git a/src/type/thread_pool.cpp b/src/type/thread_pool.cpp new file mode 100644 index 0000000..a2e4bab --- /dev/null +++ b/src/type/thread_pool.cpp @@ -0,0 +1,43 @@ +/* + * Wormhole - Federated social network + * Copyright (C) 2022 Nekobit + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include +#include "thread_pool.h" + +using namespace Threads; + +ThreadPool::ThreadPool(size_t size) + : pool{size}, + it_smallest_pool{pool.begin()} +{} + +// Sets `it_smallest_pool' to the smallest pool available (based on ThreadInfo::children) +void ThreadPool::set_smallest_pool() +{ + auto cur_pool = pool.begin(); + size_t count = cur_pool->children; + + for (auto i = pool.begin(); i != pool.end(); ++i) + if (i->children < count) + { + cur_pool = i; + count = i->children; + } + + it_smallest_pool = cur_pool; +} diff --git a/src/type/thread_pool.h b/src/type/thread_pool.h new file mode 100644 index 0000000..6dee2f5 --- /dev/null +++ b/src/type/thread_pool.h @@ -0,0 +1,58 @@ +/* + * Wormhole - Federated social network + * Copyright (C) 2022 Nekobit + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#pragma once +#include +#include + +namespace Threads +{ + class ThreadPool + { + struct ThreadInfo + { + int children; /// Represents how many "members" in a pool + std::thread thr; + }; + public: + using threadpool_t = std::vector; + + ThreadPool(size_t size); + ~ThreadPool() = default; + + template + void start(Function&& f, Args&&... args) + { + for (size_t i = 0; i < pool.size(); ++i) + { + pool[i].thr = std::thread{f, &args..., i}; + } + } + + /// Gets a pool with the least amount of children ("smallest" thread) + inline ThreadPool::ThreadInfo& select_pool() { return *it_smallest_pool; } + + inline size_t get_size() const noexcept { return pool.size(); } + private: + /// Only calculate the smallest pool when new pools are added + void set_smallest_pool(); + + threadpool_t pool; + threadpool_t::iterator it_smallest_pool; + }; +}