Apollo 10.0
自动驾驶开放平台
base_map_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
18
22
23namespace apollo {
24namespace localization {
25namespace msf {
26namespace pyramid_map {
27
28BaseMapNodePool::BaseMapNodePool(unsigned int pool_size,
29 unsigned int thread_size)
30 : pool_size_(pool_size) {}
31
33
35 bool is_fixed_size) {
36 is_fixed_size_ = is_fixed_size;
37 map_config_ = map_config;
38 for (unsigned int i = 0; i < pool_size_; ++i) {
39 BaseMapNode* node = AllocNewMapNode();
40 InitNewMapNode(node);
41 free_list_.push_back(node);
42 }
43}
44
46 if (node_reset_workers_.valid()) {
48 }
49 for (BaseMapNode* node : free_list_) {
50 FinalizeMapNode(node);
51 DellocMapNode(node);
52 }
53 free_list_.clear();
54 for (BaseMapNode* node : busy_nodes_) {
55 FinalizeMapNode(node);
56 DellocMapNode(node);
57 }
58 busy_nodes_.clear();
59 pool_size_ = 0;
60}
61
63 if (free_list_.empty()) {
64 if (node_reset_workers_.valid()) {
66 }
67 }
68 boost::unique_lock<boost::mutex> lock(mutex_);
69 if (free_list_.empty()) {
70 if (is_fixed_size_) {
71 return nullptr;
72 }
73 BaseMapNode* node = AllocNewMapNode();
74 InitNewMapNode(node);
75 ++pool_size_;
76 busy_nodes_.insert(node);
77 return node;
78 } else {
79 BaseMapNode* node = free_list_.front();
80 free_list_.pop_front();
81 busy_nodes_.insert(node);
82 return node;
83 }
84}
85
88 cyber::Async(&BaseMapNodePool::FreeMapNodeTask, this, map_node);
89}
90
91void BaseMapNodePool::FreeMapNodeTask(BaseMapNode* map_node) {
92 FinalizeMapNode(map_node);
93 ResetMapNode(map_node);
94 {
95 boost::unique_lock<boost::mutex> lock(mutex_);
96 typename std::set<BaseMapNode*>::iterator f = busy_nodes_.find(map_node);
97 if (f == busy_nodes_.end()) {
98 throw "[BaseMapNodePool::free_map_node_task] f == busy_nodes_.end()";
99 }
100 free_list_.push_back(*f);
101 busy_nodes_.erase(f);
102 }
103}
104
105void BaseMapNodePool::InitNewMapNode(BaseMapNode* node) {
106 node->Init(map_config_);
107}
108
109void BaseMapNodePool::FinalizeMapNode(BaseMapNode* node) {
110 if (node != nullptr) {
111 node->Finalize();
112 }
113}
114
115void BaseMapNodePool::DellocMapNode(BaseMapNode* node) {
116 if (node != nullptr) {
117 delete node;
118 }
119}
120
121void BaseMapNodePool::ResetMapNode(BaseMapNode* node) {
122 if (node != nullptr) {
123 node->ResetMapNode();
124 }
125}
126
127} // namespace pyramid_map
128} // namespace msf
129} // namespace localization
130} // namespace apollo
double f
const BaseMapConfig * map_config_
The mutex for release thread.
BaseMapNode * AllocMapNode()
Get a MapNode object from memory pool.
std::set< BaseMapNode * > busy_nodes_
The set for used node.
BaseMapNodePool(unsigned int pool_size, unsigned int thread_size)
Constructor
boost::mutex mutex_
The mutex for release thread.
std::list< BaseMapNode * > free_list_
The list for free node.
std::future< void > node_reset_workers_
The thread pool for release node.
void FreeMapNode(BaseMapNode *map_node)
Release MapNode object to memory pool.
void Initial(const BaseMapConfig *map_config, bool is_fixed_size=true)
Initialize the pool.
unsigned int pool_size_
The size of memory pool.
The data structure of a Node in the map.
class register implement
Definition arena_queue.h:37