-
Notifications
You must be signed in to change notification settings - Fork 1
/
thread_workers_co.cpp
269 lines (215 loc) · 6.78 KB
/
thread_workers_co.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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include "thread_workers_co.h"
#include <vector>
#include <unistd.h>
#include "task.h"
#include "logging.h"
namespace utils {
#define _MIN_(a, b) (a)>=(b)?(b):(a)
#define _MAX_(a, b) (a)>=(b)?(a):(b)
int ThreadWorker::init(int idx, int coroutines, int copy_stack_k) {
idx_ = idx;
coroutines_ = _MIN_(_MAX_(coroutines, 1), 1000); // between [1, 1000]
copy_stack_k_ = _MIN_(_MAX_(copy_stack_k, 0), 10240); // between [0, 10M]
if (pipe(task_pipes_)) {
return -1;
}
if (pipe(exit_pipes_)) {
return -1;
}
evbase_ = event_base_new();
//timer_event_ = evtimer_new(evbase_, timerCallback, this);
task_ev_ = event_new(evbase_, task_pipes_[0], EV_READ|EV_PERSIST, newTaskCallback, (void*)this);
event_add(task_ev_, NULL);
exit_ev_ = event_new(evbase_, exit_pipes_[0], EV_READ|EV_PERSIST, exitTaskCallback, (void*)this);
event_add(exit_ev_, NULL);
return 0;
}
struct stEnv_t {
int idx;
ThreadWorker* w;
stCoRoutine_t* consumer_routine;
};
void* ThreadWorker::CoConsumer(void* args) {
co_enable_hook_sys();
stEnv_t* env = (stEnv_t*)args;
assert(env);
int idx = env->idx;
ThreadWorker* w = env->w;
assert(w);
LOG_INFO<<"coroutine start "<<w->idx_<<"-"<<idx;
while (true) {
//if (env->task_queue->size() <= 0) {
if (w->task_queue_size_ <= 0) {
co_cond_timedwait(w->task_queue_cond_, -1);
continue;
}
w->task_queue_size_--;
Task* task = w->task_queue_.front();
w->task_queue_.pop_front();
//Task* task = nullptr;
//if(env->task_queue->TryDequeue(task) || task == nullptr) {
if(task == nullptr) {
continue;
}
long cur = w->coroutines_running_.fetch_add(1);
LOG_DEBUG<<"worker:"<<w->idx_<<" RECV task "<<task->id()<<" runing_coroutines:"<<(cur+1)<<" queue_len:"<<w->task_queue_size_;
try {
task->run();
} catch(...) {
// LOG_ERROR
}
w->done(task);
}
delete env;
co_yield_ct();
return nullptr;
}
int ThreadWorker::run(bool detach/*=false*/) {
worker_thread_.reset(new std::thread(ThreadWorker::runRoutine, (void*)this));
thread_id_ = worker_thread_->get_id(); // 线程ID
if(detach) {
worker_thread_->detach();
}
return 0;
}
//static
void ThreadWorker::runRoutine(void* arg) {
ThreadWorker* w = (ThreadWorker*)arg;
assert(w);
w->runRoutineImp();
}
void ThreadWorker::runRoutineImp() {
task_queue_cond_ = co_cond_alloc();
if(copy_stack_k_ > 0)
share_stack_ = co_alloc_sharestack(1, 1024 * copy_stack_k_);
else
share_stack_ = nullptr;
// 协程池
std::vector<stEnv_t*> coroutine_envs;
for(int i=0; i<coroutines_; i++) {
stEnv_t* env = new stEnv_t;
env->w = this;
env->idx = i;
coroutine_envs.push_back(env);
if(share_stack_) {
stCoRoutineAttr_t attr;
attr.stack_size = 0;
attr.share_stack = share_stack_;
co_create(&env->consumer_routine, &attr, ThreadWorker::CoConsumer, (void*)env);
} else {
co_create(&env->consumer_routine, nullptr, ThreadWorker::CoConsumer, (void*)env);
}
co_resume(env->consumer_routine);
}
co_eventloop(co_get_epoll_ct(), ThreadWorker::eventLoop, (void*)this);
// 释放协程资源
for(int i=0; i<coroutine_envs.size(); i++) {
stEnv_t* env = coroutine_envs[i];
co_release(env->consumer_routine);
delete env;
}
//event_free(timer_event_);
event_free(task_ev_);
event_free(exit_ev_);
event_base_free(evbase_);
LOG_INFO<<"worker:"<<idx_<<" exit!";
}
int ThreadWorker::eventLoop(void* args) {
ThreadWorker* w = (ThreadWorker*)args;
assert(w);
event_base_loop(w->evbase_, EVLOOP_ONCE | EVLOOP_NONBLOCK);
return 0;
}
void ThreadWorker::wait() {
if(worker_thread_) {
if(worker_thread_->joinable()) {
worker_thread_->join();
}
worker_thread_.reset();
}
LOG_DEBUG<<"wait exit worker="<<thread_id_;
}
//static
//void ThreadWorker::timerCallback(int fd, short kind, void *userp) {
// // TODO: stats
//}
int ThreadWorker::addTask(Task* task) {
LOG_DEBUG<<"worker:"<<idx_<<" ADD task "<<task->id();
int res = write(task_pipes_[1], &task, sizeof(task)); // 写入内存地址
return res==-1?-1:0;
}
struct stEnvNotify_t {
ThreadWorker* w;
Task* task;
stCoRoutine_t* notify_routine;
};
//static
void ThreadWorker::newTaskCallback(int fd, short events, void* arg) {
ThreadWorker* w = (ThreadWorker*)arg;
assert(w);
Task* task = nullptr;
if(read(fd, &task, sizeof(task)) != sizeof(task)) {
LOG_ERROR<<"woker:"<<w->idx_<<" task_fd read error fd="<<fd;
return;
}
assert(task);
//task_queue_.Enqueue(task);
w->task_queue_.push_back(task);
w->task_queue_size_++;
co_cond_signal(w->task_queue_cond_); // 协程池直接监控队列,进行处理
long cur = w->coroutines_running_.fetch_add(0); // TODO: load(..)
LOG_DEBUG<<"worker:"<<w->idx_<<" RECV task "<<task->id()<<" runing_coroutines:"<<(cur+1);
}
void ThreadWorker::done(Task* task) {
long cur = coroutines_running_.fetch_sub(1);
long tasks_done = tasks_done_.fetch_add(1);
LOG_INFO<<"worker:"<<idx_<<" DONE task "<<task->id()<<" done:"<<(tasks_done+1)<<" runing_coroutines:"<<(cur-1)<<" queue_len:"<<task_queue_size_;
if(task != nullptr)
delete task; // TODO: free
}
void ThreadWorker::stop() {
char buf[1] = {'-'};
write(exit_pipes_[1], buf, 1);
}
//static
void ThreadWorker::exitTaskCallback(int fd, short events, void* arg) {
ThreadWorker* w = (ThreadWorker*)arg;
assert(w);
char buf[1];
if(read(fd, buf, 1) != 1) {
LOG_ERROR<<"woker:"<<w->idx_<<" exit_fd read error fd="<<fd;
return;
}
w->stopLoop();
}
void ThreadWorker::stopLoop() {
if(evbase_) {
event_base_loopexit(evbase_, NULL);
//event_base_loopbreak(evbase_);
}
}
int ThreadWorkers::init(int32_t workers, int coroutines, int copy_stack_k) {
workers_n_ = _MIN_(_MAX_(workers, 1), 100); // between [1, 100]
for(int i=0; i<workers_n_; i++) {
assert(workers_[i].init(i, coroutines, copy_stack_k) == 0);
assert(workers_[i].run() == 0);
}
LOG_INFO<<"ThreadWorkers start workers "<<workers_n_;
return 0;
}
int ThreadWorkers::destroy() {
for(int i = 0; i < workers_n_; i++) {
workers_[i].stop();
workers_[i].wait();
}
LOG_INFO<<"ThreadWorkers exit";
return 0;
}
int ThreadWorkers::addTask(Task* task) {
if(task == nullptr) return -1;
int worker_idx = rand()%workers_n_;
ThreadWorker* w = &workers_[worker_idx];
assert(w);
return w->addTask(task);
}
}