-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathasync_thread.cc
444 lines (390 loc) Β· 13.4 KB
/
async_thread.cc
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/*
+----------------------------------------------------------------------+
| Swoole |
+----------------------------------------------------------------------+
| This source file is subject to version 2.0 of the Apache license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.apache.org/licenses/LICENSE-2.0.html |
| If you did not receive a copy of the Apache2.0 license and are unable|
| to obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Tianfeng Han <[email protected]> |
+----------------------------------------------------------------------+
*/
#include "swoole_api.h"
#include "swoole_socket.h"
#include "swoole_reactor.h"
#include "swoole_string.h"
#include "swoole_signal.h"
#include "swoole_pipe.h"
#include "swoole_async.h"
#include "swoole_util.h"
#include <thread>
#include <atomic>
#include <unordered_map>
#include <chrono>
#include <condition_variable>
#include <mutex>
#include <queue>
#include <sstream>
#include <system_error>
static std::mutex async_thread_lock;
static std::shared_ptr<swoole::async::ThreadPool> async_thread_pool;
namespace swoole {
namespace async {
//-------------------------------------------------------------------------------
class EventQueue {
public:
inline void push(AsyncEvent *event) {
_queue.push(event);
}
inline AsyncEvent *pop() {
if (_queue.empty()) {
return nullptr;
}
AsyncEvent *retval = _queue.front();
_queue.pop();
return retval;
}
inline double get_max_wait_time() {
if (_queue.empty()) {
return 0;
} else {
AsyncEvent *event = _queue.front();
return microtime() - event->timestamp;
}
}
inline size_t count() {
return _queue.size();
}
private:
std::queue<AsyncEvent *> _queue;
};
class ThreadPool {
public:
ThreadPool(size_t _core_worker_num, size_t _worker_num, double _max_wait_time, double _max_idle_time) {
running = false;
core_worker_num = _core_worker_num == 0 ? SW_CPU_NUM : SW_MAX(1, _core_worker_num);
worker_num = _worker_num == 0 ? SW_CPU_NUM * SW_AIO_THREAD_NUM_MULTIPLE : SW_MAX(core_worker_num, _worker_num);
max_wait_time = _max_wait_time == 0 ? SW_AIO_TASK_MAX_WAIT_TIME : _max_wait_time;
max_idle_time = _max_idle_time == 0 ? SW_AIO_THREAD_MAX_IDLE_TIME : _max_idle_time;
}
~ThreadPool() {
shutdown();
}
bool is_running() {
return running;
}
bool start() {
running = true;
current_task_id = 0;
n_waiting = 0;
n_closing = 0;
for (size_t i = 0; i < core_worker_num; i++) {
create_thread(true);
}
return true;
}
bool shutdown() {
if (!running) {
return false;
}
event_mutex.lock();
running = false;
_cv.notify_all();
event_mutex.unlock();
for (auto &i : threads) {
std::thread *_thread = i.second;
if (_thread->joinable()) {
_thread->join();
}
delete _thread;
}
threads.clear();
return true;
}
void schedule() {
if (n_waiting == 0 && threads.size() < worker_num && max_wait_time > 0) {
double _max_wait_time = _queue.get_max_wait_time();
if (_max_wait_time > max_wait_time) {
size_t n = 1;
/**
* maybe we can find a better strategy
*/
if (threads.size() + n > worker_num) {
n = worker_num - threads.size();
}
swoole_trace_log(SW_TRACE_AIO,
"Create %zu thread due to wait %fs, we will have %zu threads",
n,
_max_wait_time,
threads.size() + n);
while (n--) {
create_thread();
}
}
}
}
AsyncEvent *dispatch(const AsyncEvent *request) {
auto _event_copy = new AsyncEvent(*request);
event_mutex.lock();
schedule();
_event_copy->task_id = current_task_id++;
_event_copy->timestamp = microtime();
_event_copy->pipe_socket = SwooleTG.async_threads->write_socket;
_queue.push(_event_copy);
_cv.notify_one();
event_mutex.unlock();
swoole_debug("push and notify one: %f", microtime());
return _event_copy;
}
inline size_t get_worker_num() {
return threads.size();
}
inline size_t get_queue_size() {
std::unique_lock<std::mutex> lock(event_mutex);
return _queue.count();
}
static std::string get_thread_id(std::thread::id id) {
std::stringstream ss;
ss << id;
return ss.str();
}
void release_thread(std::thread::id tid) {
auto i = threads.find(tid);
if (i == threads.end()) {
swoole_warning("AIO thread#%s is missing", get_thread_id(tid).c_str());
return;
} else {
std::thread *_thread = i->second;
swoole_trace_log(SW_TRACE_AIO,
"release idle thread#%s, we have %zu now",
get_thread_id(tid).c_str(),
threads.size() - 1);
if (_thread->joinable()) {
_thread->join();
}
threads.erase(i);
delete _thread;
}
}
static void release_callback(AsyncEvent *event) {
std::thread::id *tid = reinterpret_cast<std::thread::id *>(event->object);
SwooleTG.async_threads->pool->release_thread(*tid);
delete tid;
// balance
SwooleTG.async_threads->task_num++;
}
void notify_one() {
_cv.notify_one();
}
private:
void create_thread(const bool is_core_worker = false);
void main_func(const bool is_core_worker);
size_t core_worker_num;
size_t worker_num;
double max_wait_time;
double max_idle_time;
bool running;
std::atomic<size_t> n_waiting;
std::atomic<size_t> n_closing;
size_t current_task_id = 0;
std::unordered_map<std::thread::id, std::thread *> threads;
EventQueue _queue;
std::mutex event_mutex;
std::condition_variable _cv;
};
void ThreadPool::main_func(bool is_core_worker) {
bool exit_flag = false;
swoole_thread_init();
while (running) {
event_mutex.lock();
AsyncEvent *event = _queue.pop();
event_mutex.unlock();
swoole_debug("%s: %f", event ? "pop 1 event" : "no event", microtime());
if (event) {
if (sw_unlikely(event->handler == nullptr)) {
event->error = SW_ERROR_AIO_BAD_REQUEST;
event->retval = -1;
} else if (sw_unlikely(event->canceled)) {
event->error = SW_ERROR_AIO_CANCELED;
event->retval = -1;
} else {
event->handler(event);
}
swoole_trace_log(SW_TRACE_AIO,
"aio_thread %s. ret=%ld, error=%d",
event->retval > 0 ? "ok" : "failed",
event->retval,
event->error);
_send_event:
while (true) {
ssize_t ret = event->pipe_socket->write(&event, sizeof(event));
if (ret < 0) {
if (errno == EAGAIN) {
event->pipe_socket->wait_event(1000, SW_EVENT_WRITE);
continue;
} else if (errno == EINTR) {
continue;
} else {
delete event;
swoole_sys_warning("sendto swoole_aio_pipe_write failed");
}
}
break;
}
// exit
if (exit_flag) {
n_closing--;
break;
}
} else {
std::unique_lock<std::mutex> lock(event_mutex);
if (_queue.count() > 0) {
continue;
}
if (!running) {
break;
}
++n_waiting;
if (is_core_worker || max_idle_time <= 0) {
_cv.wait(lock);
} else {
while (true) {
if (_cv.wait_for(lock, std::chrono::microseconds((size_t) (max_idle_time * 1000 * 1000))) ==
std::cv_status::timeout) {
if (running && n_closing != 0) {
// wait for the next round
continue;
}
/* notifies the main thread to release this thread */
event = new AsyncEvent;
event->object = new std::thread::id(std::this_thread::get_id());
event->callback = release_callback;
event->pipe_socket = SwooleG.aio_default_socket;
event->canceled = false;
--n_waiting;
++n_closing;
exit_flag = true;
goto _send_event;
}
break;
}
}
--n_waiting;
}
}
swoole_thread_clean();
}
void ThreadPool::create_thread(const bool is_core_worker) {
try {
std::thread *_thread = new std::thread([this, is_core_worker]() { main_func(is_core_worker); });
threads[_thread->get_id()] = _thread;
} catch (const std::system_error &e) {
swoole_sys_notice("create aio thread failed, please check your system configuration or adjust aio_worker_num");
return;
}
}
AsyncEvent *dispatch(const AsyncEvent *request) {
if (sw_unlikely(!SwooleTG.async_threads)) {
SwooleTG.async_threads = new AsyncThreads();
}
AsyncEvent *event = SwooleTG.async_threads->pool->dispatch(request);
if (sw_likely(event)) {
SwooleTG.async_threads->task_num++;
}
return event;
}
//-------------------------------------------------------------------------------
} // namespace async
int AsyncThreads::callback(Reactor *reactor, Event *event) {
AsyncEvent *events[SW_AIO_EVENT_NUM];
ssize_t n = event->socket->read(events, sizeof(AsyncEvent *) * SW_AIO_EVENT_NUM);
if (n < 0) {
swoole_sys_warning("read() aio events failed");
return SW_ERR;
}
for (size_t i = 0; i < n / sizeof(AsyncEvent *); i++) {
AsyncEvent *event = events[i];
if (!event->canceled) {
event->callback(event);
}
SwooleTG.async_threads->task_num--;
delete event;
}
return SW_OK;
}
size_t AsyncThreads::get_worker_num() {
return pool ? pool->get_worker_num() : 0;
}
size_t AsyncThreads::get_queue_size() {
return pool ? pool->get_queue_size() : 0;
}
void AsyncThreads::notify_one() {
if (pool) {
pool->notify_one();
}
}
AsyncThreads::AsyncThreads() {
if (!SwooleTG.reactor) {
swoole_warning("no event loop, cannot initialized");
throw swoole::Exception(SW_ERROR_WRONG_OPERATION);
}
pipe = new Pipe(false);
if (!pipe->ready()) {
delete pipe;
pipe = nullptr;
swoole_throw_error(SW_ERROR_SYSTEM_CALL_FAIL);
}
read_socket = pipe->get_socket(false);
write_socket = pipe->get_socket(true);
read_socket->fd_type = SW_FD_AIO;
write_socket->fd_type = SW_FD_AIO;
swoole_event_add(read_socket, SW_EVENT_READ);
sw_reactor()->add_destroy_callback([](void *data) {
if (!SwooleTG.async_threads) {
return;
}
swoole_event_del(SwooleTG.async_threads->read_socket);
delete SwooleTG.async_threads;
SwooleTG.async_threads = nullptr;
});
sw_reactor()->set_exit_condition(Reactor::EXIT_CONDITION_AIO_TASK, [](Reactor *reactor, size_t &event_num) -> bool {
if (SwooleTG.async_threads && SwooleTG.async_threads->task_num == 0) {
event_num--;
}
return true;
});
async_thread_lock.lock();
if (!async_thread_pool) {
async_thread_pool = std::make_shared<async::ThreadPool>(
SwooleG.aio_core_worker_num, SwooleG.aio_worker_num, SwooleG.aio_max_wait_time, SwooleG.aio_max_idle_time);
}
if (!async_thread_pool->is_running()) {
async_thread_pool->start();
}
pool = async_thread_pool;
async_thread_lock.unlock();
SwooleG.aio_default_socket = write_socket;
SwooleTG.async_threads = this;
}
AsyncThreads::~AsyncThreads() {
pool.reset();
async_thread_lock.lock();
/**
* When the reference count is 1, it means that all reactor threads have ended
* and all aio threads can be terminated.
*/
if (async_thread_pool.use_count() == 1) {
async_thread_pool->shutdown();
}
async_thread_lock.unlock();
pipe->close();
read_socket = nullptr;
write_socket = nullptr;
delete pipe;
pipe = nullptr;
}
}; // namespace swoole