summaryrefslogtreecommitdiff
path: root/servers
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 /servers
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 'servers')
-rw-r--r--servers/audio/audio_driver_dummy.cpp12
-rw-r--r--servers/audio/audio_driver_dummy.h2
-rw-r--r--servers/audio_server.cpp19
-rw-r--r--servers/audio_server.h2
-rw-r--r--servers/physics_2d/physics_2d_server_wrap_mt.cpp2
-rw-r--r--servers/physics_2d/physics_2d_server_wrap_mt.h2
-rw-r--r--servers/server_wrap_mt_common.h18
-rw-r--r--servers/visual/rasterizer_rd/shader_rd.cpp12
-rw-r--r--servers/visual/rasterizer_rd/shader_rd.h4
-rw-r--r--servers/visual/visual_server_wrap_mt.cpp2
-rw-r--r--servers/visual/visual_server_wrap_mt.h2
11 files changed, 30 insertions, 47 deletions
diff --git a/servers/audio/audio_driver_dummy.cpp b/servers/audio/audio_driver_dummy.cpp
index 5389c64099..69b098edfc 100644
--- a/servers/audio/audio_driver_dummy.cpp
+++ b/servers/audio/audio_driver_dummy.cpp
@@ -49,7 +49,6 @@ Error AudioDriverDummy::init() {
samples_in = memnew_arr(int32_t, buffer_frames * channels);
- mutex = Mutex::create();
thread = Thread::create(AudioDriverDummy::thread_func, this);
return OK;
@@ -95,16 +94,16 @@ AudioDriver::SpeakerMode AudioDriverDummy::get_speaker_mode() const {
void AudioDriverDummy::lock() {
- if (!thread || !mutex)
+ if (!thread)
return;
- mutex->lock();
+ mutex.lock();
};
void AudioDriverDummy::unlock() {
- if (!thread || !mutex)
+ if (!thread)
return;
- mutex->unlock();
+ mutex.unlock();
};
void AudioDriverDummy::finish() {
@@ -120,14 +119,11 @@ void AudioDriverDummy::finish() {
};
memdelete(thread);
- if (mutex)
- memdelete(mutex);
thread = NULL;
};
AudioDriverDummy::AudioDriverDummy() {
- mutex = NULL;
thread = NULL;
};
diff --git a/servers/audio/audio_driver_dummy.h b/servers/audio/audio_driver_dummy.h
index ba99e5a239..a2cd9b2dc6 100644
--- a/servers/audio/audio_driver_dummy.h
+++ b/servers/audio/audio_driver_dummy.h
@@ -39,7 +39,7 @@
class AudioDriverDummy : public AudioDriver {
Thread *thread;
- Mutex *mutex;
+ Mutex mutex;
int32_t *samples_in;
diff --git a/servers/audio_server.cpp b/servers/audio_server.cpp
index abf351135a..caee5f5db5 100644
--- a/servers/audio_server.cpp
+++ b/servers/audio_server.cpp
@@ -1137,27 +1137,28 @@ void *AudioServer::audio_data_alloc(uint32_t p_data_len, const uint8_t *p_from_d
copymem(ad, p_from_data, p_data_len);
}
- audio_data_lock->lock();
- audio_data[ad] = p_data_len;
- audio_data_total_mem += p_data_len;
- audio_data_max_mem = MAX(audio_data_total_mem, audio_data_max_mem);
- audio_data_lock->unlock();
+ {
+ MutexLock lock(audio_data_lock);
+
+ audio_data[ad] = p_data_len;
+ audio_data_total_mem += p_data_len;
+ audio_data_max_mem = MAX(audio_data_total_mem, audio_data_max_mem);
+ }
return ad;
}
void AudioServer::audio_data_free(void *p_data) {
- audio_data_lock->lock();
+ MutexLock lock(audio_data_lock);
+
if (!audio_data.has(p_data)) {
- audio_data_lock->unlock();
ERR_FAIL();
}
audio_data_total_mem -= audio_data[p_data];
audio_data.erase(p_data);
memfree(p_data);
- audio_data_lock->unlock();
}
size_t AudioServer::audio_data_get_total_memory_usage() const {
@@ -1399,7 +1400,6 @@ AudioServer::AudioServer() {
singleton = this;
audio_data_total_mem = 0;
audio_data_max_mem = 0;
- audio_data_lock = Mutex::create();
mix_frames = 0;
channel_count = 0;
to_mix = 0;
@@ -1413,7 +1413,6 @@ AudioServer::AudioServer() {
AudioServer::~AudioServer() {
- memdelete(audio_data_lock);
singleton = NULL;
}
diff --git a/servers/audio_server.h b/servers/audio_server.h
index 1d9e5100fa..cc0c9d1112 100644
--- a/servers/audio_server.h
+++ b/servers/audio_server.h
@@ -238,7 +238,7 @@ private:
size_t audio_data_total_mem;
size_t audio_data_max_mem;
- Mutex *audio_data_lock;
+ Mutex audio_data_lock;
void init_channels_and_buffers();
diff --git a/servers/physics_2d/physics_2d_server_wrap_mt.cpp b/servers/physics_2d/physics_2d_server_wrap_mt.cpp
index 291693de39..7070902f77 100644
--- a/servers/physics_2d/physics_2d_server_wrap_mt.cpp
+++ b/servers/physics_2d/physics_2d_server_wrap_mt.cpp
@@ -160,7 +160,6 @@ Physics2DServerWrapMT::Physics2DServerWrapMT(Physics2DServer *p_contained, bool
step_sem = NULL;
step_pending = 0;
step_thread_up = false;
- alloc_mutex = Mutex::create();
pool_max_size = GLOBAL_GET("memory/limits/multithreaded_server/rid_pool_prealloc");
@@ -177,6 +176,5 @@ Physics2DServerWrapMT::Physics2DServerWrapMT(Physics2DServer *p_contained, bool
Physics2DServerWrapMT::~Physics2DServerWrapMT() {
memdelete(physics_2d_server);
- memdelete(alloc_mutex);
//finish();
}
diff --git a/servers/physics_2d/physics_2d_server_wrap_mt.h b/servers/physics_2d/physics_2d_server_wrap_mt.h
index 9a01344390..0648b8651f 100644
--- a/servers/physics_2d/physics_2d_server_wrap_mt.h
+++ b/servers/physics_2d/physics_2d_server_wrap_mt.h
@@ -67,7 +67,7 @@ class Physics2DServerWrapMT : public Physics2DServer {
bool first_frame;
- Mutex *alloc_mutex;
+ Mutex alloc_mutex;
int pool_max_size;
public:
diff --git a/servers/server_wrap_mt_common.h b/servers/server_wrap_mt_common.h
index f01e0b9578..4481b296c6 100644
--- a/servers/server_wrap_mt_common.h
+++ b/servers/server_wrap_mt_common.h
@@ -57,7 +57,7 @@
virtual RID m_type##_create() { \
if (Thread::get_caller_id() != server_thread) { \
RID rid; \
- alloc_mutex->lock(); \
+ MutexLock lock(alloc_mutex); \
if (m_type##_id_pool.size() == 0) { \
int ret; \
command_queue.push_and_ret(this, &ServerNameWrapMT::m_type##allocn, &ret); \
@@ -65,7 +65,6 @@
} \
rid = m_type##_id_pool.front()->get(); \
m_type##_id_pool.pop_front(); \
- alloc_mutex->unlock(); \
return rid; \
} else { \
return server_name->m_type##_create(); \
@@ -88,7 +87,7 @@
virtual RID m_type##_create(m_arg1 p1) { \
if (Thread::get_caller_id() != server_thread) { \
RID rid; \
- alloc_mutex->lock(); \
+ MutexLock lock(alloc_mutex); \
if (m_type##_id_pool.size() == 0) { \
int ret; \
command_queue.push_and_ret(this, &ServerNameWrapMT::m_type##allocn, p1, &ret); \
@@ -96,7 +95,6 @@
} \
rid = m_type##_id_pool.front()->get(); \
m_type##_id_pool.pop_front(); \
- alloc_mutex->unlock(); \
return rid; \
} else { \
return server_name->m_type##_create(p1); \
@@ -119,7 +117,7 @@
virtual RID m_type##_create(m_arg1 p1, m_arg2 p2) { \
if (Thread::get_caller_id() != server_thread) { \
RID rid; \
- alloc_mutex->lock(); \
+ MutexLock lock(alloc_mutex); \
if (m_type##_id_pool.size() == 0) { \
int ret; \
command_queue.push_and_ret(this, &ServerNameWrapMT::m_type##allocn, p1, p2, &ret); \
@@ -127,7 +125,6 @@
} \
rid = m_type##_id_pool.front()->get(); \
m_type##_id_pool.pop_front(); \
- alloc_mutex->unlock(); \
return rid; \
} else { \
return server_name->m_type##_create(p1, p2); \
@@ -150,7 +147,7 @@
virtual RID m_type##_create(m_arg1 p1, m_arg2 p2, m_arg3 p3) { \
if (Thread::get_caller_id() != server_thread) { \
RID rid; \
- alloc_mutex->lock(); \
+ MutexLock lock(alloc_mutex); \
if (m_type##_id_pool.size() == 0) { \
int ret; \
command_queue.push_and_ret(this, &ServerNameWrapMT::m_type##allocn, p1, p2, p3, &ret); \
@@ -158,7 +155,6 @@
} \
rid = m_type##_id_pool.front()->get(); \
m_type##_id_pool.pop_front(); \
- alloc_mutex->unlock(); \
return rid; \
} else { \
return server_name->m_type##_create(p1, p2, p3); \
@@ -181,7 +177,7 @@
virtual RID m_type##_create(m_arg1 p1, m_arg2 p2, m_arg3 p3, m_arg4 p4) { \
if (Thread::get_caller_id() != server_thread) { \
RID rid; \
- alloc_mutex->lock(); \
+ MutexLock lock(alloc_mutex); \
if (m_type##_id_pool.size() == 0) { \
int ret; \
command_queue.push_and_ret(this, &ServerNameWrapMT::m_type##allocn, p1, p2, p3, p4, &ret); \
@@ -189,7 +185,6 @@
} \
rid = m_type##_id_pool.front()->get(); \
m_type##_id_pool.pop_front(); \
- alloc_mutex->unlock(); \
return rid; \
} else { \
return server_name->m_type##_create(p1, p2, p3, p4); \
@@ -213,7 +208,7 @@
virtual RID m_type##_create(m_arg1 p1, m_arg2 p2, m_arg3 p3, m_arg4 p4, m_arg5 p5) { \
if (Thread::get_caller_id() != server_thread) { \
RID rid; \
- alloc_mutex->lock(); \
+ MutexLock lock(alloc_mutex); \
if (m_type##_id_pool.size() == 0) { \
int ret; \
command_queue.push_and_ret(this, &ServerNameWrapMT::m_type##allocn, p1, p2, p3, p4, p5, &ret); \
@@ -221,7 +216,6 @@
} \
rid = m_type##_id_pool.front()->get(); \
m_type##_id_pool.pop_front(); \
- alloc_mutex->unlock(); \
return rid; \
} else { \
return server_name->m_type##_create(p1, p2, p3, p4, p5); \
diff --git a/servers/visual/rasterizer_rd/shader_rd.cpp b/servers/visual/rasterizer_rd/shader_rd.cpp
index cc6c13f598..857a29f7f4 100644
--- a/servers/visual/rasterizer_rd/shader_rd.cpp
+++ b/servers/visual/rasterizer_rd/shader_rd.cpp
@@ -342,23 +342,21 @@ void ShaderRD::_compile_variant(uint32_t p_variant, Version *p_version) {
}
if (!build_ok) {
- variant_set_mutex.lock(); //properly print the errors
+ MutexLock lock(variant_set_mutex); //properly print the errors
ERR_PRINT("Error compiling " + String(current_stage == RD::SHADER_STAGE_COMPUTE ? "Compute " : (current_stage == RD::SHADER_STAGE_VERTEX ? "Vertex" : "Fragment")) + " shader, variant #" + itos(p_variant) + " (" + variant_defines[p_variant].get_data() + ").");
ERR_PRINT(error);
#ifdef DEBUG_ENABLED
ERR_PRINT("code:\n" + current_source.get_with_code_lines());
#endif
-
- variant_set_mutex.unlock();
return;
}
RID shader = RD::get_singleton()->shader_create(stages);
-
- variant_set_mutex.lock();
- p_version->variants[p_variant] = shader;
- variant_set_mutex.unlock();
+ {
+ MutexLock lock(variant_set_mutex);
+ p_version->variants[p_variant] = shader;
+ }
}
void ShaderRD::_compile_version(Version *p_version) {
diff --git a/servers/visual/rasterizer_rd/shader_rd.h b/servers/visual/rasterizer_rd/shader_rd.h
index 8581b85ff4..6635b08cc8 100644
--- a/servers/visual/rasterizer_rd/shader_rd.h
+++ b/servers/visual/rasterizer_rd/shader_rd.h
@@ -33,11 +33,11 @@
#include "core/hash_map.h"
#include "core/map.h"
+#include "core/os/mutex.h"
#include "core/rid_owner.h"
#include "core/variant.h"
#include <stdio.h>
-#include <mutex>
/**
@author Juan Linietsky <reduzio@gmail.com>
*/
@@ -66,7 +66,7 @@ class ShaderRD {
bool initialize_needed;
};
- std::mutex variant_set_mutex;
+ Mutex variant_set_mutex;
void _compile_variant(uint32_t p_variant, Version *p_version);
diff --git a/servers/visual/visual_server_wrap_mt.cpp b/servers/visual/visual_server_wrap_mt.cpp
index 9a5e5e896a..90d301c0cd 100644
--- a/servers/visual/visual_server_wrap_mt.cpp
+++ b/servers/visual/visual_server_wrap_mt.cpp
@@ -180,7 +180,6 @@ VisualServerWrapMT::VisualServerWrapMT(VisualServer *p_contained, bool p_create_
thread = NULL;
draw_pending = 0;
draw_thread_up = false;
- alloc_mutex = Mutex::create();
pool_max_size = GLOBAL_GET("memory/limits/multithreaded_server/rid_pool_prealloc");
if (!p_create_thread) {
@@ -193,6 +192,5 @@ VisualServerWrapMT::VisualServerWrapMT(VisualServer *p_contained, bool p_create_
VisualServerWrapMT::~VisualServerWrapMT() {
memdelete(visual_server);
- memdelete(alloc_mutex);
//finish();
}
diff --git a/servers/visual/visual_server_wrap_mt.h b/servers/visual/visual_server_wrap_mt.h
index 9d5281b17a..2eaafe220b 100644
--- a/servers/visual/visual_server_wrap_mt.h
+++ b/servers/visual/visual_server_wrap_mt.h
@@ -57,7 +57,7 @@ class VisualServerWrapMT : public VisualServer {
void thread_exit();
- Mutex *alloc_mutex;
+ Mutex alloc_mutex;
int pool_max_size;