DiscordCoreAPI
A Discord bot library written in C++, with custom asynchronous coroutines.
Loading...
Searching...
No Matches
CoRoutineThreadPool.hpp
Go to the documentation of this file.
1/*
2 MIT License
3
4 DiscordCoreAPI, A bot library for Discord, written in C++, and featuring explicit multithreading through the usage of custom, asynchronous C++ CoRoutines.
5
6 Copyright 2022, 2023 Chris M. (RealTimeChris)
7
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this software and associated documentation files (the "Software"), to deal
10 in the Software without restriction, including without limitation the rights
11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 copies of the Software, and to permit persons to whom the Software is
13 furnished to do so, subject to the following conditions:
14
15 The above copyright notice and this permission notice shall be included in all
16 copies or substantial portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 SOFTWARE.
25*/
26/// CoRoutineThreadPool.hpp - Header for the "co_routine_thread_pool" related stuff.
27/// Dec 18, 2021
28/// https://discordcoreapi.com
29/// \file CoRoutineThreadPool.hpp
30#pragma once
31
33#include <coroutine>
34#include <deque>
35
36using namespace std::literals;
37
38namespace discord_core_api {
39
40 namespace discord_core_internal {
41
42 /**
43 * \addtogroup discord_core_internal
44 * @{
45 */
46
47 /// @brief A struct representing a worker thread for coroutine-based tasks.
49 inline worker_thread() = default;
50
51 inline worker_thread& operator=(worker_thread&& other) noexcept {
52 this->areWeCurrentlyWorking.store(other.areWeCurrentlyWorking.load(std::memory_order_acquire), std::memory_order_release);
53 std::swap(this->thread, other.thread);
54 tasks = std::move(other.tasks);
55 return *this;
56 }
57
58 inline worker_thread(worker_thread&& other) noexcept {
59 *this = std::move(other);
60 }
61
62 inline ~worker_thread() = default;
63
65 std::atomic_bool areWeCurrentlyWorking{};///< Atomic flag indicating if the thread is working.
66 std::jthread thread{};///< Joinable thread.
67 };
68
69 /// @brief A class representing a coroutine-based thread pool.
70 class co_routine_thread_pool : protected discord_core_api::unordered_map<uint64_t, unique_ptr<worker_thread>> {
71 public:
72 using map_type = discord_core_api::unordered_map<uint64_t, unique_ptr<worker_thread>>;
73
74 /// @brief Constructor to create a coroutine thread pool. initializes the worker threads.
75 inline co_routine_thread_pool() : threadCount(std::thread::hardware_concurrency()) {
76 for (uint32_t x = 0; x < threadCount; ++x) {
77 currentIndex.fetch_add(1, std::memory_order_release);
78 currentCount.fetch_add(1, std::memory_order_release);
79 uint64_t indexNew = currentIndex.load(std::memory_order_acquire);
80 getMap().emplace(indexNew, makeUnique<worker_thread>());
81 getMap()[indexNew]->thread = std::jthread([=, this](std::stop_token tokenNew) mutable {
82 threadFunction(getMap().at(indexNew).get(), tokenNew);
83 });
84 }
85 }
86
87 /// @brief Submit a coroutine task to the thread pool.
88 /// @param coro the coroutine handle to submit.
89 inline void submitTask(std::coroutine_handle<> coro) {
90 bool areWeAllBusy{ true };
91 uint64_t currentLowestValue{ std::numeric_limits<uint64_t>::max() };
92 uint64_t currentLowestIndex{ std::numeric_limits<uint64_t>::max() };
93 std::shared_lock lock01{ workerAccessMutex };
94 for (auto& [key, value]: getMap()) {
95 if (!value->areWeCurrentlyWorking.load(std::memory_order_acquire)) {
96 areWeAllBusy = false;
97 if (value->tasks.size() < currentLowestValue) {
98 currentLowestValue = value->tasks.size();
99 currentLowestIndex = key;
100 }
101 break;
102 }
103 }
104 if (areWeAllBusy) {
105 currentIndex.fetch_add(1, std::memory_order_release);
106 currentCount.fetch_add(1, std::memory_order_release);
107 uint64_t indexNew = currentIndex.load(std::memory_order_acquire);
108 lock01.unlock();
109 std::unique_lock lock02{ workerAccessMutex };
110 getMap().emplace(indexNew, makeUnique<worker_thread>());
111 getMap()[indexNew]->thread = std::jthread([=, this](std::stop_token tokenNew) mutable {
112 threadFunction(getMap().at(indexNew).get(), tokenNew);
113 });
114 lock02.unlock();
115 } else {
116 getMap()[currentLowestIndex]->tasks.send(std::move(coro));
117 }
118 }
119
121 doWeQuit.store(true, std::memory_order_release);
122 }
123
124 protected:
125 std::shared_mutex workerAccessMutex{};///< Shared mutex for worker thread access.
126 std::atomic_uint64_t currentCount{};///< current count of worker threads.
127 std::atomic_uint64_t currentIndex{};///< current index of worker threads.
128 std::atomic_bool doWeQuit{ false };///< Whether or not we're quitting.
129 const uint64_t threadCount{};///< Total thread count.
130
131 /// @brief Thread function for each worker thread.
132 /// @param thread A pointer to the current thread of execution.
133 /// @param tokenNew The stop token for the thread.
134 inline void threadFunction(worker_thread* thread, std::stop_token tokenNew) {
135 while (!doWeQuit.load(std::memory_order_acquire) && !tokenNew.stop_requested()) {
136 std::coroutine_handle<> coroHandle{};
137 if (thread->tasks.tryReceive(coroHandle)) {
138 thread->areWeCurrentlyWorking.store(true, std::memory_order_release);
139 try {
140 coroHandle();
141 while (!coroHandle.done()) {
142 std::this_thread::sleep_for(1ms);
143 }
144 } catch (const std::runtime_error& error) {
145 message_printer::printError<print_message_type::general>(error.what());
146 }
147 thread->areWeCurrentlyWorking.store(false, std::memory_order_release);
148 }
149 if (currentCount.load(std::memory_order_acquire) > threadCount) {
150 uint64_t extraWorkers{ currentCount.load(std::memory_order_acquire) - threadCount };
151 while (extraWorkers > 0) {
152 --extraWorkers;
153 size_type currentHighestIndex{};
154 for (const auto& [key, value]: *this) {
155 if (key > currentHighestIndex) {
156 currentHighestIndex = key;
157 }
158 }
159 std::unique_lock lock{ workerAccessMutex };
160 auto oldThread = begin() + currentHighestIndex;
161 if (oldThread->second->thread.joinable()) {
162 oldThread->second->thread.request_stop();
163 oldThread->second->thread.detach();
164 currentCount.fetch_sub(1, std::memory_order_release);
165 getMap().erase(oldThread->first);
166 }
167 }
168 }
169 std::this_thread::sleep_for(std::chrono::nanoseconds{ 100000 });
170 }
171 }
172
173 inline map_type& getMap() {
174 return *this;
175 }
176 };
177
178 /**@}*/
179 }
180}
A class representing a coroutine-based thread pool.
std::shared_mutex workerAccessMutex
Shared mutex for worker thread access.
void threadFunction(worker_thread *thread, std::stop_token tokenNew)
Thread function for each worker thread.
std::atomic_bool doWeQuit
Whether or not we're quitting.
co_routine_thread_pool()
Constructor to create a coroutine thread pool. initializes the worker threads.
std::atomic_uint64_t currentIndex
current index of worker threads.
std::atomic_uint64_t currentCount
current count of worker threads.
void submitTask(std::coroutine_handle<> coro)
Submit a coroutine task to the thread pool.
A thread-safe messaging block for data-structures.
The main namespace for the forward-facing interfaces.
A struct representing a worker thread for coroutine-based tasks.
unbounded_message_block< std::coroutine_handle<> > tasks
Queue of coroutine tasks.
std::atomic_bool areWeCurrentlyWorking
Atomic flag indicating if the thread is working.