Apollo
10.0
自动驾驶开放平台
prediction_thread_pool.cc
浏览该文件的文档.
1
/******************************************************************************
2
* Copyright 2019 The Apollo Authors. All Rights Reserved.
3
*
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
7
*
8
* http://www.apache.org/licenses/LICENSE-2.0
9
*
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
15
*****************************************************************************/
16
17
#include "
modules/prediction/common/prediction_thread_pool.h
"
18
19
namespace
apollo
{
20
namespace
prediction {
21
22
thread_local
int
PredictionThreadPool::s_thread_pool_level
= 0;
23
std::vector<int>
BaseThreadPool::THREAD_POOL_CAPACITY
= {20, 20, 20};
24
25
BaseThreadPool::BaseThreadPool
(
int
thread_num,
int
next_thread_pool_level)
26
: stopped_(false) {
27
if
(!task_queue_.
Init
(thread_num,
28
new
apollo::cyber::base::BlockWaitStrategy
())) {
29
throw
std::runtime_error(
"Task queue init failed."
);
30
}
31
for
(
int
i = 0; i < thread_num; ++i) {
32
workers_.emplace_back([
this
, next_thread_pool_level, i] {
33
PredictionThreadPool::s_thread_pool_level
= next_thread_pool_level;
34
while
(!stopped_) {
35
std::function<void()> task;
36
if
(task_queue_.
WaitDequeue
(&task)) {
37
task();
38
}
39
}
40
});
41
}
42
}
43
44
void
BaseThreadPool::Stop
() {
45
task_queue_.
BreakAllWait
();
46
for
(std::thread& worker : workers_) {
47
worker.join();
48
}
49
stopped_ =
true
;
50
}
51
52
BaseThreadPool::~BaseThreadPool
() {
53
if
(stopped_.exchange(
true
)) {
54
return
;
55
}
56
task_queue_.
BreakAllWait
();
57
for
(std::thread& worker : workers_) {
58
worker.join();
59
}
60
}
61
62
BaseThreadPool
*
PredictionThreadPool::Instance
() {
63
int
pool_level =
s_thread_pool_level
;
64
int
max_thread_pool_level =
65
static_cast<
int
>
(
BaseThreadPool::THREAD_POOL_CAPACITY
.size());
66
CHECK_LT(pool_level, max_thread_pool_level);
67
int
index =
s_thread_pool_level
;
68
switch
(index) {
69
case
0: {
70
return
LevelThreadPool<0>::Instance
();
71
}
72
case
1: {
73
return
LevelThreadPool<1>::Instance
();
74
}
75
case
2: {
76
return
LevelThreadPool<2>::Instance
();
77
}
78
}
79
AERROR
<<
"Should not hit here"
;
80
return
LevelThreadPool<0>::Instance
();
81
}
82
83
}
// namespace prediction
84
}
// namespace apollo
apollo::cyber::base::BlockWaitStrategy
Definition
wait_strategy.h:38
apollo::cyber::base::BoundedQueue::Init
bool Init(uint64_t size)
Definition
bounded_queue.h:90
apollo::cyber::base::BoundedQueue::BreakAllWait
void BreakAllWait()
Definition
bounded_queue.h:240
apollo::cyber::base::BoundedQueue::WaitDequeue
bool WaitDequeue(T *element)
Definition
bounded_queue.h:204
apollo::prediction::BaseThreadPool
Definition
prediction_thread_pool.h:34
apollo::prediction::BaseThreadPool::~BaseThreadPool
~BaseThreadPool()
Definition
prediction_thread_pool.cc:52
apollo::prediction::BaseThreadPool::BaseThreadPool
BaseThreadPool(int thread_num, int next_thread_pool_level)
Definition
prediction_thread_pool.cc:25
apollo::prediction::BaseThreadPool::Stop
void Stop()
Definition
prediction_thread_pool.cc:44
apollo::prediction::BaseThreadPool::THREAD_POOL_CAPACITY
static std::vector< int > THREAD_POOL_CAPACITY
Definition
prediction_thread_pool.h:77
apollo::prediction::LevelThreadPool::Instance
static LevelThreadPool * Instance()
Definition
prediction_thread_pool.h:88
apollo::prediction::PredictionThreadPool::s_thread_pool_level
static thread_local int s_thread_pool_level
Definition
prediction_thread_pool.h:104
apollo::prediction::PredictionThreadPool::Instance
static BaseThreadPool * Instance()
Definition
prediction_thread_pool.cc:62
AERROR
#define AERROR
Definition
log.h:44
apollo
class register implement
Definition
arena_queue.h:37
prediction_thread_pool.h
modules
prediction
common
prediction_thread_pool.cc