From 18fbdbb456c07a56b358bea2e392765fbcbb3283 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20J=2E=20Est=C3=A9banez?= Date: Wed, 26 Feb 2020 11:28:13 +0100 Subject: Reimplement Mutex with C++'s Main: - It's now implemented thanks to ``. No more platform-specific implementations. - `BinaryMutex` (non-recursive) is added, as an alternative for special cases. - Doesn't need allocation/deallocation anymore. It can live in the stack and be part of other classes. - Because of that, it's methods are now `const` and the inner mutex is `mutable` so it can be easily used in `const` contexts. - A no-op implementation is provided if `NO_THREADS` is defined. No more need to add `#ifdef NO_THREADS` just for this. - `MutexLock` now takes a reference. At this point the cases of null `Mutex`es are rare. If you ever need that, just don't use `MutexLock`. - Thread-safe utilities are therefore simpler now. Misc.: - `ScopedMutexLock` is dropped and replaced by `MutexLock`, because they were pretty much the same. - Every case of lock, do-something, unlock is replaced by `MutexLock` (complex cases where it's not straightfoward are kept as as explicit lock and unlock). - `ShaderRD` contained an `std::mutex`, which has been replaced by `Mutex`. --- scene/2d/navigation_polygon.cpp | 44 ++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 20 deletions(-) (limited to 'scene/2d/navigation_polygon.cpp') diff --git a/scene/2d/navigation_polygon.cpp b/scene/2d/navigation_polygon.cpp index 6754c1c9a6..41f29c4072 100644 --- a/scene/2d/navigation_polygon.cpp +++ b/scene/2d/navigation_polygon.cpp @@ -82,9 +82,10 @@ bool NavigationPolygon::_edit_is_selected_on_click(const Point2 &p_point, double void NavigationPolygon::set_vertices(const Vector &p_vertices) { - navmesh_generation->lock(); - navmesh.unref(); - navmesh_generation->unlock(); + { + MutexLock lock(navmesh_generation); + navmesh.unref(); + } vertices = p_vertices; rect_cache_dirty = true; } @@ -96,9 +97,10 @@ Vector NavigationPolygon::get_vertices() const { void NavigationPolygon::_set_polygons(const Array &p_array) { - navmesh_generation->lock(); - navmesh.unref(); - navmesh_generation->unlock(); + { + MutexLock lock(navmesh_generation); + navmesh.unref(); + } polygons.resize(p_array.size()); for (int i = 0; i < p_array.size(); i++) { polygons.write[i].indices = p_array[i]; @@ -141,9 +143,10 @@ void NavigationPolygon::add_polygon(const Vector &p_polygon) { Polygon polygon; polygon.indices = p_polygon; polygons.push_back(polygon); - navmesh_generation->lock(); - navmesh.unref(); - navmesh_generation->unlock(); + { + MutexLock lock(navmesh_generation); + navmesh.unref(); + } } void NavigationPolygon::add_outline_at_index(const Vector &p_outline, int p_index) { @@ -164,13 +167,15 @@ Vector NavigationPolygon::get_polygon(int p_idx) { void NavigationPolygon::clear_polygons() { polygons.clear(); - navmesh_generation->lock(); - navmesh.unref(); - navmesh_generation->unlock(); + { + MutexLock lock(navmesh_generation); + navmesh.unref(); + } } Ref NavigationPolygon::get_mesh() { - navmesh_generation->lock(); + MutexLock lock(navmesh_generation); + if (navmesh.is_null()) { navmesh.instance(); Vector verts; @@ -190,7 +195,7 @@ Ref NavigationPolygon::get_mesh() { navmesh->add_polygon(get_polygon(i)); } } - navmesh_generation->unlock(); + return navmesh; } @@ -230,9 +235,10 @@ void NavigationPolygon::clear_outlines() { } void NavigationPolygon::make_polygons_from_outlines() { - navmesh_generation->lock(); - navmesh.unref(); - navmesh_generation->unlock(); + { + MutexLock lock(navmesh_generation); + navmesh.unref(); + } List in_poly, out_poly; Vector2 outside_point(-1e10, -1e10); @@ -362,12 +368,10 @@ void NavigationPolygon::_bind_methods() { } NavigationPolygon::NavigationPolygon() : - rect_cache_dirty(true), - navmesh_generation(Mutex::create()) { + rect_cache_dirty(true) { } NavigationPolygon::~NavigationPolygon() { - memdelete(navmesh_generation); } void NavigationPolygonInstance::set_enabled(bool p_enabled) { -- cgit v1.2.3