-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker_pool.cpp
More file actions
119 lines (99 loc) · 2.6 KB
/
Copy pathworker_pool.cpp
File metadata and controls
119 lines (99 loc) · 2.6 KB
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
#include "../include/dispatch_queue/detail/worker_pool.hpp"
#include <cassert>
namespace dispatch_queue {
namespace detail {
worker_pool::~worker_pool() {
shutdown();
}
int worker_pool::thread_count() const {
return worker_thread_count;
}
bool worker_pool::empty() const {
std::lock_guard<std::mutex> lock(mutex);
return task_queue.empty();
}
bool worker_pool::main_empty() const {
std::lock_guard<std::mutex> lock(mutex);
return task_queue.main_empty();
}
size_t worker_pool::size() const {
std::lock_guard<std::mutex> lock(mutex);
return task_queue.size();
}
size_t worker_pool::main_size() const {
std::lock_guard<std::mutex> lock(mutex);
return task_queue.main_size();
}
void worker_pool::enqueue_task(task_type type, task_function&& task, float delay, task_tag tag) {
bool should_wake_thread;
{
std::lock_guard<std::mutex> lock(mutex);
bool has_new_background_task = task_queue.push(type, std::move(task), delay, tag);
should_wake_thread = has_new_background_task && idle_threads;
}
if (should_wake_thread) {
task_condition_variable.notify_one();
}
}
std::list<task_function> worker_pool::pop_main_loop_tasks(float delta) {
std::lock_guard<std::mutex> lock(mutex);
return task_queue.pop_main_loop_tasks(delta);
}
void worker_pool::clear() {
std::lock_guard<std::mutex> lock(mutex);
task_queue.clear();
}
void worker_pool::main_clear() {
std::lock_guard<std::mutex> lock(mutex);
task_queue.main_clear();
}
void worker_pool::shutdown() {
if (worker_threads.empty()) {
return;
}
{
std::lock_guard<std::mutex> lock(mutex);
is_shutting_down = true;
}
for (int i = 0; i < thread_count(); i++) {
task_condition_variable.notify_one();
}
for (auto& thread : worker_threads) {
if (thread.joinable()) {
thread.join();
}
}
worker_threads.clear();
idle_threads = 0;
is_shutting_down = false;
}
void worker_pool::wait() const {
std::unique_lock<std::mutex> lock(mutex);
all_done_condition_variable.wait(lock, wait_predicate());
}
void worker_pool::run_task_loop() {
pending_task task;
while (true) {
// 1. Get a valid task
{
std::unique_lock<std::mutex> lock(mutex);
if (!task_queue.try_pop(task)) {
++idle_threads;
assert(idle_threads <= worker_thread_count);
if (idle_threads == worker_thread_count) {
all_done_condition_variable.notify_all();
}
task_condition_variable.wait(lock, [this, &task]{ return is_shutting_down || task_queue.try_pop(task); });
--idle_threads;
assert(idle_threads >= 0);
}
if (is_shutting_down) {
return;
}
}
// 2. Do some work
task();
}
}
} // end namespace detail
} // end namespace dispatch_queue