summaryrefslogtreecommitdiff
path: root/modules/gdnavigation/gd_navigation_server.cpp
diff options
context:
space:
mode:
authorPedro J. Estébanez <pedrojrulez@gmail.com>2020-02-26 11:28:13 +0100
committerPedro J. Estébanez <pedrojrulez@gmail.com>2020-02-26 20:40:10 +0100
commit18fbdbb456c07a56b358bea2e392765fbcbb3283 (patch)
tree737363d20493afe45e75d932e0c1957dd9a79589 /modules/gdnavigation/gd_navigation_server.cpp
parent1e57b558f215dd4920768e9567b6f55825877c89 (diff)
Reimplement Mutex with C++'s <mutex>
Main: - It's now implemented thanks to `<mutex>`. 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`.
Diffstat (limited to 'modules/gdnavigation/gd_navigation_server.cpp')
-rw-r--r--modules/gdnavigation/gd_navigation_server.cpp32
1 files changed, 11 insertions, 21 deletions
diff --git a/modules/gdnavigation/gd_navigation_server.cpp b/modules/gdnavigation/gd_navigation_server.cpp
index 4129d1f65a..4db10cda78 100644
--- a/modules/gdnavigation/gd_navigation_server.cpp
+++ b/modules/gdnavigation/gd_navigation_server.cpp
@@ -115,31 +115,27 @@
GdNavigationServer::GdNavigationServer() :
NavigationServer(),
- commands_mutex(Mutex::create()),
- operations_mutex(Mutex::create()),
active(true) {
}
GdNavigationServer::~GdNavigationServer() {
flush_queries();
- memdelete(operations_mutex);
- memdelete(commands_mutex);
}
void GdNavigationServer::add_command(SetCommand *command) const {
auto mut_this = const_cast<GdNavigationServer *>(this);
- commands_mutex->lock();
- mut_this->commands.push_back(command);
- commands_mutex->unlock();
+ {
+ MutexLock lock(commands_mutex);
+ mut_this->commands.push_back(command);
+ }
}
RID GdNavigationServer::map_create() const {
auto mut_this = const_cast<GdNavigationServer *>(this);
- mut_this->operations_mutex->lock();
+ MutexLock lock(mut_this->operations_mutex);
NavMap *space = memnew(NavMap);
RID rid = map_owner.make_rid(space);
space->set_self(rid);
- mut_this->operations_mutex->unlock();
return rid;
}
@@ -242,11 +238,10 @@ RID GdNavigationServer::map_get_closest_point_owner(RID p_map, const Vector3 &p_
RID GdNavigationServer::region_create() const {
auto mut_this = const_cast<GdNavigationServer *>(this);
- mut_this->operations_mutex->lock();
+ MutexLock lock(mut_this->operations_mutex);
NavRegion *reg = memnew(NavRegion);
RID rid = region_owner.make_rid(reg);
reg->set_self(rid);
- mut_this->operations_mutex->unlock();
return rid;
}
@@ -298,11 +293,10 @@ void GdNavigationServer::region_bake_navmesh(Ref<NavigationMesh> r_mesh, Node *p
RID GdNavigationServer::agent_create() const {
auto mut_this = const_cast<GdNavigationServer *>(this);
- mut_this->operations_mutex->lock();
+ MutexLock lock(mut_this->operations_mutex);
RvoAgent *agent = memnew(RvoAgent());
RID rid = agent_owner.make_rid(agent);
agent->set_self(rid);
- mut_this->operations_mutex->unlock();
return rid;
}
@@ -470,23 +464,20 @@ COMMAND_1(free, RID, p_object) {
void GdNavigationServer::set_active(bool p_active) const {
auto mut_this = const_cast<GdNavigationServer *>(this);
- mut_this->operations_mutex->lock();
+ MutexLock lock(mut_this->operations_mutex);
mut_this->active = p_active;
- mut_this->operations_mutex->unlock();
}
void GdNavigationServer::flush_queries() {
// In c++ we can't be sure that this is performed in the main thread
// even with mutable functions.
- commands_mutex->lock();
- operations_mutex->lock();
+ MutexLock lock(commands_mutex);
+ MutexLock lock2(operations_mutex);
for (size_t i(0); i < commands.size(); i++) {
commands[i]->exec(this);
memdelete(commands[i]);
}
commands.clear();
- operations_mutex->unlock();
- commands_mutex->unlock();
}
void GdNavigationServer::process(real_t p_delta_time) {
@@ -498,13 +489,12 @@ void GdNavigationServer::process(real_t p_delta_time) {
// In c++ we can't be sure that this is performed in the main thread
// even with mutable functions.
- operations_mutex->lock();
+ MutexLock lock(operations_mutex);
for (int i(0); i < active_maps.size(); i++) {
active_maps[i]->sync();
active_maps[i]->step(p_delta_time);
active_maps[i]->dispatch_callbacks();
}
- operations_mutex->unlock();
}
#undef COMMAND_1