Avoid raw lock/unlock calls

This commit is contained in:
Chris Robinson 2019-10-07 23:22:06 -07:00
parent f0fa6c6baf
commit 52a003e9bb
14 changed files with 44 additions and 58 deletions

View File

@ -471,7 +471,7 @@ int AlsaPlayback::mixerProc()
avail -= avail%update_size;
// it is possible that contiguous areas are smaller, thus we use a loop
lock();
std::lock_guard<AlsaPlayback> _{*this};
while(avail > 0)
{
snd_pcm_uframes_t frames{avail};
@ -498,7 +498,6 @@ int AlsaPlayback::mixerProc()
avail -= frames;
}
unlock();
}
return 0;
@ -551,7 +550,7 @@ int AlsaPlayback::mixerNoMMapProc()
continue;
}
lock();
std::lock_guard<AlsaPlayback> _{*this};
al::byte *WritePtr{mBuffer.data()};
avail = snd_pcm_bytes_to_frames(mPcmHandle, static_cast<ssize_t>(mBuffer.size()));
aluMixData(mDevice, WritePtr, static_cast<ALuint>(avail));
@ -586,7 +585,6 @@ int AlsaPlayback::mixerNoMMapProc()
if(ret < 0) break;
}
}
unlock();
}
return 0;
@ -847,7 +845,7 @@ ClockLatency AlsaPlayback::getClockLatency()
{
ClockLatency ret;
lock();
std::lock_guard<AlsaPlayback> _{*this};
ret.ClockTime = GetDeviceClockTime(mDevice);
snd_pcm_sframes_t delay{};
int err{snd_pcm_delay(mPcmHandle, &delay)};
@ -858,7 +856,6 @@ ClockLatency AlsaPlayback::getClockLatency()
}
ret.Latency = std::chrono::seconds{std::max<snd_pcm_sframes_t>(0, delay)};
ret.Latency /= mDevice->Frequency;
unlock();
return ret;
}
@ -1188,7 +1185,7 @@ ClockLatency AlsaCapture::getClockLatency()
{
ClockLatency ret;
lock();
std::lock_guard<AlsaCapture> _{*this};
ret.ClockTime = GetDeviceClockTime(mDevice);
snd_pcm_sframes_t delay{};
int err{snd_pcm_delay(mPcmHandle, &delay)};
@ -1199,7 +1196,6 @@ ClockLatency AlsaCapture::getClockLatency()
}
ret.Latency = std::chrono::seconds{std::max<snd_pcm_sframes_t>(0, delay)};
ret.Latency /= mDevice->Frequency;
unlock();
return ret;
}

View File

@ -81,9 +81,8 @@ CoreAudioPlayback::~CoreAudioPlayback()
OSStatus CoreAudioPlayback::MixerProc(AudioUnitRenderActionFlags*,
const AudioTimeStamp*, UInt32, UInt32, AudioBufferList *ioData)
{
lock();
std::lock_guard<CoreAudioPlayback> _{*this};
aluMixData(mDevice, ioData->mBuffers[0].mData, ioData->mBuffers[0].mDataByteSize/mFrameSize);
unlock();
return noErr;
}

View File

@ -276,17 +276,18 @@ FORCE_ALIGN int DSoundPlayback::mixerProc()
if(SUCCEEDED(err))
{
lock();
std::unique_lock<DSoundPlayback> dlock{*this};
aluMixData(mDevice, WritePtr1, WriteCnt1/FrameSize);
if(WriteCnt2 > 0)
aluMixData(mDevice, WritePtr2, WriteCnt2/FrameSize);
unlock();
dlock.unlock();
mBuffer->Unlock(WritePtr1, WriteCnt1, WritePtr2, WriteCnt2);
}
else
{
ERR("Buffer lock error: %#lx\n", err);
std::lock_guard<DSoundPlayback> _{*this};
aluHandleDisconnect(mDevice, "Failed to lock output buffer: 0x%lx", err);
return 1;
}

View File

@ -296,15 +296,15 @@ int JackPlayback::mixerProc()
SetRTPriority();
althrd_setname(MIXER_THREAD_NAME);
lock();
std::unique_lock<JackPlayback> dlock{*this};
while(!mKillNow.load(std::memory_order_acquire) &&
mDevice->Connected.load(std::memory_order_acquire))
{
if(mRing->writeSpace() < mDevice->UpdateSize)
{
unlock();
dlock.unlock();
mSem.wait();
lock();
dlock.lock();
continue;
}
@ -320,7 +320,6 @@ int JackPlayback::mixerProc()
aluMixData(mDevice, data.second.buf, len2);
mRing->writeAdvance(todo);
}
unlock();
return 0;
}
@ -484,11 +483,10 @@ ClockLatency JackPlayback::getClockLatency()
{
ClockLatency ret;
lock();
std::lock_guard<JackPlayback> _{*this};
ret.ClockTime = GetDeviceClockTime(mDevice);
ret.Latency = std::chrono::seconds{mRing->readSpace()};
ret.Latency /= mDevice->Frequency;
unlock();
return ret;
}

View File

@ -86,9 +86,8 @@ int NullBackend::mixerProc()
}
while(avail-done >= mDevice->UpdateSize)
{
lock();
std::lock_guard<NullBackend> _{*this};
aluMixData(mDevice, nullptr, mDevice->UpdateSize);
unlock();
done += mDevice->UpdateSize;
}

View File

@ -228,7 +228,7 @@ int OpenSLPlayback::mixerProc()
PRINTERR(result, "bufferQueue->GetInterface SL_IID_PLAY");
}
lock();
std::unique_lock<OpenSLPlayback> dlock{*this};
if(SL_RESULT_SUCCESS != result)
aluHandleDisconnect(mDevice, "Failed to get playback buffer: 0x%08x", result);
@ -254,9 +254,9 @@ int OpenSLPlayback::mixerProc()
if(mRing->writeSpace() == 0)
{
unlock();
dlock.unlock();
mSem.wait();
lock();
dlock.lock();
continue;
}
}
@ -292,7 +292,6 @@ int OpenSLPlayback::mixerProc()
data.first.buf += mDevice->UpdateSize*mFrameSize;
}
}
unlock();
return 0;
}
@ -617,11 +616,10 @@ ClockLatency OpenSLPlayback::getClockLatency()
{
ClockLatency ret;
lock();
std::lock_guard<OpenSLPlayback> _{*this};
ret.ClockTime = GetDeviceClockTime(mDevice);
ret.Latency = std::chrono::seconds{mRing->readSpace() * mDevice->UpdateSize};
ret.Latency /= mDevice->Frequency;
unlock();
return ret;
}

View File

@ -279,7 +279,7 @@ int OSSPlayback::mixerProc()
const ALuint frame_size{mDevice->frameSizeFromFmt()};
lock();
std::unique_lock<OSSPlayback> dlock{*this};
while(!mKillNow.load(std::memory_order_acquire) &&
mDevice->Connected.load(std::memory_order_acquire))
{
@ -287,9 +287,9 @@ int OSSPlayback::mixerProc()
pollitem.fd = mFd;
pollitem.events = POLLOUT;
unlock();
dlock.unlock();
int pret{poll(&pollitem, 1, 1000)};
lock();
dlock.lock();
if(pret < 0)
{
if(errno == EINTR || errno == EAGAIN)
@ -324,7 +324,6 @@ int OSSPlayback::mixerProc()
write_ptr += wrote;
}
}
unlock();
return 0;
}

View File

@ -110,9 +110,8 @@ int PortPlayback::writeCallback(const void*, void *outputBuffer,
unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo*,
const PaStreamCallbackFlags)
{
lock();
std::lock_guard<PortPlayback> _{*this};
aluMixData(mDevice, outputBuffer, static_cast<ALuint>(framesPerBuffer));
unlock();
return 0;
}

View File

@ -207,7 +207,7 @@ FORCE_ALIGN static int qsa_proc_playback(void *ptr)
const ALint frame_size = device->frameSizeFromFmt();
self->lock();
std::unique_lock<PlaybackWrapper> dlock{*self};
while(!data->mKillNow.load(std::memory_order_acquire))
{
pollfd pollitem{};
@ -215,9 +215,9 @@ FORCE_ALIGN static int qsa_proc_playback(void *ptr)
pollitem.events = POLLOUT;
/* Select also works like time slice to OS */
self->unlock();
dlock.unlock();
sret = poll(&pollitem, 1, 2000);
self->lock();
dlock.lock();
if(sret == -1)
{
if(errno == EINTR || errno == EAGAIN)
@ -266,7 +266,6 @@ FORCE_ALIGN static int qsa_proc_playback(void *ptr)
}
}
}
self->unlock();
return 0;
}

View File

@ -85,9 +85,10 @@ int SndioPlayback::mixerProc()
ALubyte *WritePtr{mBuffer.data()};
size_t len{mBuffer.size()};
lock();
aluMixData(mDevice, WritePtr, static_cast<ALuint>(len/frameSize));
unlock();
{
std::lock_guard<SndioPlayback> _{*this};
aluMixData(mDevice, WritePtr, static_cast<ALuint>(len/frameSize));
}
while(len > 0 && !mKillNow.load(std::memory_order_acquire))
{
size_t wrote{sio_write(mSndHandle, WritePtr, len)};

View File

@ -91,7 +91,7 @@ int SolarisBackend::mixerProc()
const ALuint frame_size{mDevice->frameSizeFromFmt()};
lock();
std::unique_lock<SolarisBackend> dlock{*this};
while(!mKillNow.load(std::memory_order_acquire) &&
mDevice->Connected.load(std::memory_order_acquire))
{
@ -99,9 +99,9 @@ int SolarisBackend::mixerProc()
pollitem.fd = mFd;
pollitem.events = POLLOUT;
unlock();
dlock.unlock();
int pret{poll(&pollitem, 1, 1000)};
lock();
dlock.lock();
if(pret < 0)
{
if(errno == EINTR || errno == EAGAIN)
@ -137,7 +137,6 @@ int SolarisBackend::mixerProc()
write_ptr += wrote;
}
}
unlock();
return 0;
}

View File

@ -689,10 +689,10 @@ FORCE_ALIGN int WasapiPlayback::mixerProc()
hr = mRender->GetBuffer(len, &buffer);
if(SUCCEEDED(hr))
{
lock();
std::unique_lock<WasapiPlayback> dlock{*this};
aluMixData(mDevice, buffer, len);
mPadding.store(written + len, std::memory_order_relaxed);
unlock();
dlock.unlock();
hr = mRender->ReleaseBuffer(len, 0);
}
if(FAILED(hr))
@ -1136,11 +1136,10 @@ ClockLatency WasapiPlayback::getClockLatency()
{
ClockLatency ret;
lock();
std::lock_guard<WasapiPlayback> _{*this};
ret.ClockTime = GetDeviceClockTime(mDevice);
ret.Latency = std::chrono::seconds{mPadding.load(std::memory_order_relaxed)};
ret.Latency /= mDevice->Frequency;
unlock();
return ret;
}

View File

@ -145,9 +145,10 @@ int WaveBackend::mixerProc()
}
while(avail-done >= mDevice->UpdateSize)
{
lock();
aluMixData(mDevice, mBuffer.data(), mDevice->UpdateSize);
unlock();
{
std::lock_guard<WaveBackend> _{*this};
aluMixData(mDevice, mBuffer.data(), mDevice->UpdateSize);
}
done += mDevice->UpdateSize;
if(!IS_LITTLE_ENDIAN)

View File

@ -183,16 +183,16 @@ FORCE_ALIGN int WinMMPlayback::mixerProc()
SetRTPriority();
althrd_setname(MIXER_THREAD_NAME);
lock();
std::unique_lock<WinMMPlayback> dlock{*this};
while(!mKillNow.load(std::memory_order_acquire) &&
mDevice->Connected.load(std::memory_order_acquire))
{
ALsizei todo = mWritable.load(std::memory_order_acquire);
if(todo < 1)
{
unlock();
dlock.unlock();
mSem.wait();
lock();
dlock.lock();
continue;
}
@ -207,7 +207,6 @@ FORCE_ALIGN int WinMMPlayback::mixerProc()
} while(--todo);
mIdx = static_cast<ALuint>(widx);
}
unlock();
return 0;
}
@ -427,16 +426,16 @@ int WinMMCapture::captureProc()
{
althrd_setname(RECORD_THREAD_NAME);
lock();
std::unique_lock<WinMMCapture> dlock{*this};
while(!mKillNow.load(std::memory_order_acquire) &&
mDevice->Connected.load(std::memory_order_acquire))
{
ALuint todo{mReadable.load(std::memory_order_acquire)};
if(todo < 1)
{
unlock();
dlock.unlock();
mSem.wait();
lock();
dlock.lock();
continue;
}
@ -451,7 +450,6 @@ int WinMMCapture::captureProc()
} while(--todo);
mIdx = static_cast<ALuint>(widx);
}
unlock();
return 0;
}