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 DCA_INLINE worker_thread(){};
50
51 DCA_INLINE worker_thread& operator=(worker_thread&& other) noexcept {
52 areWeCurrentlyWorking.store(other.areWeCurrentlyWorking.load(std::memory_order_acquire), std::memory_order_release);
53 std::swap(thread, other.thread);
54 tasks = std::move(other.tasks);
55 return *this;
56 }
57
58 DCA_INLINE worker_thread(worker_thread&& other) noexcept {
59 *this = std::move(other);
60 }
61
62 DCA_INLINE ~worker_thread() = default;
63
64 unbounded_message_block<std::coroutine_handle<>> tasks{};///< Queue of coroutine tasks.
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 std::unordered_map<uint64_t, unique_ptr<worker_thread>> {
71 public:
72 using map_type = std::unordered_map<uint64_t, unique_ptr<worker_thread>>;
73
74 /// @brief Constructor to create a coroutine thread pool. initializes the worker threads.
75 DCA_INLINE co_routine_thread_pool() : threadCount(std::thread::hardware_concurrency()) {
76 std::unique_lock lock01{ workerAccessMutex };
77 for (uint32_t x = 0; x < threadCount; ++x) {
78 uint64_t indexNew = x;
79 getMap().emplace(indexNew, makeUnique<worker_thread>());
80 std::this_thread::sleep_for(150ms);
81 getMap().at(indexNew)->thread = std::jthread{ [=, this](std::stop_token tokenNew) mutable {
82 auto indexNewer = indexNew;
83 threadFunction(getMap().at(indexNewer).get(), tokenNew);
84 } };
85 }
86 }
87
88 /// @brief Submit a coroutine task to the thread pool.
89 /// @param coro the coroutine handle to submit.
90 DCA_INLINE void submitTask(std::coroutine_handle<> coro) {
91 uint64_t currentLowestIndex{ std::numeric_limits<uint64_t>::max() };
92 while (currentLowestIndex == std::numeric_limits<uint64_t>::max()) {
93 currentLowestIndex = areWeAllBusy();
94 std::this_thread::sleep_for(1ms);
95 }
96 std::shared_lock lock01{ workerAccessMutex };
97 if (std ::this_thread::get_id() == getMap()[currentLowestIndex]->thread.get_id()) {
98 ++currentLowestIndex;
99 }
100 getMap()[currentLowestIndex]->tasks.send(std::move(coro));
101 }
102
104 doWeQuit.store(true, std::memory_order_release);
105 }
106
107 protected:
108 std::shared_mutex workerAccessMutex{};///< Shared mutex for worker thread access.
109 std::atomic_bool doWeQuit{ false };///< Whether or not we're quitting.
110 const uint64_t threadCount{};///< Total thread count.
111
112 /// @brief Thread function for each worker thread.
113 /// @param thread A pointer to the current thread of execution.
114 /// @param tokenNew The stop token for the thread.
115 DCA_INLINE void threadFunction(worker_thread* thread, std::stop_token tokenNew) {
116 while (!doWeQuit.load(std::memory_order_acquire) && !tokenNew.stop_requested()) {
117 std::coroutine_handle<> coroHandle{};
118 if (thread->tasks.tryReceive(coroHandle)) {
119 thread->areWeCurrentlyWorking.store(true, std::memory_order_release);
120 try {
121 coroHandle();
122 while (!coroHandle.done()) {
123 std::this_thread::sleep_for(1ms);
124 }
125 } catch (const std::runtime_error& error) {
127 }
128 thread->areWeCurrentlyWorking.store(false, std::memory_order_release);
129 }
130 std::this_thread::sleep_for(std::chrono::nanoseconds{ 100000 });
131 }
132 }
133
134 DCA_INLINE uint64_t areWeAllBusy() {
135 for (uint64_t x = 0; x < this->size(); ++x) {
136 if (this->contains(x)) {
137 if (!this->operator[](x)->areWeCurrentlyWorking.load(std::memory_order_acquire)) {
138 return x;
139 }
140 }
141 }
142 return std::numeric_limits<uint64_t>::max();
143 }
144
145 DCA_INLINE map_type& getMap() {
146 return *this;
147 }
148 };
149
150 /**@}*/
151 }
152}
A class representing a coroutine-based thread pool.
DCA_INLINE co_routine_thread_pool()
Constructor to create a coroutine thread pool. initializes the worker threads.
std::shared_mutex workerAccessMutex
Shared mutex for worker thread access.
DCA_INLINE void threadFunction(worker_thread *thread, std::stop_token tokenNew)
Thread function for each worker thread.
DCA_INLINE void submitTask(std::coroutine_handle<> coro)
Submit a coroutine task to the thread pool.
static DCA_INLINE void printError(const string_type &what, std::source_location where=std::source_location::current())
Print an error message of the specified type.
Definition Base.hpp:252
DCA_INLINE unique_ptr< value_type, deleter > makeUnique(arg_types &&... args)
Helper function to create a unique_ptr for a non-array object.
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.