-
Notifications
You must be signed in to change notification settings - Fork 2
/
new_threads.h
240 lines (198 loc) · 6.1 KB
/
new_threads.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include "includes.h"
#include "data.h"
#include "globals.h"
#include <deque>
#include <mutex>
#include <thread>
#include <future>
#include <semaphore>
#include <algorithm>
#include <memory>
#include <functional>
#include <map>
#include <queue>
#include <any>
// credits
// https://habr.com/ru/post/656515/
// https://github.com/bshoshany/thread-pool
enum class task_status_t
{
in_q,
completed
};
// C++ 17
class c_task
{
public:
template <typename func_ret_type, typename ...fargs, typename ...func_types>
c_task(func_ret_type(*func)(func_types...), fargs&&... args) :
is_void{ std::is_void_v<func_ret_type> }
{
if constexpr (std::is_void_v<func_ret_type>)
{
void_func = std::bind(func, args...);
any_func = []()->int { return 0; };
}
else {
void_func = []()->void {};
any_func = std::bind(func, args...);
}
}
void operator() ()
{
void_func();
any_func_result = any_func();
}
bool has_result()
{
return !is_void;
}
std::any get_result() const
{
//assert(!is_void);
// assert(any_func_result.has_value());
return any_func_result;
}
private:
std::function<void()> void_func;
std::function<std::any()> any_func;
std::any any_func_result;
bool is_void;
};
struct task_info_t
{
task_status_t status = task_status_t::in_q;
std::any result;
};
class ThreadPool
{
private:
std::vector<std::thread> threads;
std::queue<std::pair<c_task, uint64_t>> q;
std::mutex q_mtx;
std::condition_variable q_cv;
std::unordered_map<uint64_t, task_info_t> tasks_info;
std::condition_variable tasks_info_cv;
std::mutex tasks_info_mtx;
std::condition_variable wait_all_cv;
std::atomic<bool> quite{ false };
std::atomic<std::uint64_t> last_idx{ 0 };
std::atomic<std::uint64_t> cnt_completed_tasks{ 0 };
using allocate_thread_id_fn = std::int32_t(*)();
using free_thread_id_fn = void(*)();
allocate_thread_id_fn allocate_thread_id;
free_thread_id_fn free_thread_id;
void run()
{
allocate_thread_id();
while (!quite)
{
std::unique_lock<std::mutex> lock(q_mtx);
q_cv.wait(lock, [this]()->bool { return !q.empty() || quite; });
if (!q.empty() && !quite)
{
std::pair<c_task, uint64_t> task = std::move(q.front());
q.pop();
lock.unlock();
task.first();
std::lock_guard<std::mutex> lock(tasks_info_mtx);
if (task.first.has_result()) {
tasks_info[task.second].result = task.first.get_result();
}
tasks_info[task.second].status = task_status_t::completed;
++cnt_completed_tasks;
}
wait_all_cv.notify_all();
tasks_info_cv.notify_all(); // notify for wait function
}
free_thread_id();
}
public:
int get_threads_count(HMODULE tier0)
{
if (tier0)
{
auto tier0_threads = (bool*)((uintptr_t)tier0 + 0x54E30);
auto count = 1;
for (auto i = 1; i < 128; ++i)
{
if (!tier0_threads[i])
break;
++count;
}
return clamp((int)std::thread::hardware_concurrency(), 1, 32 - count - 1);
}
return 3;
}
void init()
{
auto tier0 = GetModuleHandleA(crypt_str("tier0.dll"));
allocate_thread_id = (allocate_thread_id_fn)GetProcAddress(tier0, crypt_str("AllocateThreadID"));
free_thread_id = (free_thread_id_fn)GetProcAddress(tier0, crypt_str("FreeThreadID"));
const auto& num_threads = get_threads_count(tier0);
threads.reserve(num_threads);
for (int i = 0; i < num_threads; ++i)
threads.emplace_back(&ThreadPool::run, this);
}
void remove()
{
quite = true;
q_cv.notify_all();
for (int i = 0; i < threads.size(); ++i)
threads[i].join();
}
~ThreadPool()
{
remove();
}
template <typename Func, typename ...fargs, typename ...func_types>
std::uint64_t add_task(Func(*func)(func_types...), fargs&&... args)
{
const std::uint64_t task_id = last_idx++;
std::unique_lock<std::mutex> lock(tasks_info_mtx);
tasks_info[task_id] = task_info_t();
lock.unlock();
std::lock_guard<std::mutex> q_lock(q_mtx);
q.emplace(c_task(func, std::forward<fargs>(args)...), task_id);
q_cv.notify_one();
return task_id;
}
void wait(const std::uint64_t task_id)
{
std::unique_lock<std::mutex> lock(tasks_info_mtx);
tasks_info_cv.wait(lock, [this, task_id]()->bool
{
return task_id < last_idx&& tasks_info[task_id].status == task_status_t::completed;
});
}
std::any wait_result(const std::uint64_t task_id)
{
std::unique_lock<std::mutex> lock(tasks_info_mtx);
tasks_info_cv.wait(lock, [this, task_id]()->bool
{
return task_id < last_idx&& tasks_info[task_id].status == task_status_t::completed;
});
return tasks_info[task_id].result;
}
template<class T>
void wait_result(const std::uint64_t task_id, T& value)
{
std::unique_lock<std::mutex> lock(tasks_info_mtx);
tasks_info_cv.wait(lock, [this, task_id]()->bool
{
return task_id < last_idx&& tasks_info[task_id].status == task_status_t::completed;
});
value = std::any_cast<T>(tasks_info[task_id].result);
}
void wait_all()
{
std::unique_lock<std::mutex> lock(tasks_info_mtx);
wait_all_cv.wait(lock, [this]()->bool { return cnt_completed_tasks == last_idx; });
}
bool calculated(const std::uint64_t task_id)
{
std::lock_guard<std::mutex> lock(tasks_info_mtx);
return task_id < last_idx&& tasks_info[task_id].status == task_status_t::completed;
}
};
extern crypt_ptr<ThreadPool> thread_pool;