player/Thread: use class ScopeLock

This commit is contained in:
Max Kellermann 2016-12-14 12:54:37 +01:00
parent 03151310cf
commit 7c6b0d5c31

View File

@ -398,7 +398,9 @@ Player::ActivateDecoder()
queued = false; queued = false;
pc.Lock(); {
const ScopeLock lock(pc.mutex);
pc.ClearTaggedSong(); pc.ClearTaggedSong();
delete song; delete song;
@ -415,8 +417,7 @@ Player::ActivateDecoder()
pc.total_time = song->GetDuration(); pc.total_time = song->GetDuration();
pc.bit_rate = 0; pc.bit_rate = 0;
pc.audio_format.Clear(); pc.audio_format.Clear();
}
pc.Unlock();
/* call syncPlaylistWithQueue() in the main thread */ /* call syncPlaylistWithQueue() in the main thread */
pc.listener.OnPlayerSync(); pc.listener.OnPlayerSync();
@ -471,9 +472,10 @@ Player::OpenOutput()
output_open = true; output_open = true;
paused = false; paused = false;
pc.Lock(); {
const ScopeLock lock(pc.mutex);
pc.state = PlayerState::PLAY; pc.state = PlayerState::PLAY;
pc.Unlock(); }
idle_add(IDLE_PLAYER); idle_add(IDLE_PLAYER);
@ -503,10 +505,12 @@ Player::CheckDecoderStartup()
all chunks yet - wait for that */ all chunks yet - wait for that */
return true; return true;
pc.Lock(); {
pc.total_time = real_song_duration(*dc.song, dc.total_time); const ScopeLock lock(pc.mutex);
pc.total_time = real_song_duration(*dc.song,
dc.total_time);
pc.audio_format = dc.in_audio_format; pc.audio_format = dc.in_audio_format;
pc.Unlock(); }
idle_add(IDLE_PLAYER); idle_add(IDLE_PLAYER);
@ -658,9 +662,11 @@ Player::ProcessCommand()
break; break;
case PlayerCommand::UPDATE_AUDIO: case PlayerCommand::UPDATE_AUDIO:
pc.Unlock(); {
const ScopeUnlock unlock(pc.mutex);
pc.outputs.EnableDisable(); pc.outputs.EnableDisable();
pc.Lock(); }
pc.CommandFinished(); pc.CommandFinished();
break; break;
@ -672,10 +678,11 @@ Player::ProcessCommand()
queued = true; queued = true;
pc.CommandFinished(); pc.CommandFinished();
pc.Unlock(); {
const ScopeUnlock unlock(pc.mutex);
if (dc.LockIsIdle()) if (dc.LockIsIdle())
StartDecoder(*new MusicPipe()); StartDecoder(*new MusicPipe());
pc.Lock(); }
break; break;
@ -704,9 +711,10 @@ Player::ProcessCommand()
break; break;
case PlayerCommand::SEEK: case PlayerCommand::SEEK:
pc.Unlock(); {
const ScopeUnlock unlock(pc.mutex);
SeekDecoder(); SeekDecoder();
pc.Lock(); }
break; break;
case PlayerCommand::CANCEL: case PlayerCommand::CANCEL:
@ -721,9 +729,8 @@ Player::ProcessCommand()
if (IsDecoderAtNextSong()) { if (IsDecoderAtNextSong()) {
/* the decoder is already decoding the song - /* the decoder is already decoding the song -
stop it and reset the position */ stop it and reset the position */
pc.Unlock(); const ScopeUnlock unlock(pc.mutex);
StopDecoder(); StopDecoder();
pc.Lock();
} }
delete pc.next_song; delete pc.next_song;
@ -734,9 +741,8 @@ Player::ProcessCommand()
case PlayerCommand::REFRESH: case PlayerCommand::REFRESH:
if (output_open && !paused) { if (output_open && !paused) {
pc.Unlock(); const ScopeUnlock unlock(pc.mutex);
pc.outputs.Check(); pc.outputs.Check();
pc.Lock();
} }
pc.elapsed_time = !pc.outputs.GetElapsedTime().IsNegative() pc.elapsed_time = !pc.outputs.GetElapsedTime().IsNegative()
@ -792,9 +798,10 @@ play_chunk(PlayerControl &pc,
return; return;
} }
pc.Lock(); {
const ScopeLock lock(pc.mutex);
pc.bit_rate = chunk->bit_rate; pc.bit_rate = chunk->bit_rate;
pc.Unlock(); }
/* send the chunk to the audio outputs */ /* send the chunk to the audio outputs */
@ -868,19 +875,16 @@ Player::PlayNextChunk()
} else { } else {
/* there are not enough decoded chunks yet */ /* there are not enough decoded chunks yet */
pc.Lock(); const ScopeLock lock(pc.mutex);
if (dc.IsIdle()) { if (dc.IsIdle()) {
/* the decoder isn't running, abort /* the decoder isn't running, abort
cross fading */ cross fading */
pc.Unlock();
xfade_state = CrossFadeState::DISABLED; xfade_state = CrossFadeState::DISABLED;
} else { } else {
/* wait for the decoder */ /* wait for the decoder */
dc.Signal(); dc.Signal();
dc.WaitForDecoder(); dc.WaitForDecoder();
pc.Unlock();
return true; return true;
} }
@ -919,10 +923,11 @@ Player::PlayNextChunk()
return false; return false;
} }
const ScopeLock lock(pc.mutex);
/* this formula should prevent that the decoder gets woken up /* this formula should prevent that the decoder gets woken up
with each chunk; it is more efficient to make it decode a with each chunk; it is more efficient to make it decode a
larger block at a time */ larger block at a time */
pc.Lock();
if (!dc.IsIdle() && if (!dc.IsIdle() &&
dc.pipe->GetSize() <= (pc.buffered_before_play + dc.pipe->GetSize() <= (pc.buffered_before_play +
buffer.GetSize() * 3) / 4) { buffer.GetSize() * 3) / 4) {
@ -932,7 +937,6 @@ Player::PlayNextChunk()
} }
} else } else
decoder_woken = false; decoder_woken = false;
pc.Unlock();
return true; return true;
} }