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`. --- drivers/alsamidi/midi_driver_alsamidi.cpp | 13 ++----------- drivers/alsamidi/midi_driver_alsamidi.h | 2 +- 2 files changed, 3 insertions(+), 12 deletions(-) (limited to 'drivers/alsamidi') diff --git a/drivers/alsamidi/midi_driver_alsamidi.cpp b/drivers/alsamidi/midi_driver_alsamidi.cpp index 670d7e0a4a..0cecf1de3e 100644 --- a/drivers/alsamidi/midi_driver_alsamidi.cpp +++ b/drivers/alsamidi/midi_driver_alsamidi.cpp @@ -148,7 +148,6 @@ Error MIDIDriverALSAMidi::open() { } snd_device_name_free_hint(hints); - mutex = Mutex::create(); exit_thread = false; thread = Thread::create(MIDIDriverALSAMidi::thread_func, this); @@ -165,11 +164,6 @@ void MIDIDriverALSAMidi::close() { thread = NULL; } - if (mutex) { - memdelete(mutex); - mutex = NULL; - } - for (int i = 0; i < connected_inputs.size(); i++) { snd_rawmidi_t *midi_in = connected_inputs[i]; snd_rawmidi_close(midi_in); @@ -179,14 +173,12 @@ void MIDIDriverALSAMidi::close() { void MIDIDriverALSAMidi::lock() const { - if (mutex) - mutex->lock(); + mutex.lock(); } void MIDIDriverALSAMidi::unlock() const { - if (mutex) - mutex->unlock(); + mutex.unlock(); } PackedStringArray MIDIDriverALSAMidi::get_connected_inputs() { @@ -210,7 +202,6 @@ PackedStringArray MIDIDriverALSAMidi::get_connected_inputs() { MIDIDriverALSAMidi::MIDIDriverALSAMidi() { - mutex = NULL; thread = NULL; exit_thread = false; diff --git a/drivers/alsamidi/midi_driver_alsamidi.h b/drivers/alsamidi/midi_driver_alsamidi.h index 9900d90553..6797c7c138 100644 --- a/drivers/alsamidi/midi_driver_alsamidi.h +++ b/drivers/alsamidi/midi_driver_alsamidi.h @@ -44,7 +44,7 @@ class MIDIDriverALSAMidi : public MIDIDriver { Thread *thread; - Mutex *mutex; + Mutex mutex; Vector connected_inputs; -- cgit v1.2.3