diff options
author | Pedro J. Estébanez <pedrojrulez@gmail.com> | 2020-02-26 11:28:13 +0100 |
---|---|---|
committer | Pedro J. Estébanez <pedrojrulez@gmail.com> | 2020-02-26 20:40:10 +0100 |
commit | 18fbdbb456c07a56b358bea2e392765fbcbb3283 (patch) | |
tree | 737363d20493afe45e75d932e0c1957dd9a79589 /drivers | |
parent | 1e57b558f215dd4920768e9567b6f55825877c89 (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 'drivers')
-rw-r--r-- | drivers/alsa/audio_driver_alsa.cpp | 15 | ||||
-rw-r--r-- | drivers/alsa/audio_driver_alsa.h | 2 | ||||
-rw-r--r-- | drivers/alsamidi/midi_driver_alsamidi.cpp | 13 | ||||
-rw-r--r-- | drivers/alsamidi/midi_driver_alsamidi.h | 2 | ||||
-rw-r--r-- | drivers/coreaudio/audio_driver_coreaudio.cpp | 18 | ||||
-rw-r--r-- | drivers/coreaudio/audio_driver_coreaudio.h | 2 | ||||
-rw-r--r-- | drivers/pulseaudio/audio_driver_pulseaudio.cpp | 14 | ||||
-rw-r--r-- | drivers/pulseaudio/audio_driver_pulseaudio.h | 2 | ||||
-rw-r--r-- | drivers/unix/mutex_posix.cpp | 73 | ||||
-rw-r--r-- | drivers/unix/mutex_posix.h | 61 | ||||
-rw-r--r-- | drivers/unix/os_unix.cpp | 3 | ||||
-rw-r--r-- | drivers/wasapi/audio_driver_wasapi.cpp | 13 | ||||
-rw-r--r-- | drivers/wasapi/audio_driver_wasapi.h | 2 | ||||
-rw-r--r-- | drivers/windows/mutex_windows.cpp | 101 | ||||
-rw-r--r-- | drivers/windows/mutex_windows.h | 63 | ||||
-rw-r--r-- | drivers/xaudio2/audio_driver_xaudio2.cpp | 12 | ||||
-rw-r--r-- | drivers/xaudio2/audio_driver_xaudio2.h | 2 |
17 files changed, 25 insertions, 373 deletions
diff --git a/drivers/alsa/audio_driver_alsa.cpp b/drivers/alsa/audio_driver_alsa.cpp index fe6cd091b7..400ce31bf7 100644 --- a/drivers/alsa/audio_driver_alsa.cpp +++ b/drivers/alsa/audio_driver_alsa.cpp @@ -154,7 +154,6 @@ Error AudioDriverALSA::init() { Error err = init_device(); if (err == OK) { - mutex = Mutex::create(); thread = Thread::create(AudioDriverALSA::thread_func, this); } @@ -299,16 +298,16 @@ void AudioDriverALSA::set_device(String device) { void AudioDriverALSA::lock() { - if (!thread || !mutex) + if (!thread) return; - mutex->lock(); + mutex.lock(); } void AudioDriverALSA::unlock() { - if (!thread || !mutex) + if (!thread) return; - mutex->unlock(); + mutex.unlock(); } void AudioDriverALSA::finish_device() { @@ -327,11 +326,6 @@ void AudioDriverALSA::finish() { memdelete(thread); thread = NULL; - - if (mutex) { - memdelete(mutex); - mutex = NULL; - } } finish_device(); @@ -339,7 +333,6 @@ void AudioDriverALSA::finish() { AudioDriverALSA::AudioDriverALSA() : thread(NULL), - mutex(NULL), pcm_handle(NULL), device_name("Default"), new_device("Default") { diff --git a/drivers/alsa/audio_driver_alsa.h b/drivers/alsa/audio_driver_alsa.h index fb793df6cd..a8caf0fbae 100644 --- a/drivers/alsa/audio_driver_alsa.h +++ b/drivers/alsa/audio_driver_alsa.h @@ -40,7 +40,7 @@ class AudioDriverALSA : public AudioDriver { Thread *thread; - Mutex *mutex; + Mutex mutex; snd_pcm_t *pcm_handle; 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<snd_rawmidi_t *> connected_inputs; diff --git a/drivers/coreaudio/audio_driver_coreaudio.cpp b/drivers/coreaudio/audio_driver_coreaudio.cpp index d8229f7bf2..1e95bcf5d9 100644 --- a/drivers/coreaudio/audio_driver_coreaudio.cpp +++ b/drivers/coreaudio/audio_driver_coreaudio.cpp @@ -69,8 +69,6 @@ OSStatus AudioDriverCoreAudio::output_device_address_cb(AudioObjectID inObjectID #endif Error AudioDriverCoreAudio::init() { - mutex = Mutex::create(); - AudioComponentDescription desc; zeromem(&desc, sizeof(desc)); desc.componentType = kAudioUnitType_Output; @@ -280,19 +278,15 @@ AudioDriver::SpeakerMode AudioDriverCoreAudio::get_speaker_mode() const { }; void AudioDriverCoreAudio::lock() { - if (mutex) - mutex->lock(); + mutex.lock(); }; void AudioDriverCoreAudio::unlock() { - if (mutex) - mutex->unlock(); + mutex.unlock(); }; bool AudioDriverCoreAudio::try_lock() { - if (mutex) - return mutex->try_lock() == OK; - return true; + return mutex.try_lock() == OK; } void AudioDriverCoreAudio::finish() { @@ -344,11 +338,6 @@ void AudioDriverCoreAudio::finish() { audio_unit = NULL; unlock(); } - - if (mutex) { - memdelete(mutex); - mutex = NULL; - } } Error AudioDriverCoreAudio::capture_init() { @@ -691,7 +680,6 @@ AudioDriverCoreAudio::AudioDriverCoreAudio() : audio_unit(NULL), input_unit(NULL), active(false), - mutex(NULL), device_name("Default"), capture_device_name("Default"), mix_rate(0), diff --git a/drivers/coreaudio/audio_driver_coreaudio.h b/drivers/coreaudio/audio_driver_coreaudio.h index 05aa759078..fb9473e230 100644 --- a/drivers/coreaudio/audio_driver_coreaudio.h +++ b/drivers/coreaudio/audio_driver_coreaudio.h @@ -46,7 +46,7 @@ class AudioDriverCoreAudio : public AudioDriver { AudioComponentInstance input_unit; bool active; - Mutex *mutex; + Mutex mutex; String device_name; String capture_device_name; diff --git a/drivers/pulseaudio/audio_driver_pulseaudio.cpp b/drivers/pulseaudio/audio_driver_pulseaudio.cpp index df9303fbec..ee9278fb8f 100644 --- a/drivers/pulseaudio/audio_driver_pulseaudio.cpp +++ b/drivers/pulseaudio/audio_driver_pulseaudio.cpp @@ -291,7 +291,6 @@ Error AudioDriverPulseAudio::init() { Error err = init_device(); if (err == OK) { - mutex = Mutex::create(); thread = Thread::create(AudioDriverPulseAudio::thread_func, this); } @@ -597,16 +596,16 @@ void AudioDriverPulseAudio::set_device(String device) { void AudioDriverPulseAudio::lock() { - if (!thread || !mutex) + if (!thread) return; - mutex->lock(); + mutex.lock(); } void AudioDriverPulseAudio::unlock() { - if (!thread || !mutex) + if (!thread) return; - mutex->unlock(); + mutex.unlock(); } void AudioDriverPulseAudio::finish_device() { @@ -640,10 +639,6 @@ void AudioDriverPulseAudio::finish() { } memdelete(thread); - if (mutex) { - memdelete(mutex); - mutex = NULL; - } thread = NULL; } @@ -800,7 +795,6 @@ String AudioDriverPulseAudio::capture_get_device() { AudioDriverPulseAudio::AudioDriverPulseAudio() : thread(NULL), - mutex(NULL), pa_ml(NULL), pa_ctx(NULL), pa_str(NULL), diff --git a/drivers/pulseaudio/audio_driver_pulseaudio.h b/drivers/pulseaudio/audio_driver_pulseaudio.h index 15948fa763..1ece332a8a 100644 --- a/drivers/pulseaudio/audio_driver_pulseaudio.h +++ b/drivers/pulseaudio/audio_driver_pulseaudio.h @@ -42,7 +42,7 @@ class AudioDriverPulseAudio : public AudioDriver { Thread *thread; - Mutex *mutex; + Mutex mutex; pa_mainloop *pa_ml; pa_context *pa_ctx; diff --git a/drivers/unix/mutex_posix.cpp b/drivers/unix/mutex_posix.cpp deleted file mode 100644 index a9fc12fb06..0000000000 --- a/drivers/unix/mutex_posix.cpp +++ /dev/null @@ -1,73 +0,0 @@ -/*************************************************************************/ -/* mutex_posix.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#include "mutex_posix.h" - -#include "core/os/memory.h" - -#if defined(UNIX_ENABLED) || defined(PTHREAD_ENABLED) - -void MutexPosix::lock() { - - pthread_mutex_lock(&mutex); -} -void MutexPosix::unlock() { - - pthread_mutex_unlock(&mutex); -} -Error MutexPosix::try_lock() { - - return (pthread_mutex_trylock(&mutex) == 0) ? OK : ERR_BUSY; -} - -Mutex *MutexPosix::create_func_posix(bool p_recursive) { - - return memnew(MutexPosix(p_recursive)); -} - -void MutexPosix::make_default() { - - create_func = create_func_posix; -} - -MutexPosix::MutexPosix(bool p_recursive) { - - pthread_mutexattr_init(&attr); - if (p_recursive) - pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); - pthread_mutex_init(&mutex, &attr); -} - -MutexPosix::~MutexPosix() { - - pthread_mutex_destroy(&mutex); -} - -#endif diff --git a/drivers/unix/mutex_posix.h b/drivers/unix/mutex_posix.h deleted file mode 100644 index bd67106836..0000000000 --- a/drivers/unix/mutex_posix.h +++ /dev/null @@ -1,61 +0,0 @@ -/*************************************************************************/ -/* mutex_posix.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#ifndef MUTEX_POSIX_H -#define MUTEX_POSIX_H - -#if defined(UNIX_ENABLED) || defined(PTHREAD_ENABLED) - -#include "core/os/mutex.h" - -#include <pthread.h> - -class MutexPosix : public Mutex { - - pthread_mutexattr_t attr; - pthread_mutex_t mutex; - - static Mutex *create_func_posix(bool p_recursive); - -public: - virtual void lock(); - virtual void unlock(); - virtual Error try_lock(); - - static void make_default(); - - MutexPosix(bool p_recursive); - - ~MutexPosix(); -}; - -#endif - -#endif diff --git a/drivers/unix/os_unix.cpp b/drivers/unix/os_unix.cpp index 2d8d37b2f1..1d94b9618d 100644 --- a/drivers/unix/os_unix.cpp +++ b/drivers/unix/os_unix.cpp @@ -36,7 +36,6 @@ #include "core/project_settings.h" #include "drivers/unix/dir_access_unix.h" #include "drivers/unix/file_access_unix.h" -#include "drivers/unix/mutex_posix.h" #include "drivers/unix/net_socket_posix.h" #include "drivers/unix/rw_lock_posix.h" #include "drivers/unix/semaphore_posix.h" @@ -123,14 +122,12 @@ void OS_Unix::initialize_core() { #ifdef NO_THREADS ThreadDummy::make_default(); SemaphoreDummy::make_default(); - MutexDummy::make_default(); RWLockDummy::make_default(); #else ThreadPosix::make_default(); #if !defined(OSX_ENABLED) && !defined(IPHONE_ENABLED) SemaphorePosix::make_default(); #endif - MutexPosix::make_default(); RWLockPosix::make_default(); #endif FileAccess::make_default<FileAccessUnix>(FileAccess::ACCESS_RESOURCES); diff --git a/drivers/wasapi/audio_driver_wasapi.cpp b/drivers/wasapi/audio_driver_wasapi.cpp index 8aa6fb96c9..fa78771993 100644 --- a/drivers/wasapi/audio_driver_wasapi.cpp +++ b/drivers/wasapi/audio_driver_wasapi.cpp @@ -406,7 +406,6 @@ Error AudioDriverWASAPI::init() { exit_thread = false; thread_exited = false; - mutex = Mutex::create(true); thread = Thread::create(thread_func, this); return OK; @@ -782,14 +781,12 @@ void AudioDriverWASAPI::start() { void AudioDriverWASAPI::lock() { - if (mutex) - mutex->lock(); + mutex.lock(); } void AudioDriverWASAPI::unlock() { - if (mutex) - mutex->unlock(); + mutex.unlock(); } void AudioDriverWASAPI::finish() { @@ -804,11 +801,6 @@ void AudioDriverWASAPI::finish() { finish_capture_device(); finish_render_device(); - - if (mutex) { - memdelete(mutex); - mutex = NULL; - } } Error AudioDriverWASAPI::capture_start() { @@ -863,7 +855,6 @@ String AudioDriverWASAPI::capture_get_device() { AudioDriverWASAPI::AudioDriverWASAPI() { - mutex = NULL; thread = NULL; samples_in.clear(); diff --git a/drivers/wasapi/audio_driver_wasapi.h b/drivers/wasapi/audio_driver_wasapi.h index ebceedd431..3ea61c6010 100644 --- a/drivers/wasapi/audio_driver_wasapi.h +++ b/drivers/wasapi/audio_driver_wasapi.h @@ -75,7 +75,7 @@ class AudioDriverWASAPI : public AudioDriver { AudioDeviceWASAPI audio_input; AudioDeviceWASAPI audio_output; - Mutex *mutex; + Mutex mutex; Thread *thread; Vector<int32_t> samples_in; diff --git a/drivers/windows/mutex_windows.cpp b/drivers/windows/mutex_windows.cpp deleted file mode 100644 index 1f14adb48f..0000000000 --- a/drivers/windows/mutex_windows.cpp +++ /dev/null @@ -1,101 +0,0 @@ -/*************************************************************************/ -/* mutex_windows.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#include "mutex_windows.h" - -#include "core/os/memory.h" - -#ifdef WINDOWS_ENABLED - -void MutexWindows::lock() { - -#ifdef WINDOWS_USE_MUTEX - WaitForSingleObject(mutex, INFINITE); -#else - EnterCriticalSection(&mutex); -#endif -} - -void MutexWindows::unlock() { - -#ifdef WINDOWS_USE_MUTEX - ReleaseMutex(mutex); -#else - LeaveCriticalSection(&mutex); -#endif -} - -Error MutexWindows::try_lock() { - -#ifdef WINDOWS_USE_MUTEX - return (WaitForSingleObject(mutex, 0) == WAIT_TIMEOUT) ? ERR_BUSY : OK; -#else - - if (TryEnterCriticalSection(&mutex)) - return OK; - else - return ERR_BUSY; -#endif -} - -Mutex *MutexWindows::create_func_windows(bool p_recursive) { - - return memnew(MutexWindows); -} - -void MutexWindows::make_default() { - - create_func = create_func_windows; -} - -MutexWindows::MutexWindows() { - -#ifdef WINDOWS_USE_MUTEX - mutex = CreateMutex(NULL, FALSE, NULL); -#else -#ifdef UWP_ENABLED - InitializeCriticalSectionEx(&mutex, 0, 0); -#else - InitializeCriticalSection(&mutex); -#endif -#endif -} - -MutexWindows::~MutexWindows() { - -#ifdef WINDOWS_USE_MUTEX - CloseHandle(mutex); -#else - - DeleteCriticalSection(&mutex); -#endif -} - -#endif diff --git a/drivers/windows/mutex_windows.h b/drivers/windows/mutex_windows.h deleted file mode 100644 index 28b97540b7..0000000000 --- a/drivers/windows/mutex_windows.h +++ /dev/null @@ -1,63 +0,0 @@ -/*************************************************************************/ -/* mutex_windows.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#ifndef MUTEX_WINDOWS_H -#define MUTEX_WINDOWS_H - -#ifdef WINDOWS_ENABLED - -#include "core/os/mutex.h" - -#include <windows.h> - -class MutexWindows : public Mutex { - -#ifdef WINDOWS_USE_MUTEX - HANDLE mutex; -#else - CRITICAL_SECTION mutex; -#endif - - static Mutex *create_func_windows(bool p_recursive); - -public: - virtual void lock(); - virtual void unlock(); - virtual Error try_lock(); - - static void make_default(); - - MutexWindows(); - ~MutexWindows(); -}; - -#endif - -#endif diff --git a/drivers/xaudio2/audio_driver_xaudio2.cpp b/drivers/xaudio2/audio_driver_xaudio2.cpp index 85e505009b..9c7cb4f0f3 100644 --- a/drivers/xaudio2/audio_driver_xaudio2.cpp +++ b/drivers/xaudio2/audio_driver_xaudio2.cpp @@ -79,7 +79,6 @@ Error AudioDriverXAudio2::init() { hr = xaudio->CreateSourceVoice(&source_voice, &wave_format, 0, XAUDIO2_MAX_FREQ_RATIO, &voice_callback); ERR_FAIL_COND_V_MSG(hr != S_OK, ERR_UNAVAILABLE, "Error creating XAudio2 source voice. Error code: " + itos(hr) + "."); - mutex = Mutex::create(); thread = Thread::create(AudioDriverXAudio2::thread_func, this); return OK; @@ -158,15 +157,15 @@ float AudioDriverXAudio2::get_latency() { void AudioDriverXAudio2::lock() { - if (!thread || !mutex) + if (!thread) return; - mutex->lock(); + mutex.lock(); } void AudioDriverXAudio2::unlock() { - if (!thread || !mutex) + if (!thread) return; - mutex->unlock(); + mutex.unlock(); } void AudioDriverXAudio2::finish() { @@ -194,14 +193,11 @@ void AudioDriverXAudio2::finish() { mastering_voice->DestroyVoice(); memdelete(thread); - if (mutex) - memdelete(mutex); thread = NULL; } AudioDriverXAudio2::AudioDriverXAudio2() : thread(NULL), - mutex(NULL), current_buffer(0) { wave_format = { 0 }; for (int i = 0; i < AUDIO_BUFFERS; i++) { diff --git a/drivers/xaudio2/audio_driver_xaudio2.h b/drivers/xaudio2/audio_driver_xaudio2.h index 98950851d0..108891a288 100644 --- a/drivers/xaudio2/audio_driver_xaudio2.h +++ b/drivers/xaudio2/audio_driver_xaudio2.h @@ -65,7 +65,7 @@ class AudioDriverXAudio2 : public AudioDriver { }; Thread *thread; - Mutex *mutex; + Mutex mutex; int32_t *samples_in; int16_t *samples_out[AUDIO_BUFFERS]; |