thread_pool.cpp
7.65 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
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
/*
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* CC/LICENSE
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <errno.h>
#include "thread_pool.h"
#include "lock.h"
#include "tools.h"
#include "ivr_timer.h"
using namespace std;
pthread_mutex_t ThreadPool::_mutex_sync = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t ThreadPool::_mutex_work_completion = PTHREAD_MUTEX_INITIALIZER;
ThreadPool::ThreadPool() {
_max_thread_num = 0;
_cur_thread_num = 0;
_queue_size = 0;
_unfinished_work = 0;
sem_init(&_available_work, 0, 0);
sem_init(&_available_threads, 0, _queue_size);
}
ThreadPool::~ThreadPool() {
}
ThreadPool* ThreadPool::get_instance() {
///<gcc默认的threadsafe-statics保证线程安全
static ThreadPool pool;
return &pool;
}
uint32_t ThreadPool::get_max_thread_num() {
return this->_max_thread_num;
}
void ThreadPool::initialize_threads(uint32_t max_thread_num) {
if (max_thread_num < 0) {
max_thread_num = 0;
}
int32_t empty_works = 0;
{
IVR_TRACE("set thread pool size(%u)!", max_thread_num);
::lock_t lock(_mutex_sync);
if (lock.locked()) {
//空余20%的资源,20%以内的呼叫直接挂机,超过20%则不予响应
uint32_t tmp = (uint32_t)((double)max_thread_num * 1.2) + 1;
sem_lock_t lock_threads(&_available_threads);
for (uint32_t i = _max_thread_num; i < tmp; ++i) {
pthread_t tempThread;
ivr_thread_param_t* thr_param = new(std::nothrow) ivr_thread_param_t;
if (thr_param == NULL) {
IVR_WARN("failed to new ivr_thread_param_t! exit at once!");
_exit(0);
}
thr_param->pool = this;
thr_param->event_queue = new(std::nothrow) ivr_event_queue_t;
// thr_param->evt_sem = new(std::nothrow) sem_t;
// sem_init(thr_param->evt_sem, 0, 0);
thr_param->timer = new(std::nothrow) IvrTimer;
// fix bug: tcb not free after thread stoped
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
if (pthread_create(&tempThread, &attr, &ThreadPool::thread_execute, (void*) thr_param) == 0) {
IVR_TRACE("create thread %u %lu success", i, tempThread);
} else {
IVR_WARN("create thread %u failure", i);
}
pthread_attr_destroy(&attr);
// queue size update
lock_threads.post();
++ _cur_thread_num;
}
// if now < max, add some empty works
if (tmp < _max_thread_num && _worker_queue.empty()) {
empty_works = (int32_t)(_max_thread_num - tmp);
empty_works -= std::max(0, (int32_t)(tmp - _cur_thread_num));
}
_max_thread_num = tmp;
//_queue_size = tmp;
} else {
IVR_WARN("locked failure");
}
}
IVR_TRACE("add empty works(%d)", empty_works);
for (int32_t i = 0; i < empty_works; ++ i) {
assign_work(NULL);
}
}
void ThreadPool::destroy_pool(int max_poll_second = 2) {
while (_unfinished_work > 0) {
ivr_tools_t::safe_sleeps(max_poll_second);
}
sem_destroy(&_available_work);
sem_destroy(&_available_threads);
pthread_mutex_destroy(&_mutex_sync);
pthread_mutex_destroy(&_mutex_work_completion);
}
bool ThreadPool::assign_work(WorkerThread* worker_thread) {
sem_lock_t lock_sem(&_available_threads);
if (!lock_sem.wait()) {
return false;
}
::lock_t lock2(_mutex_sync);
if (!lock2.locked()) {
return false;
}
::lock_t lock(_mutex_work_completion);
if (!lock.locked()) {
return false;
}
_worker_queue.push(worker_thread);
_unfinished_work++;
IVR_TRACE("unfinished_work = %u", _unfinished_work);
IVR_TRACE("push worker address:[%p]", worker_thread);
sem_lock_t lock_work(&_available_work);
return lock_work.post();
}
bool ThreadPool::fetch_work(WorkerThread** worker) {
sem_lock_t lock_work(&_available_work);
if (!lock_work.wait()) {
return false;
}
::lock_t lock(_mutex_sync);
if (!lock.locked()) {
return false;
}
IVR_TRACE("QUEUE SIZE:%lu", _worker_queue.size());
*worker = _worker_queue.front();
_worker_queue.pop();
IVR_TRACE("fetch work (%p)", *worker);
sem_lock_t lock_thread(&_available_threads);
return lock_thread.post();
}
void* ThreadPool::thread_execute(void* param) {
// com_openlog_r();
WorkerThread* worker = NULL;
ivr_thread_param_t* thr_param = (ivr_thread_param_t*)param;
session_dynamic_resource_t task_param;
while (thr_param->pool->fetch_work(&worker)) {
if (g_stopped) {
IVR_TRACE("thread [%lu] terminated!", pthread_self());
break;
}
if (worker) {
task_param.event_queue = thr_param->event_queue;
IVR_DEBUG("thread_execute event queue pointer %lu, queue size: %lu",
(uint64_t)task_param.event_queue, task_param.event_queue->size());
// task_param.sem = thr_param->evt_sem;
task_param.timer = thr_param->timer;
if (task_param.timer != NULL) {
task_param.timer->clear_all_timer();
}
IVR_TRACE("pop worker address:[%lu]", (uint64_t)worker);
worker->execute(&task_param);
delete worker;
worker = NULL;
}
{
::lock_t lock(thr_param->pool->_mutex_work_completion);
if (lock.locked()) {
IVR_TRACE("thread [%lu] has finished a work!", pthread_self());
thr_param->pool->_unfinished_work--;
} else {
IVR_WARN("locked failed");
}
}
if (ThreadPool::get_instance()->is_too_many()) {
IVR_TRACE("thread [%lu] don't need to run, terminated", pthread_self());
break;
}
}
// delete params
IVR_DEBUG("free thread [%lu]'s resource(event queue, timer, param)!", pthread_self());
if (param != NULL) {
if (thr_param->event_queue != NULL) {
delete thr_param->event_queue;
thr_param->event_queue = NULL;
}
if (thr_param->timer != NULL) {
delete thr_param->timer;
thr_param->timer = NULL;
}
delete thr_param;
param = NULL;
}
IVR_TRACE("thread [%lu] Terminated!", pthread_self());
ThreadPool::get_instance()->thread_end();
return 0;
}
bool ThreadPool::is_too_many() const {
::lock_t lock(_mutex_sync);
if (lock.locked()) {
IVR_DEBUG("too many? current(%u), max(%u)", _cur_thread_num, _max_thread_num);
return _max_thread_num < _cur_thread_num;
} else {
IVR_TRACE("locked failed!");
}
return false;
}
void ThreadPool::thread_end() {
IVR_TRACE("thread [%lu] end", pthread_self());
::lock_t lock(_mutex_sync);
if (lock.locked()) {
-- _cur_thread_num;
} else {
IVR_TRACE("locked failed!");
}
}