Skip to content

Commit

Permalink
Add reader functions to cache classes to mitigate direct data members…
Browse files Browse the repository at this point in the history
… access and having to lock the mutex in different places

Signed-off-by: Mateusz Palczuk <[email protected]>
  • Loading branch information
TauTheLepton committed Jan 23, 2025
1 parent fa8a552 commit 1649df0
Showing 1 changed file with 33 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ class RouteCache
shortest_path_ids);
}
}
std::lock_guard lock(mutex_);
return data_.at({from_lanelet_id, to_lanelet_id, routing_configuration.allow_lane_change});
return readData(from_lanelet_id, to_lanelet_id, routing_configuration.allow_lane_change);
}

auto getRoute(const lanelet::Id from, const lanelet::Id to, const bool allow_lane_change)
Expand All @@ -107,8 +106,7 @@ class RouteCache
"route from : ", from, " to : ", to, (allow_lane_change ? " with" : " without"),
" lane change does not exists on route cache.");
} else {
std::lock_guard lock(mutex_);
return data_.at({from, to, allow_lane_change});
return readData(from, to, allow_lane_change);
}
}

Expand All @@ -120,6 +118,13 @@ class RouteCache
return data_.find(key) != data_.end();
}

auto readData(const lanelet::Id from, const lanelet::Id to, const bool allow_lane_change)
-> lanelet::Ids
{
std::lock_guard lock(mutex_);
return data_.at({from, to, allow_lane_change});
}

auto appendData(
const lanelet::Id from, const lanelet::Id to, const bool allow_lane_change,
const lanelet::Ids & route) -> void
Expand All @@ -140,17 +145,15 @@ class CenterPointsCache
if (!exists(lanelet_id)) {
THROW_SIMULATION_ERROR("center point of : ", lanelet_id, " does not exists on route cache.");
}
std::lock_guard lock(mutex_);
return data_.at(lanelet_id);
return readData(lanelet_id);
}

auto centerPointsSpline(lanelet::Id lanelet_id) -> decltype(auto)
{
if (!exists(lanelet_id)) {
THROW_SIMULATION_ERROR("center point of : ", lanelet_id, " does not exists on route cache.");
}
std::lock_guard lock(mutex_);
return splines_.at(lanelet_id);
return readDataSpline(lanelet_id);
}

auto getCenterPoints(const lanelet::Id lanelet_id, const lanelet::LaneletMapPtr & lanelet_map)
Expand All @@ -159,8 +162,7 @@ class CenterPointsCache
if (!exists(lanelet_id)) {
appendData(lanelet_id, centerPoints(lanelet_id, lanelet_map));
}
std::lock_guard lock(mutex_);
return data_.at(lanelet_id);
return readData(lanelet_id);
}

auto getCenterPointsSpline(
Expand All @@ -170,8 +172,7 @@ class CenterPointsCache
if (!exists(lanelet_id)) {
appendData(lanelet_id, centerPoints(lanelet_id, lanelet_map));
}
std::lock_guard lock(mutex_);
return splines_.at(lanelet_id);
return readDataSpline(lanelet_id);
}

private:
Expand All @@ -181,6 +182,18 @@ class CenterPointsCache
return data_.find(lanelet_id) != data_.end();
}

auto readData(const lanelet::Id lanelet_id) -> std::vector<Point>
{
std::lock_guard lock(mutex_);
return data_.at(lanelet_id);
}

auto readDataSpline(const lanelet::Id lanelet_id) -> std::shared_ptr<Spline>
{
std::lock_guard lock(mutex_);
return splines_.at(lanelet_id);
}

auto appendData(const lanelet::Id lanelet_id, const std::vector<Point> & route) -> void
{
std::lock_guard lock(mutex_);
Expand Down Expand Up @@ -223,8 +236,7 @@ class LaneletLengthCache
if (!exists(lanelet_id)) {
THROW_SIMULATION_ERROR("length of : ", lanelet_id, " does not exists on route cache.");
}
std::lock_guard lock(mutex_);
return data_.at(lanelet_id);
return readData(lanelet_id);
}

auto getLength(const lanelet::Id lanelet_id, const lanelet::LaneletMapPtr & lanelet_map) -> double
Expand All @@ -233,8 +245,7 @@ class LaneletLengthCache
appendData(
lanelet_id, lanelet::utils::getLaneletLength2d(lanelet_map->laneletLayer.get(lanelet_id)));
}
std::lock_guard lock(mutex_);
return data_.at(lanelet_id);
return readData(lanelet_id);
}

private:
Expand All @@ -244,6 +255,12 @@ class LaneletLengthCache
return data_.find(lanelet_id) != data_.end();
}

auto readData(const lanelet::Id lanelet_id) -> double
{
std::lock_guard lock(mutex_);
return data_.at(lanelet_id);
}

auto appendData(const lanelet::Id lanelet_id, double length) -> void
{
std::lock_guard lock(mutex_);
Expand Down

0 comments on commit 1649df0

Please sign in to comment.