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`.
This commit is contained in:
Pedro J. Estébanez 2020-02-26 11:28:13 +01:00
parent 1e57b558f2
commit 18fbdbb456
98 changed files with 739 additions and 1754 deletions

View file

@ -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