-
Notifications
You must be signed in to change notification settings - Fork 0
/
thread_pool_fast.cpp
186 lines (145 loc) · 3.92 KB
/
thread_pool_fast.cpp
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
#include <cstdlib>
#include <sys/time.h> // gettimeofday
#include "callback.hpp"
#include "logging.hpp"
#include "thread.hpp"
#include "thread_pool_fast.hpp"
namespace base {
using base::Callback;
using base::makeCallableOnce;
static __thread bool last_worker_ = false;
ThreadLocal<int> ThreadPoolFast::worker_num_;
//
// Internal Worker Class
//
class ThreadPoolFast::Worker {
public:
Worker(ThreadPoolFast*);
~Worker();
void workerLoop(int instance);
void assignTask(Callback<void>* task);
private:
ThreadPoolFast* my_pool_; // not owned here
Mutex m_;
ConditionVar cv_has_task_;
bool has_task_;
Callback<void>* task_;
};
ThreadPoolFast::Worker::Worker(ThreadPoolFast* pool)
: my_pool_(pool),
has_task_(false),
task_(NULL) {
}
ThreadPoolFast::Worker::~Worker() {
}
void ThreadPoolFast::Worker::workerLoop(int instance) {
worker_num_.setVal(instance);
while (true) {
// Wait until I know my task. Because a task is assigned to this
// worker, we assume it left the free worker's pool.
{
ScopedLock l(&m_);
while (!has_task_) {
cv_has_task_.wait(&m_);
}
has_task_ = false;
}
// A NULL task is considered a request to stop this worker.
if (task_ == NULL) {
delete this;
break;
}
// If this worker is executing the ThreadPool tear down,
// i.e. stop(), the latter will notify this thread is the last
// worker, after waiting for all other worker threads to join.
(*task_)(); // would self-delete if once-run task
if (last_worker_) {
delete this;
break;
}
// Return the worker to the free worker's pool
my_pool_->queueWorker(this);
}
}
void ThreadPoolFast::Worker::assignTask(Callback<void>* task) {
ScopedLock l(&m_);
task_ = task;
has_task_ = true;
cv_has_task_.signal();
}
//
// ThreadPoolFast Definitions
//
ThreadPoolFast::ThreadPoolFast(int num_workers) {
for (int i = 0; i < num_workers; i++) {
Worker* worker = new Worker(this);
Callback<void>* body = makeCallableOnce(&Worker::workerLoop, worker, i);
workers_tids_.push_back(makeThread(body));
queueWorker(worker);
}
}
ThreadPoolFast::~ThreadPoolFast() {
m_dispatch_.lock();
while (! dispatch_queue_.empty()) {
Callback<void>* task = dispatch_queue_.front();
dispatch_queue_.pop();
if (task && task->once()) {
delete task;
}
}
m_dispatch_.unlock();
}
void ThreadPoolFast::stop() {
// Issue a stop request callback for each worker thread. If the
// stop() is being issued from one of the workers itself, one of the
// stop callbacks won't be consummed. The destructor would dispose
// of it.
for (size_t i = 0; i < workers_tids_.size(); i++) {
addTask(NULL);
}
bool exit_last_worker = false;
const size_t num_workers = workers_tids_.size();
for (size_t i = 0; i < num_workers; ++i) {
if (pthread_self() == workers_tids_[i]) {
exit_last_worker = true;
} else {
pthread_join(workers_tids_[i], NULL);
}
}
if (exit_last_worker) {
last_worker_ = true;
}
}
void ThreadPoolFast::queueWorker(Worker* worker) {
ScopedLock l(&m_dispatch_);
// If there are tasks waiting, pick the worker right away; don't
// bother putting it back in the pool.
if (! dispatch_queue_.empty()) {
worker->assignTask(dispatch_queue_.front());
dispatch_queue_.pop();
} else {
workers_.push_front(worker);
}
}
void ThreadPoolFast::addTask(Callback<void>* task) {
ScopedLock l(&m_dispatch_);
if (! workers_.empty()) {
Worker* worker = workers_.front();
workers_.pop_front();
worker->assignTask(task);
return;
}
dispatch_queue_.push(task);
}
int ThreadPoolFast::count() const {
ScopedLock l(&m_dispatch_);
return dispatch_queue_.size();
}
/*static*/
int ThreadPoolFast::ME() {
return worker_num_.getVal();
}
void ThreadPoolFast::setMEForTest(int i) {
worker_num_.setVal(i);
}
} // namespace base