From 07b5d7b3d06ae009007e49246fa899c3f6c2b196 Mon Sep 17 00:00:00 2001 From: Paul Fd Date: Mon, 13 Apr 2020 00:28:11 +0200 Subject: [PATCH] Use unique_locks --- src/sfizz/Defer.h | 24 ------------------------ src/sfizz/EQPool.cpp | 6 +++--- src/sfizz/FilePool.cpp | 5 ++--- src/sfizz/FilterPool.cpp | 5 ++--- src/sfizz/Synth.cpp | 21 ++++++++++----------- 5 files changed, 17 insertions(+), 44 deletions(-) delete mode 100644 src/sfizz/Defer.h diff --git a/src/sfizz/Defer.h b/src/sfizz/Defer.h deleted file mode 100644 index e88bd21c..00000000 --- a/src/sfizz/Defer.h +++ /dev/null @@ -1,24 +0,0 @@ -#pragma once - -// From https://stackoverflow.com/questions/48117908/is-the-a-practical-way-to-emulate-go-language-defer-in-c-or-c-destructors -#include -#include - -template -struct deferred -{ - F f; - deferred(F f) : f(f) {} - ~deferred() { f(); } -}; - - -template -deferred deferred_func(F f) { - return deferred(f); -} - -#define CAT_(x, y) x##y -#define CAT(x, y) CAT_(x, y) -#define ANONYMOUS_VAR(x) CAT(x, __LINE__) -#define DEFER(code) auto ANONYMOUS_VAR(defer_variable) = deferred_func([&] { code ; }) diff --git a/src/sfizz/EQPool.cpp b/src/sfizz/EQPool.cpp index ccee55bc..94aac145 100644 --- a/src/sfizz/EQPool.cpp +++ b/src/sfizz/EQPool.cpp @@ -1,5 +1,4 @@ #include "EQPool.h" -#include "Defer.h" #include #include "absl/algorithm/container.h" #include "SIMDHelpers.h" @@ -108,9 +107,10 @@ sfz::EQPool::EQPool(const MidiState& state, int numEQs) sfz::EQHolderPtr sfz::EQPool::getEQ(const EQDescription& description, unsigned numChannels, float velocity) { - if (!eqGuard.try_lock()) + const std::unique_lock lock { eqGuard, std::try_to_lock }; + if (!lock.owns_lock()) return {}; - DEFER(eqGuard.unlock()); + auto eq = absl::c_find_if(eqs, [](const EQHolderPtr& holder) { return holder.use_count() == 1; diff --git a/src/sfizz/FilePool.cpp b/src/sfizz/FilePool.cpp index 55f57f24..a356dca2 100644 --- a/src/sfizz/FilePool.cpp +++ b/src/sfizz/FilePool.cpp @@ -29,7 +29,6 @@ #include "Config.h" #include "Debug.h" #include "Oversampler.h" -#include "Defer.h" #include "absl/types/span.h" #include "absl/strings/match.h" #include "absl/memory/memory.h" @@ -383,9 +382,9 @@ void sfz::FilePool::clear() void sfz::FilePool::cleanupPromises() noexcept { - if (!promiseGuard.try_lock()) + const std::unique_lock lock { promiseGuard, std::try_to_lock }; + if (!lock.owns_lock()) return; - DEFER(promiseGuard.unlock()); // The garbage collection cleared the data from these so we can move them // back to the empty queue diff --git a/src/sfizz/FilterPool.cpp b/src/sfizz/FilterPool.cpp index ce18be28..9e4f9091 100644 --- a/src/sfizz/FilterPool.cpp +++ b/src/sfizz/FilterPool.cpp @@ -1,7 +1,6 @@ #include "FilterPool.h" #include "SIMDHelpers.h" #include "absl/algorithm/container.h" -#include "Defer.h" #include #include @@ -109,9 +108,9 @@ sfz::FilterPool::FilterPool(const MidiState& state, int numFilters) sfz::FilterHolderPtr sfz::FilterPool::getFilter(const FilterDescription& description, unsigned numChannels, int noteNumber, float velocity) { - if (!filterGuard.try_lock()) + const std::unique_lock lock { filterGuard, std::try_to_lock }; + if (!lock.owns_lock()) return {}; - DEFER(filterGuard.unlock()); auto filter = absl::c_find_if(filters, [](const FilterHolderPtr& holder) { return holder.use_count() == 1; diff --git a/src/sfizz/Synth.cpp b/src/sfizz/Synth.cpp index a83743b7..1141624c 100644 --- a/src/sfizz/Synth.cpp +++ b/src/sfizz/Synth.cpp @@ -5,7 +5,6 @@ // If not, contact the sfizz maintainers at https://github.com/sfztools/sfizz #include "Synth.h" -#include "Defer.h" #include "Config.h" #include "Debug.h" #include "Macros.h" @@ -539,9 +538,9 @@ void sfz::Synth::renderBlock(AudioSpan buffer) noexcept if (freeWheeling) resources.filePool.waitForBackgroundLoading(); - if (!callbackGuard.try_lock()) + const std::unique_lock lock { callbackGuard, std::try_to_lock }; + if (!lock.owns_lock()) return; - DEFER(callbackGuard.unlock()); size_t numFrames = buffer.getNumFrames(); @@ -635,9 +634,9 @@ void sfz::Synth::noteOn(int delay, int noteNumber, uint8_t velocity) noexcept ScopedTiming logger { dispatchDuration, ScopedTiming::Operation::addToDuration }; resources.midiState.noteOnEvent(delay, noteNumber, normalizedVelocity); - if (!callbackGuard.try_lock()) + const std::unique_lock lock { callbackGuard, std::try_to_lock }; + if (!lock.owns_lock()) return; - DEFER(callbackGuard.unlock()); noteOnDispatch(delay, noteNumber, normalizedVelocity); } @@ -651,9 +650,9 @@ void sfz::Synth::noteOff(int delay, int noteNumber, uint8_t velocity) noexcept ScopedTiming logger { dispatchDuration, ScopedTiming::Operation::addToDuration }; resources.midiState.noteOffEvent(delay, noteNumber, normalizedVelocity); - if (!callbackGuard.try_lock()) + const std::unique_lock lock { callbackGuard, std::try_to_lock }; + if (!lock.owns_lock()) return; - DEFER(callbackGuard.unlock()); // FIXME: Some keyboards (e.g. Casio PX5S) can send a real note-off velocity. In this case, do we have a // way in sfz to specify that a release trigger should NOT use the note-on velocity? @@ -747,9 +746,9 @@ void sfz::Synth::cc(int delay, int ccNumber, uint8_t ccValue) noexcept ScopedTiming logger { dispatchDuration, ScopedTiming::Operation::addToDuration }; resources.midiState.ccEvent(delay, ccNumber, normalizedCC); - if (!callbackGuard.try_lock()) + const std::unique_lock lock { callbackGuard, std::try_to_lock }; + if (!lock.owns_lock()) return; - DEFER(callbackGuard.unlock()); if (ccNumber == config::resetCC) { resetAllControllers(delay); @@ -1008,9 +1007,9 @@ void sfz::Synth::resetAllControllers(int delay) noexcept { resources.midiState.resetAllControllers(delay); - if (!callbackGuard.try_lock()) + const std::unique_lock lock { callbackGuard, std::try_to_lock }; + if (!lock.owns_lock()) return; - DEFER(callbackGuard.unlock()); for (auto& voice : voices) { voice->registerPitchWheel(delay, 0);