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 "CoRoutineThreadPool" related stuff.
27/// Dec 18, 2021
28/// https://discordcoreapi.com
29/// \file CoRoutineThreadPool.hpp
30
31#pragma once
32
36#include <coroutine>
37
38namespace DiscordCoreAPI {
39
40 namespace DiscordCoreInternal {
41
42 /**
43 * \addtogroup discord_core_internal
44 * @{
45 */
46
47 /// @brief A struct representing a worker thread for coroutine-based tasks.
48 struct WorkerThread {
49 inline WorkerThread() = default;
50
51 inline ~WorkerThread() = default;
52
53 UnboundedMessageBlock<std::coroutine_handle<>> tasks{};///< Queue of coroutine tasks.
54 std::atomic_bool areWeCurrentlyWorking{};///< Atomic flag indicating if the thread is working.
55 UniquePtr<ThreadWrapper> thread{};///< Joinable thread.
56 };
57
58 /// @brief A class representing a coroutine-based thread pool.
59 class CoRoutineThreadPool : protected DiscordCoreAPI::UnorderedMap<uint64_t, UniquePtr<WorkerThread>> {
60 public:
61 using map_type = DiscordCoreAPI::UnorderedMap<uint64_t, UniquePtr<WorkerThread>>;
63
64 /// @brief Constructor to create a coroutine thread pool. Initializes the worker threads.
65 inline CoRoutineThreadPool() : threadCount(std::thread::hardware_concurrency()) {
66 for (uint32_t x = 0; x < threadCount; ++x) {
67 UniquePtr<WorkerThread> workerThread{ makeUnique<WorkerThread>() };
68 currentIndex.fetch_add(1, std::memory_order_release);
69 currentCount.fetch_add(1, std::memory_order_release);
70 uint64_t indexNew = currentIndex.load(std::memory_order_acquire);
71 emplace(indexNew, std::move(workerThread));
72 getMap()[indexNew]->thread = makeUnique<ThreadWrapper>([=, this](StopToken stopToken) {
73 threadFunction(stopToken, indexNew);
74 });
75 }
76 }
77
78 /// @brief Submit a coroutine task to the thread pool.
79 /// @param coro The coroutine handle to submit.
80 inline void submitTask(std::coroutine_handle<> coro) {
81 bool areWeAllBusy{ true };
82 uint64_t currentLowestValue{ std::numeric_limits<uint64_t>::max() };
83 uint64_t currentLowestIndex{ std::numeric_limits<uint64_t>::max() };
84 std::shared_lock lock01{ workerAccessMutex };
85 for (auto& [key, value]: getMap()) {
86 if (!value->areWeCurrentlyWorking.load(std::memory_order_acquire)) {
87 areWeAllBusy = false;
88 if (value->tasks.size() < currentLowestValue) {
89 currentLowestValue = value->tasks.size();
90 currentLowestIndex = key;
91 }
92 break;
93 }
94 }
95 if (areWeAllBusy) {
96 UniquePtr<WorkerThread> workerThread{ makeUnique<WorkerThread>() };
97 currentIndex.fetch_add(1, std::memory_order_release);
98 currentCount.fetch_add(1, std::memory_order_release);
99 uint64_t indexNew = currentIndex.load(std::memory_order_acquire);
100 lock01.unlock();
101 std::unique_lock lock02{ workerAccessMutex };
102 getMap().emplace(indexNew, std::move(workerThread));
103 getMap()[indexNew]->thread = makeUnique<ThreadWrapper>([=, this](StopToken stopToken) {
104 threadFunction(stopToken, indexNew);
105 });
106 lock02.unlock();
107 } else {
108 getMap()[currentLowestIndex]->tasks.send(std::move(coro));
109 }
110 }
111
112 protected:
113 std::shared_mutex workerAccessMutex{};///< Shared mutex for worker thread access.
114 std::atomic_uint64_t currentCount{};///< Current count of worker threads.
115 std::atomic_uint64_t currentIndex{};///< Current index of worker threads.
116 const uint64_t threadCount{};///< Total thread count.
117
118 /// @brief Thread function for each worker thread.
119 /// @param stopToken The stop token for the thread.
120 /// @param index The index of the worker thread.
121 inline void threadFunction(StopToken stopToken, uint64_t index) {
122 while (!stopToken.stopRequested()) {
123 if (!getMap().contains(index)) {
124 return;
125 }
126 std::coroutine_handle<> coroHandle{};
127 if (getMap()[index]->tasks.tryReceive(coroHandle)) {
128 getMap()[index]->areWeCurrentlyWorking.store(true, std::memory_order_release);
129 try {
130 coroHandle();
131 } catch (...) {
132 }
133 if (!contains(index)) {
134 return;
135 }
136 getMap()[index]->areWeCurrentlyWorking.store(false, std::memory_order_release);
137 }
138 if (currentCount.load(std::memory_order_acquire) > threadCount) {
139 uint64_t extraWorkers{ currentCount.load(std::memory_order_acquire) - threadCount };
140 while (extraWorkers > 0) {
141 --extraWorkers;
142 std::unique_lock lock{ workerAccessMutex };
143 auto oldThread = begin();
144 if (oldThread->second->thread->joinable()) {
145 oldThread->second->thread->requestStop();
146 oldThread->second->thread->detach();
147 currentCount.store(currentCount.load(std::memory_order_acquire) - 1, std::memory_order_release);
148 getMap().erase(oldThread->first);
149 }
150 }
151 }
152 std::this_thread::sleep_for(Nanoseconds{ 100000 });
153 }
154 }
155
156 inline map_type& getMap() {
157 return *this;
158 }
159 };
160
161 /**@}*/
162 }
163}
DiscordCoreClient - The main class for this library.
A struct representing a worker thread for coroutine-based tasks.
UniquePtr< ThreadWrapper > thread
Joinable thread.
std::atomic_bool areWeCurrentlyWorking
Atomic flag indicating if the thread is working.
UnboundedMessageBlock< std::coroutine_handle<> > tasks
Queue of coroutine tasks.
A class representing a coroutine-based thread pool.
std::atomic_uint64_t currentIndex
Current index of worker threads.
CoRoutineThreadPool()
Constructor to create a coroutine thread pool. Initializes the worker threads.
void submitTask(std::coroutine_handle<> coro)
Submit a coroutine task to the thread pool.
void threadFunction(StopToken stopToken, uint64_t index)
Thread function for each worker thread.
std::shared_mutex workerAccessMutex
Shared mutex for worker thread access.
std::atomic_uint64_t currentCount
Current count of worker threads.
A token used to control thread stopping.
bool stopRequested()
Check if stop has been requested.
A thread-safe messaging block for data-structures.
A smart pointer class that provides unique ownership semantics.
Definition: UniquePtr.hpp:49