Use unique_locks

This commit is contained in:
Paul Fd 2020-04-13 00:28:11 +02:00 committed by Jean Pierre Cimalando
parent 3e010a4ba6
commit 07b5d7b3d0
5 changed files with 17 additions and 44 deletions

View file

@ -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<type_traits>
#include<utility>
template<typename F>
struct deferred
{
F f;
deferred(F f) : f(f) {}
~deferred() { f(); }
};
template <typename F>
deferred<F> deferred_func(F f) {
return deferred<F>(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 ; })

View file

@ -1,5 +1,4 @@
#include "EQPool.h"
#include "Defer.h"
#include <thread>
#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<std::mutex> 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;

View file

@ -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<std::mutex> 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

View file

@ -1,7 +1,6 @@
#include "FilterPool.h"
#include "SIMDHelpers.h"
#include "absl/algorithm/container.h"
#include "Defer.h"
#include <thread>
#include <chrono>
@ -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<std::mutex> 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;

View file

@ -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<float> buffer) noexcept
if (freeWheeling)
resources.filePool.waitForBackgroundLoading();
if (!callbackGuard.try_lock())
const std::unique_lock<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> lock { callbackGuard, std::try_to_lock };
if (!lock.owns_lock())
return;
DEFER(callbackGuard.unlock());
for (auto& voice : voices) {
voice->registerPitchWheel(delay, 0);