From 7b43cb8266056f1c28c1556c123fed238ec3210f Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Tue, 13 Oct 2020 05:54:05 -0700 Subject: [PATCH] Declare variables closer to where they're used --- al/source.cpp | 93 +++++++++++++++++++++++++++-------------------------------- 1 file changed, 43 insertions(+), 50 deletions(-) diff --git a/al/source.cpp b/al/source.cpp index c16b77e6..b519025b 100644 --- a/al/source.cpp +++ b/al/source.cpp @@ -1252,13 +1252,9 @@ bool SetSourcefv(ALsource *Source, ALCcontext *Context, SourceProp prop, const a bool SetSourceiv(ALsource *Source, ALCcontext *Context, SourceProp prop, const al::span values) { ALCdevice *device{Context->mDevice.get()}; - ALbuffer *buffer{nullptr}; - ALfilter *filter{nullptr}; ALeffectslot *slot{nullptr}; ALbufferlistitem *oldlist{nullptr}; std::unique_lock slotlock; - std::unique_lock filtlock; - std::unique_lock buflock; float fvals[6]; switch(prop) @@ -1303,28 +1299,27 @@ bool SetSourceiv(ALsource *Source, ALCcontext *Context, SourceProp prop, const a case AL_BUFFER: CHECKSIZE(values, 1); - buflock = std::unique_lock{device->BufferLock}; - if(values[0] && (buffer=LookupBuffer(device, static_cast(values[0]))) == nullptr) - SETERR_RETURN(Context, AL_INVALID_VALUE, false, "Invalid buffer ID %u", - static_cast(values[0])); - - if(buffer && buffer->MappedAccess && !(buffer->MappedAccess&AL_MAP_PERSISTENT_BIT_SOFT)) - SETERR_RETURN(Context, AL_INVALID_OPERATION, false, - "Setting non-persistently mapped buffer %u", buffer->id); - else if(buffer && buffer->mBuffer.mCallback && ReadRef(buffer->ref) != 0) - SETERR_RETURN(Context, AL_INVALID_OPERATION, false, - "Setting already-set callback buffer %u", buffer->id); - else { const ALenum state{GetSourceState(Source, GetSourceVoice(Source, Context))}; if(state == AL_PLAYING || state == AL_PAUSED) SETERR_RETURN(Context, AL_INVALID_OPERATION, false, "Setting buffer on playing or paused source %u", Source->id); } - oldlist = Source->queue; - if(buffer != nullptr) - { + if(values[0]) + { + std::lock_guard _{device->BufferLock}; + ALbuffer *buffer{LookupBuffer(device, static_cast(values[0]))}; + if(!buffer) + SETERR_RETURN(Context, AL_INVALID_VALUE, false, "Invalid buffer ID %u", + static_cast(values[0])); + if(buffer->MappedAccess && !(buffer->MappedAccess&AL_MAP_PERSISTENT_BIT_SOFT)) + SETERR_RETURN(Context, AL_INVALID_OPERATION, false, + "Setting non-persistently mapped buffer %u", buffer->id); + if(buffer->mBuffer.mCallback && ReadRef(buffer->ref) != 0) + SETERR_RETURN(Context, AL_INVALID_OPERATION, false, + "Setting already-set callback buffer %u", buffer->id); + /* Add the selected buffer to a one-item queue */ auto newlist = new ALbufferlistitem{}; newlist->mSampleLen = buffer->mBuffer.mSampleLen; @@ -1341,7 +1336,6 @@ bool SetSourceiv(ALsource *Source, ALCcontext *Context, SourceProp prop, const a Source->SourceType = AL_UNDETERMINED; Source->queue = nullptr; } - buflock.unlock(); /* Delete all elements in the previous queue */ while(oldlist != nullptr) @@ -1349,7 +1343,7 @@ bool SetSourceiv(ALsource *Source, ALCcontext *Context, SourceProp prop, const a std::unique_ptr temp{oldlist}; oldlist = temp->mNext.load(std::memory_order_relaxed); - if((buffer=temp->mBuffer) != nullptr) + if(ALbuffer *buffer{temp->mBuffer}) DecrementRef(buffer->ref); } return true; @@ -1377,28 +1371,27 @@ bool SetSourceiv(ALsource *Source, ALCcontext *Context, SourceProp prop, const a case AL_DIRECT_FILTER: CHECKSIZE(values, 1); - filtlock = std::unique_lock{device->FilterLock}; - if(values[0] && (filter=LookupFilter(device, static_cast(values[0]))) == nullptr) - SETERR_RETURN(Context, AL_INVALID_VALUE, false, "Invalid filter ID %u", - static_cast(values[0])); - - if(!filter) - { - Source->Direct.Gain = 1.0f; - Source->Direct.GainHF = 1.0f; - Source->Direct.HFReference = LOWPASSFREQREF; - Source->Direct.GainLF = 1.0f; - Source->Direct.LFReference = HIGHPASSFREQREF; - } - else + if(values[0]) { + std::lock_guard _{device->FilterLock}; + ALfilter *filter{LookupFilter(device, static_cast(values[0]))}; + if(!filter) + SETERR_RETURN(Context, AL_INVALID_VALUE, false, "Invalid filter ID %u", + static_cast(values[0])); Source->Direct.Gain = filter->Gain; Source->Direct.GainHF = filter->GainHF; Source->Direct.HFReference = filter->HFReference; Source->Direct.GainLF = filter->GainLF; Source->Direct.LFReference = filter->LFReference; } - filtlock.unlock(); + else + { + Source->Direct.Gain = 1.0f; + Source->Direct.GainHF = 1.0f; + Source->Direct.HFReference = LOWPASSFREQREF; + Source->Direct.GainLF = 1.0f; + Source->Direct.LFReference = HIGHPASSFREQREF; + } return UpdateSourceProps(Source, Context); case AL_DIRECT_FILTER_GAINHF_AUTO: @@ -1465,11 +1458,21 @@ bool SetSourceiv(ALsource *Source, ALCcontext *Context, SourceProp prop, const a if(static_cast(values[1]) >= device->NumAuxSends) SETERR_RETURN(Context, AL_INVALID_VALUE, false, "Invalid send %u", values[1]); - filtlock = std::unique_lock{device->FilterLock}; - if(values[2] && (filter=LookupFilter(device, static_cast(values[2]))) == nullptr) - SETERR_RETURN(Context, AL_INVALID_VALUE, false, "Invalid filter ID %u", values[2]); + if(values[2]) + { + std::lock_guard _{device->FilterLock}; + ALfilter *filter{LookupFilter(device, static_cast(values[2]))}; + if(!filter) + SETERR_RETURN(Context, AL_INVALID_VALUE, false, "Invalid filter ID %u", values[2]); - if(!filter) + auto &send = Source->Send[static_cast(values[1])]; + send.Gain = filter->Gain; + send.GainHF = filter->GainHF; + send.HFReference = filter->HFReference; + send.GainLF = filter->GainLF; + send.LFReference = filter->LFReference; + } + else { /* Disable filter */ auto &send = Source->Send[static_cast(values[1])]; @@ -1479,16 +1482,6 @@ bool SetSourceiv(ALsource *Source, ALCcontext *Context, SourceProp prop, const a send.GainLF = 1.0f; send.LFReference = HIGHPASSFREQREF; } - else - { - auto &send = Source->Send[static_cast(values[1])]; - send.Gain = filter->Gain; - send.GainHF = filter->GainHF; - send.HFReference = filter->HFReference; - send.GainLF = filter->GainLF; - send.LFReference = filter->LFReference; - } - filtlock.unlock(); if(slot != Source->Send[static_cast(values[1])].Slot && IsPlayingOrPaused(Source)) { -- 2.11.4.GIT