player/Thread: even more fine-grained unlocking

Use one single std::lock_guard in Run(), and replace the manual
Lock()/Unlock() calls.
This commit is contained in:
Max Kellermann 2017-12-22 01:16:38 +01:00
parent 15a54230f1
commit 27b0a581a6

View File

@ -332,6 +332,11 @@ private:
*/ */
bool SendSilence() noexcept; bool SendSilence() noexcept;
unsigned UnlockCheckOutputs() noexcept {
const ScopeUnlock unlock(pc.mutex);
return pc.outputs.Check();
}
/** /**
* Player lock must be held before calling. * Player lock must be held before calling.
*/ */
@ -970,7 +975,7 @@ Player::Run() noexcept
{ {
pipe = new MusicPipe(); pipe = new MusicPipe();
pc.Lock(); const std::lock_guard<Mutex> lock(pc.mutex);
StartDecoder(*pipe); StartDecoder(*pipe);
ActivateDecoder(); ActivateDecoder();
@ -984,7 +989,7 @@ Player::Run() noexcept
if (pc.command == PlayerCommand::STOP || if (pc.command == PlayerCommand::STOP ||
pc.command == PlayerCommand::EXIT || pc.command == PlayerCommand::EXIT ||
pc.command == PlayerCommand::CLOSE_AUDIO) { pc.command == PlayerCommand::CLOSE_AUDIO) {
pc.Unlock(); const ScopeUnlock unlock(pc.mutex);
pc.outputs.Cancel(); pc.outputs.Cancel();
break; break;
} }
@ -1018,10 +1023,8 @@ Player::Run() noexcept
if (decoder_starting) { if (decoder_starting) {
/* wait until the decoder is initialized completely */ /* wait until the decoder is initialized completely */
if (!CheckDecoderStartup()) { if (!CheckDecoderStartup())
pc.Unlock();
break; break;
}
continue; continue;
} }
@ -1062,11 +1065,7 @@ Player::Run() noexcept
xfade_state = CrossFadeState::DISABLED; xfade_state = CrossFadeState::DISABLED;
} }
pc.Unlock();
if (paused) { if (paused) {
pc.Lock();
if (pc.command == PlayerCommand::NONE) if (pc.command == PlayerCommand::NONE)
pc.Wait(); pc.Wait();
continue; continue;
@ -1074,14 +1073,13 @@ Player::Run() noexcept
/* at least one music chunk is ready - send it /* at least one music chunk is ready - send it
to the audio output */ to the audio output */
const ScopeUnlock unlock(pc.mutex);
PlayNextChunk(); PlayNextChunk();
} else if (pc.outputs.Check() > 0) { } else if (UnlockCheckOutputs() > 0) {
/* not enough data from decoder, but the /* not enough data from decoder, but the
output thread is still busy, so it's output thread is still busy, so it's
okay */ okay */
pc.Lock();
/* wake up the decoder (just in case it's /* wake up the decoder (just in case it's
waiting for space in the MusicBuffer) and waiting for space in the MusicBuffer) and
wait for it */ wait for it */
@ -1091,14 +1089,16 @@ Player::Run() noexcept
} else if (IsDecoderAtNextSong()) { } else if (IsDecoderAtNextSong()) {
/* at the beginning of a new song */ /* at the beginning of a new song */
const ScopeUnlock unlock(pc.mutex);
SongBorder(); SongBorder();
} else if (dc.LockIsIdle()) { } else if (dc.IsIdle()) {
/* check the size of the pipe again, because /* check the size of the pipe again, because
the decoder thread may have added something the decoder thread may have added something
since we last checked */ since we last checked */
if (pipe->IsEmpty()) { if (pipe->IsEmpty()) {
/* wait for the hardware to finish /* wait for the hardware to finish
playback */ playback */
const ScopeUnlock unlock(pc.mutex);
pc.outputs.Drain(); pc.outputs.Drain();
break; break;
} }
@ -1106,6 +1106,7 @@ Player::Run() noexcept
/* the decoder is too busy and hasn't provided /* the decoder is too busy and hasn't provided
new PCM data in time: send silence (if the new PCM data in time: send silence (if the
output pipe is empty) */ output pipe is empty) */
const ScopeUnlock unlock(pc.mutex);
if (throttle_silence_log.CheckUpdate(std::chrono::seconds(5))) if (throttle_silence_log.CheckUpdate(std::chrono::seconds(5)))
FormatWarning(player_domain, "Decoder is too slow; playing silence to avoid xrun"); FormatWarning(player_domain, "Decoder is too slow; playing silence to avoid xrun");
@ -1113,12 +1114,8 @@ Player::Run() noexcept
if (!SendSilence()) if (!SendSilence())
break; break;
} }
pc.Lock();
} }
const std::lock_guard<Mutex> lock(pc.mutex);
StopDecoder(); StopDecoder();
ClearAndDeletePipe(); ClearAndDeletePipe();