threadpool.cpp
2.17 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
/*
* 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 "threadpool.h"
#include "event_handler.h"
#include "ims_log.h"
using namespace bgcc;
namespace ims {
threadpool_t::threadpool_t(int32_t size, int32_t timeout):
_size(size),
_timeout(timeout) {
}
threadpool_t::~threadpool_t() {
this->stop();
_threads.clear();
}
void threadpool_t::start() {
if (_isstart) {
WARNING_LOG("threadpool_t already started, do nothing.");
return;
}
TRACE_LOG("threadpool_t start size:%d, tasktimeout:%d", _size, _timeout);
for (int32_t i = 0; i < _size; ++i) {
TRACE_LOG("threadpool_t starting session thread [%d]", i);
event_handler_ptr handler(new event_handler_t(_timeout));
session_thread_ptr thrd(new session_thread_t(handler));
thrd->start();
_threads.push_back(thrd);
}
TRACE_LOG("threadpool_t start ok");
_isstart = true;
}
void threadpool_t::stop() {
std::vector<session_thread_ptr>::iterator it;
for (it = _threads.begin(); it != _threads.end(); ++it) {
dynamic_cast<event_handler_t*>((*it)->get_runner().get())->shutdown();
(*it)->join();
}
}
session_thread_ptr threadpool_t::get_prefer_thread() {
Guard<Mutex> lock(&_mutex);
std::vector<session_thread_ptr>::const_iterator it;
session_thread_ptr target;
int32_t min = -1;
int32_t cur = 0;
for (it = _threads.begin(); it != _threads.end(); ++it) {
cur = (*it)->get_session_count();
if (cur < min || min < 0) {
min = cur;
target = *it;
}
}
return target;
}
}
/* vim: set ts=4 sw=4 sts=4 tw=100 noet: */