|
| 1 | +// Copyright (C) 2025 Category Labs, Inc. |
| 2 | +// |
| 3 | +// This program is free software: you can redistribute it and/or modify |
| 4 | +// it under the terms of the GNU General Public License as published by |
| 5 | +// the Free Software Foundation, either version 3 of the License, or |
| 6 | +// (at your option) any later version. |
| 7 | +// |
| 8 | +// This program is distributed in the hope that it will be useful, |
| 9 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | +// GNU General Public License for more details. |
| 12 | +// |
| 13 | +// You should have received a copy of the GNU General Public License |
| 14 | +// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | + |
| 16 | +#include <category/core/fiber/fiber_thread_pool.hpp> |
| 17 | + |
| 18 | +#include <category/core/assert.h> |
| 19 | +#include <category/core/fiber/config.hpp> |
| 20 | +#include <category/core/fiber/fiber_group.hpp> |
| 21 | +#include <category/core/fiber/priority_algorithm.hpp> |
| 22 | +#include <category/core/fiber/priority_properties.hpp> |
| 23 | + |
| 24 | +#include <boost/fiber/channel_op_status.hpp> |
| 25 | +#include <boost/fiber/fiber.hpp> |
| 26 | +#include <boost/fiber/operations.hpp> |
| 27 | +#include <boost/fiber/protected_fixedsize_stack.hpp> |
| 28 | + |
| 29 | +#include <atomic> |
| 30 | +#include <cstddef> |
| 31 | +#include <cstdio> |
| 32 | +#include <functional> |
| 33 | +#include <memory> |
| 34 | +#include <mutex> |
| 35 | +#include <thread> |
| 36 | +#include <utility> |
| 37 | +#include <vector> |
| 38 | + |
| 39 | +#include <pthread.h> |
| 40 | + |
| 41 | +MONAD_FIBER_NAMESPACE_BEGIN |
| 42 | + |
| 43 | +FiberThreadPool::FiberThreadPool( |
| 44 | + unsigned const n_threads, bool const prevent_spin) |
| 45 | + : prevent_spin_{prevent_spin} |
| 46 | +{ |
| 47 | + MONAD_ASSERT(n_threads); |
| 48 | + |
| 49 | + threads_.reserve(n_threads); |
| 50 | + |
| 51 | + // Create worker threads (1 through n_threads-1) that wait for work via |
| 52 | + // the shared queue with priority-based work-stealing scheduler. |
| 53 | + for (unsigned i = n_threads - 1; i > 0; --i) { |
| 54 | + auto thread = std::thread([this, i] { |
| 55 | + char name[16]; |
| 56 | + std::snprintf(name, 16, "ftpool %u", i); |
| 57 | + pthread_setname_np(pthread_self(), name); |
| 58 | + |
| 59 | + boost::fibers::use_scheduling_algorithm<PriorityAlgorithm>( |
| 60 | + queue_, prevent_spin_); |
| 61 | + |
| 62 | + std::unique_lock<boost::fibers::mutex> lock{mutex_}; |
| 63 | + cv_.wait(lock, [this] { return done_; }); |
| 64 | + }); |
| 65 | + threads_.push_back(std::move(thread)); |
| 66 | + } |
| 67 | + |
| 68 | + // Thread 0 runs a bootstrap fiber that handles fiber creation requests |
| 69 | + // from FiberGroup constructors. |
| 70 | + auto thread = std::thread([this] { |
| 71 | + pthread_setname_np(pthread_self(), "ftpool 0"); |
| 72 | + |
| 73 | + boost::fibers::use_scheduling_algorithm<PriorityAlgorithm>( |
| 74 | + queue_, prevent_spin_); |
| 75 | + |
| 76 | + auto *const properties = new PriorityProperties{nullptr}; |
| 77 | + boost::fibers::fiber bootstrap_fiber{ |
| 78 | + static_cast<boost::fibers::fiber_properties *>(properties), |
| 79 | + std::allocator_arg, |
| 80 | + boost::fibers::protected_fixedsize_stack{ |
| 81 | + static_cast<size_t>(8 * 1024 * 1024)}, |
| 82 | + [this] { |
| 83 | + std::function<void()> task; |
| 84 | + while (bootstrap_channel_.pop(task) == |
| 85 | + boost::fibers::channel_op_status::success) { |
| 86 | + task(); |
| 87 | + } |
| 88 | + }}; |
| 89 | + |
| 90 | + bootstrap_fiber.detach(); |
| 91 | + |
| 92 | + std::unique_lock<boost::fibers::mutex> lock{mutex_}; |
| 93 | + cv_.wait(lock, [this] { return done_; }); |
| 94 | + }); |
| 95 | + threads_.push_back(std::move(thread)); |
| 96 | +} |
| 97 | + |
| 98 | +FiberThreadPool::~FiberThreadPool() |
| 99 | +{ |
| 100 | + MONAD_ASSERT( |
| 101 | + active_groups_.load(std::memory_order_relaxed) == 0 && |
| 102 | + "All FiberGroup instances must be destroyed before FiberThreadPool"); |
| 103 | + |
| 104 | + bootstrap_channel_.close(); |
| 105 | + |
| 106 | + { |
| 107 | + std::unique_lock<boost::fibers::mutex> const lock{mutex_}; |
| 108 | + done_ = true; |
| 109 | + } |
| 110 | + |
| 111 | + cv_.notify_all(); |
| 112 | + |
| 113 | + while (threads_.size()) { |
| 114 | + auto &thread = threads_.back(); |
| 115 | + thread.join(); |
| 116 | + threads_.pop_back(); |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +std::unique_ptr<FiberGroup> |
| 121 | +FiberThreadPool::create_fiber_group(unsigned const n_fibers) |
| 122 | +{ |
| 123 | + MONAD_ASSERT(n_fibers); |
| 124 | + MONAD_ASSERT( |
| 125 | + !done_ && "Cannot create FiberGroup after FiberThreadPool shutdown"); |
| 126 | + |
| 127 | + return std::unique_ptr<FiberGroup>(new FiberGroup{*this, n_fibers}); |
| 128 | +} |
| 129 | + |
| 130 | +MONAD_FIBER_NAMESPACE_END |
0 commit comments