Sunshine master
Self-hosted game stream host for Moonlight.
thread_pool.h
Go to the documentation of this file.
1
5#pragma once
6
7// standard includes
8#include <thread>
9
10// local includes
11#include "platform/common.h"
12#include "task_pool.h"
13
14namespace thread_pool_util {
19 public:
23 typedef TaskPool::__task __task;
24
25 private:
26 std::vector<std::thread> _thread;
27
28 std::condition_variable _cv;
29 std::mutex _lock;
30
31 bool _continue;
32
33 public:
34 ThreadPool():
35 _continue {false} {
36 }
37
43 explicit ThreadPool(int threads):
44 _thread(threads),
45 _continue {true} {
46 for (auto &t : _thread) {
47 t = std::thread(&ThreadPool::_main, this);
48 }
49 }
50
51 ~ThreadPool() noexcept {
52 if (!_continue) {
53 return;
54 }
55
56 stop();
57 join();
58 }
59
67 template<class Function, class... Args>
68 auto push(Function &&newTask, Args &&...args) {
69 std::lock_guard lg(_lock);
70 auto future = TaskPool::push(std::forward<Function>(newTask), std::forward<Args>(args)...);
71
72 _cv.notify_one();
73 return future;
74 }
75
81 void pushDelayed(std::pair<__time_point, __task> &&task) {
82 std::lock_guard lg(_lock);
83
84 TaskPool::pushDelayed(std::move(task));
85 }
86
95 template<class Function, class X, class Y, class... Args>
96 auto pushDelayed(Function &&newTask, std::chrono::duration<X, Y> duration, Args &&...args) {
97 std::lock_guard lg(_lock);
98 auto future = TaskPool::pushDelayed(std::forward<Function>(newTask), duration, std::forward<Args>(args)...);
99
100 // Update all timers for wait_until
101 _cv.notify_all();
102 return future;
103 }
104
110 void start(int threads) {
111 _continue = true;
112
113 _thread.resize(threads);
114
115 for (auto &t : _thread) {
116 t = std::thread(&ThreadPool::_main, this);
117 }
118 }
119
123 void stop() {
124 std::lock_guard lg(_lock);
125
126 _continue = false;
127 _cv.notify_all();
128 }
129
133 void join() {
134 for (auto &t : _thread) {
135 t.join();
136 }
137 }
138
139 public:
143 void _main() {
144 platf::set_thread_name("TaskPool::worker");
145 while (_continue) {
146 if (auto task = this->pop()) {
147 (*task)->run();
148 } else {
149 std::unique_lock uniq_lock(_lock);
150
151 if (ready()) {
152 continue;
153 }
154
155 if (!_continue) {
156 break;
157 }
158
159 if (auto tp = next()) {
160 _cv.wait_until(uniq_lock, *tp);
161 } else {
162 _cv.wait(uniq_lock);
163 }
164 }
165 }
166
167 // Execute remaining tasks
168 while (auto task = this->pop()) {
169 (*task)->run();
170 }
171 }
172 };
173} // namespace thread_pool_util
Queue of immediate and delayed tasks executed by worker threads.
Definition task_pool.h:67
std::optional< __task > pop()
Remove and return the next queued item, waiting when requested.
Definition task_pool.h:301
std::optional< __time_point > next()
Return the next timer task ready for execution.
Definition task_pool.h:335
bool ready()
Check whether the task pool has active workers.
Definition task_pool.h:324
Definition thread_pool.h:18
auto pushDelayed(Function &&newTask, std::chrono::duration< X, Y > duration, Args &&...args)
Queue a task that becomes runnable after a delay.
Definition thread_pool.h:96
ThreadPool(int threads)
Start a pool with the requested number of worker threads.
Definition thread_pool.h:43
void pushDelayed(std::pair< __time_point, __task > &&task)
Queue a task that becomes runnable after a delay.
Definition thread_pool.h:81
void start(int threads)
Start worker threads for queued thread-pool tasks.
Definition thread_pool.h:110
void join()
Wait for worker threads owned by the session to exit.
Definition thread_pool.h:133
void stop()
Stop worker threads and prevent additional task execution.
Definition thread_pool.h:123
TaskPool::__task __task
Callable unit executed by a worker thread.
Definition thread_pool.h:23
void _main()
Run the main application or worker loop.
Definition thread_pool.h:143
auto push(Function &&newTask, Args &&...args)
Queue work for asynchronous execution.
Definition thread_pool.h:68
Declarations for common platform specific utilities.
Functions for handling command line arguments.
Definition entry_handler.cpp:37
Declarations for the task pool system.