Skip to content

Commit

Permalink
fix: change to lock_guard
Browse files Browse the repository at this point in the history
Signed-off-by: badai-nguyen <dai.nguyen@tier4.jp>
  • Loading branch information
badai-nguyen committed Mar 5, 2025
1 parent 9d844df commit 52c5459
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ bool DistanceBasedStaticMapLoader::is_close_to_map(
bool DistanceBasedDynamicMapLoader::is_close_to_map(
const pcl::PointXYZ & point, const double distance_threshold)
{
std::lock_guard<std::mutex> lock(dynamic_map_loader_mutex_);

Check warning on line 79 in perception/autoware_compare_map_segmentation/src/distance_based_compare_map_filter/node.cpp

View check run for this annotation

Codecov / codecov/patch

perception/autoware_compare_map_segmentation/src/distance_based_compare_map_filter/node.cpp#L79

Added line #L79 was not covered by tests
if (current_voxel_grid_dict_.size() == 0) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ bool VoxelBasedApproximateStaticMapLoader::is_close_to_map(
bool VoxelBasedApproximateDynamicMapLoader::is_close_to_map(
const pcl::PointXYZ & point, [[maybe_unused]] const double distance_threshold)
{
std::lock_guard<std::mutex> lock(dynamic_map_loader_mutex_);

Check warning on line 48 in perception/autoware_compare_map_segmentation/src/voxel_based_approximate_compare_map_filter/node.cpp

View check run for this annotation

Codecov / codecov/patch

perception/autoware_compare_map_segmentation/src/voxel_based_approximate_compare_map_filter/node.cpp#L48

Added line #L48 was not covered by tests
if (current_voxel_grid_dict_.size() == 0) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ bool VoxelDistanceBasedStaticMapLoader::is_close_to_map(
bool VoxelDistanceBasedDynamicMapLoader::is_close_to_map(
const pcl::PointXYZ & point, const double distance_threshold)
{
std::lock_guard<std::mutex> lock(dynamic_map_loader_mutex_);

Check warning on line 83 in perception/autoware_compare_map_segmentation/src/voxel_distance_based_compare_map_filter/node.cpp

View check run for this annotation

Codecov / codecov/patch

perception/autoware_compare_map_segmentation/src/voxel_distance_based_compare_map_filter/node.cpp#L83

Added line #L83 was not covered by tests
if (current_voxel_grid_dict_.size() == 0) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,39 +355,36 @@ bool VoxelGridDynamicMapLoader::is_close_to_next_map_grid(
bool VoxelGridDynamicMapLoader::is_close_to_map(
const pcl::PointXYZ & point, const double distance_threshold)
{
// std::lock_guard<std::mutex> lock(dynamic_map_loader_mutex_);
dynamic_map_loader_mutex_.lock();
if (current_voxel_grid_dict_.size() == 0) {
dynamic_map_loader_mutex_.unlock();
return false;
}

// Compare point with map grid that point belong to

int map_grid_index = static_cast<int>(
std::floor((point.x - origin_x_) / map_grid_size_x_) +
map_grids_x_ * std::floor((point.y - origin_y_) / map_grid_size_y_));

if (static_cast<size_t>(map_grid_index) >= current_voxel_grid_array_.size()) {
dynamic_map_loader_mutex_.unlock();
return false;
}
if (
current_voxel_grid_array_.at(map_grid_index) != nullptr &&
is_close_to_neighbor_voxels(
point, distance_threshold, current_voxel_grid_array_.at(map_grid_index)->map_cell_pc_ptr,
current_voxel_grid_array_.at(map_grid_index)->map_cell_voxel_grid)) {
dynamic_map_loader_mutex_.unlock();
return true;
double origin_x, origin_y, map_grid_size_x, map_grid_size_y;
int map_grids_x, map_grid_index;
{
std::lock_guard<std::mutex> lock(dynamic_map_loader_mutex_);

Check warning on line 361 in perception/autoware_compare_map_segmentation/src/voxel_grid_map_loader/voxel_grid_map_loader.cpp

View check run for this annotation

Codecov / codecov/patch

perception/autoware_compare_map_segmentation/src/voxel_grid_map_loader/voxel_grid_map_loader.cpp#L361

Added line #L361 was not covered by tests
if (current_voxel_grid_dict_.size() == 0) {
return false;
}
origin_x = origin_x_;
origin_y = origin_y_;
map_grid_size_x = map_grid_size_x_;
map_grid_size_y = map_grid_size_y_;
map_grids_x = map_grids_x_;

Check warning on line 369 in perception/autoware_compare_map_segmentation/src/voxel_grid_map_loader/voxel_grid_map_loader.cpp

View check run for this annotation

Codecov / codecov/patch

perception/autoware_compare_map_segmentation/src/voxel_grid_map_loader/voxel_grid_map_loader.cpp#L365-L369

Added lines #L365 - L369 were not covered by tests
// Compare point with map grid that point belong to

map_grid_index = static_cast<int>(
std::floor((point.x - origin_x) / map_grid_size_x) +
map_grids_x * std::floor((point.y - origin_y) / map_grid_size_y));

Check warning on line 374 in perception/autoware_compare_map_segmentation/src/voxel_grid_map_loader/voxel_grid_map_loader.cpp

View check run for this annotation

Codecov / codecov/patch

perception/autoware_compare_map_segmentation/src/voxel_grid_map_loader/voxel_grid_map_loader.cpp#L372-L374

Added lines #L372 - L374 were not covered by tests

if (static_cast<size_t>(map_grid_index) >= current_voxel_grid_array_.size()) {
return false;
}
if (
current_voxel_grid_array_.at(map_grid_index) != nullptr &&
is_close_to_neighbor_voxels(
point, distance_threshold, current_voxel_grid_array_.at(map_grid_index)->map_cell_pc_ptr,

Check warning on line 382 in perception/autoware_compare_map_segmentation/src/voxel_grid_map_loader/voxel_grid_map_loader.cpp

View check run for this annotation

Codecov / codecov/patch

perception/autoware_compare_map_segmentation/src/voxel_grid_map_loader/voxel_grid_map_loader.cpp#L381-L382

Added lines #L381 - L382 were not covered by tests
current_voxel_grid_array_.at(map_grid_index)->map_cell_voxel_grid)) {
return true;
}
}

auto origin_x = origin_x_;
auto origin_y = origin_y_;
auto map_grid_size_x = map_grid_size_x_;
auto map_grid_size_y = map_grid_size_y_;
auto map_grids_x = map_grids_x_;
dynamic_map_loader_mutex_.unlock();

// Compare point with the neighbor map cells if point close to map cell boundary

if (is_close_to_next_map_grid(
Expand Down Expand Up @@ -417,9 +414,11 @@ bool VoxelGridDynamicMapLoader::is_close_to_map(
}
void VoxelGridDynamicMapLoader::timer_callback()
{
dynamic_map_loader_mutex_.lock();
auto current_position = current_position_;
dynamic_map_loader_mutex_.unlock();
std::optional<geometry_msgs::msg::Point> current_position = std::nullopt;

Check warning on line 417 in perception/autoware_compare_map_segmentation/src/voxel_grid_map_loader/voxel_grid_map_loader.cpp

View check run for this annotation

Codecov / codecov/patch

perception/autoware_compare_map_segmentation/src/voxel_grid_map_loader/voxel_grid_map_loader.cpp#L417

Added line #L417 was not covered by tests
{
std::lock_guard<std::mutex> lock(dynamic_map_loader_mutex_);
current_position = current_position_;

Check failure on line 420 in perception/autoware_compare_map_segmentation/src/voxel_grid_map_loader/voxel_grid_map_loader.cpp

View workflow job for this annotation

GitHub Actions / cppcheck-differential

Redundant initialization for 'current_position'. The initialized value is overwritten before it is read. [redundantInitialization]

Check warning on line 420 in perception/autoware_compare_map_segmentation/src/voxel_grid_map_loader/voxel_grid_map_loader.cpp

View check run for this annotation

Codecov / codecov/patch

perception/autoware_compare_map_segmentation/src/voxel_grid_map_loader/voxel_grid_map_loader.cpp#L419-L420

Added lines #L419 - L420 were not covered by tests
}

if (current_position == std::nullopt) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,22 +279,21 @@ class VoxelGridDynamicMapLoader : public VoxelGridMapLoader
virtual inline void addMapCellAndFilter(
const autoware_map_msgs::msg::PointCloudMapCellWithID & map_cell_to_add)
{
dynamic_map_loader_mutex_.lock();
map_grid_size_x_ = map_cell_to_add.metadata.max_x - map_cell_to_add.metadata.min_x;
map_grid_size_y_ = map_cell_to_add.metadata.max_y - map_cell_to_add.metadata.min_y;
if (map_grid_size_x_ > max_map_grid_size_ || map_grid_size_y_ > max_map_grid_size_) {
dynamic_map_loader_mutex_.unlock();
RCLCPP_ERROR(
logger_,
"Map was not split or split map grid size is too large. Split map with grid size smaller "
"than %f",
max_map_grid_size_);
}

origin_x_remainder_ = std::remainder(map_cell_to_add.metadata.min_x, map_grid_size_x_);
origin_y_remainder_ = std::remainder(map_cell_to_add.metadata.min_y, map_grid_size_y_);
dynamic_map_loader_mutex_.unlock();
{
std::lock_guard<std::mutex> lock(dynamic_map_loader_mutex_);
map_grid_size_x_ = map_cell_to_add.metadata.max_x - map_cell_to_add.metadata.min_x;
map_grid_size_y_ = map_cell_to_add.metadata.max_y - map_cell_to_add.metadata.min_y;

Check warning on line 285 in perception/autoware_compare_map_segmentation/src/voxel_grid_map_loader/voxel_grid_map_loader.hpp

View check run for this annotation

Codecov / codecov/patch

perception/autoware_compare_map_segmentation/src/voxel_grid_map_loader/voxel_grid_map_loader.hpp#L283-L285

Added lines #L283 - L285 were not covered by tests
if (map_grid_size_x_ > max_map_grid_size_ || map_grid_size_y_ > max_map_grid_size_) {
RCLCPP_ERROR(
logger_,
"Map was not split or split map grid size is too large. Split map with grid size smaller "
"than %f",
max_map_grid_size_);
}

origin_x_remainder_ = std::remainder(map_cell_to_add.metadata.min_x, map_grid_size_x_);
origin_y_remainder_ = std::remainder(map_cell_to_add.metadata.min_y, map_grid_size_y_);

Check warning on line 295 in perception/autoware_compare_map_segmentation/src/voxel_grid_map_loader/voxel_grid_map_loader.hpp

View check run for this annotation

Codecov / codecov/patch

perception/autoware_compare_map_segmentation/src/voxel_grid_map_loader/voxel_grid_map_loader.hpp#L294-L295

Added lines #L294 - L295 were not covered by tests
}
pcl::PointCloud<pcl::PointXYZ> map_cell_pc_tmp;
pcl::fromROSMsg(map_cell_to_add.pointcloud, map_cell_pc_tmp);

Expand Down

0 comments on commit 52c5459

Please sign in to comment.