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,25 +398,26 @@ Player::ActivateDecoder()
queued = false; queued = false;
pc.Lock(); {
pc.ClearTaggedSong(); const ScopeLock lock(pc.mutex);
delete song; pc.ClearTaggedSong();
song = pc.next_song;
pc.next_song = nullptr;
elapsed_time = pc.seek_time; delete song;
song = pc.next_song;
pc.next_song = nullptr;
/* set the "starting" flag, which will be cleared by elapsed_time = pc.seek_time;
player_check_decoder_startup() */
decoder_starting = true;
/* update PlayerControl's song information */ /* set the "starting" flag, which will be cleared by
pc.total_time = song->GetDuration(); player_check_decoder_startup() */
pc.bit_rate = 0; decoder_starting = true;
pc.audio_format.Clear();
pc.Unlock(); /* update PlayerControl's song information */
pc.total_time = song->GetDuration();
pc.bit_rate = 0;
pc.audio_format.Clear();
}
/* 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(); {
pc.state = PlayerState::PLAY; const ScopeLock lock(pc.mutex);
pc.Unlock(); pc.state = PlayerState::PLAY;
}
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.audio_format = dc.in_audio_format; pc.total_time = real_song_duration(*dc.song,
pc.Unlock(); dc.total_time);
pc.audio_format = dc.in_audio_format;
}
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(); {
pc.outputs.EnableDisable(); const ScopeUnlock unlock(pc.mutex);
pc.Lock(); pc.outputs.EnableDisable();
}
pc.CommandFinished(); pc.CommandFinished();
break; break;
@ -672,10 +678,11 @@ Player::ProcessCommand()
queued = true; queued = true;
pc.CommandFinished(); pc.CommandFinished();
pc.Unlock(); {
if (dc.LockIsIdle()) const ScopeUnlock unlock(pc.mutex);
StartDecoder(*new MusicPipe()); if (dc.LockIsIdle())
pc.Lock(); StartDecoder(*new MusicPipe());
}
break; break;
@ -704,9 +711,10 @@ Player::ProcessCommand()
break; break;
case PlayerCommand::SEEK: case PlayerCommand::SEEK:
pc.Unlock(); {
SeekDecoder(); const ScopeUnlock unlock(pc.mutex);
pc.Lock(); SeekDecoder();
}
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(); {
pc.bit_rate = chunk->bit_rate; const ScopeLock lock(pc.mutex);
pc.Unlock(); pc.bit_rate = chunk->bit_rate;
}
/* 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;
} }