Skip to content

Commit 3b41da2

Browse files
committed
Make navigation map spatial queries thread-safe
Makes navigation map spatial queries thread-safe by adding a readers–writer lock.
1 parent 7a173af commit 3b41da2

2 files changed

Lines changed: 30 additions & 5 deletions

File tree

modules/navigation/nav_map.cpp

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,11 @@ gd::PointKey NavMap::get_point_key(const Vector3 &p_pos) const {
116116
}
117117

118118
Vector<Vector3> NavMap::get_path(Vector3 p_origin, Vector3 p_destination, bool p_optimize, uint32_t p_navigation_layers, Vector<int32_t> *r_path_types, TypedArray<RID> *r_path_rids, Vector<int64_t> *r_path_owners) const {
119-
ERR_FAIL_COND_V_MSG(map_update_id == 0, Vector<Vector3>(), "NavigationServer map query failed because it was made before first map synchronization.");
119+
RWLockRead read_lock(map_rwlock);
120+
if (map_update_id == 0) {
121+
ERR_FAIL_V_MSG(Vector<Vector3>(), "NavigationServer map query failed because it was made before first map synchronization.");
122+
}
123+
120124
// Clear metadata outputs.
121125
if (r_path_types) {
122126
r_path_types->clear();
@@ -576,7 +580,11 @@ Vector<Vector3> NavMap::get_path(Vector3 p_origin, Vector3 p_destination, bool p
576580
}
577581

578582
Vector3 NavMap::get_closest_point_to_segment(const Vector3 &p_from, const Vector3 &p_to, const bool p_use_collision) const {
579-
ERR_FAIL_COND_V_MSG(map_update_id == 0, Vector3(), "NavigationServer map query failed because it was made before first map synchronization.");
583+
RWLockRead read_lock(map_rwlock);
584+
if (map_update_id == 0) {
585+
ERR_FAIL_V_MSG(Vector3(), "NavigationServer map query failed because it was made before first map synchronization.");
586+
}
587+
580588
bool use_collision = p_use_collision;
581589
Vector3 closest_point;
582590
real_t closest_point_d = FLT_MAX;
@@ -624,24 +632,35 @@ Vector3 NavMap::get_closest_point_to_segment(const Vector3 &p_from, const Vector
624632
}
625633

626634
Vector3 NavMap::get_closest_point(const Vector3 &p_point) const {
627-
ERR_FAIL_COND_V_MSG(map_update_id == 0, Vector3(), "NavigationServer map query failed because it was made before first map synchronization.");
635+
RWLockRead read_lock(map_rwlock);
636+
if (map_update_id == 0) {
637+
ERR_FAIL_V_MSG(Vector3(), "NavigationServer map query failed because it was made before first map synchronization.");
638+
}
628639
gd::ClosestPointQueryResult cp = get_closest_point_info(p_point);
629640
return cp.point;
630641
}
631642

632643
Vector3 NavMap::get_closest_point_normal(const Vector3 &p_point) const {
633-
ERR_FAIL_COND_V_MSG(map_update_id == 0, Vector3(), "NavigationServer map query failed because it was made before first map synchronization.");
644+
RWLockRead read_lock(map_rwlock);
645+
if (map_update_id == 0) {
646+
ERR_FAIL_V_MSG(Vector3(), "NavigationServer map query failed because it was made before first map synchronization.");
647+
}
634648
gd::ClosestPointQueryResult cp = get_closest_point_info(p_point);
635649
return cp.normal;
636650
}
637651

638652
RID NavMap::get_closest_point_owner(const Vector3 &p_point) const {
639-
ERR_FAIL_COND_V_MSG(map_update_id == 0, RID(), "NavigationServer map query failed because it was made before first map synchronization.");
653+
RWLockRead read_lock(map_rwlock);
654+
if (map_update_id == 0) {
655+
ERR_FAIL_V_MSG(RID(), "NavigationServer map query failed because it was made before first map synchronization.");
656+
}
640657
gd::ClosestPointQueryResult cp = get_closest_point_info(p_point);
641658
return cp.owner;
642659
}
643660

644661
gd::ClosestPointQueryResult NavMap::get_closest_point_info(const Vector3 &p_point) const {
662+
RWLockRead read_lock(map_rwlock);
663+
645664
gd::ClosestPointQueryResult result;
646665
real_t closest_point_ds = FLT_MAX;
647666

@@ -770,6 +789,8 @@ void NavMap::remove_agent_as_controlled(NavAgent *agent) {
770789
}
771790

772791
Vector3 NavMap::get_random_point(uint32_t p_navigation_layers, bool p_uniformly) const {
792+
RWLockRead read_lock(map_rwlock);
793+
773794
const LocalVector<NavRegion *> map_regions = get_regions();
774795

775796
if (map_regions.is_empty()) {
@@ -834,6 +855,8 @@ Vector3 NavMap::get_random_point(uint32_t p_navigation_layers, bool p_uniformly)
834855
}
835856

836857
void NavMap::sync() {
858+
RWLockWrite write_lock(map_rwlock);
859+
837860
// Performance Monitor
838861
int _new_pm_region_count = regions.size();
839862
int _new_pm_agent_count = agents.size();

modules/navigation/nav_map.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ class NavAgent;
4848
class NavObstacle;
4949

5050
class NavMap : public NavRid {
51+
RWLock map_rwlock;
52+
5153
/// Map Up
5254
Vector3 up = Vector3(0, 1, 0);
5355

0 commit comments

Comments
 (0)