decoder/Control: pass std::unique_lock<> to Cond::wait()

This commit is contained in:
Max Kellermann
2019-04-26 18:34:16 +02:00
parent 23d56cb6a1
commit cf348f9fae
5 changed files with 68 additions and 62 deletions

View File

@@ -74,10 +74,10 @@ DecoderBridge::CheckCancelRead() const noexcept
* one.
*/
static DecoderCommand
need_chunks(DecoderControl &dc) noexcept
NeedChunks(DecoderControl &dc, std::unique_lock<Mutex> &lock) noexcept
{
if (dc.command == DecoderCommand::NONE)
dc.Wait();
dc.Wait(lock);
return dc.command;
}
@@ -85,8 +85,8 @@ need_chunks(DecoderControl &dc) noexcept
static DecoderCommand
LockNeedChunks(DecoderControl &dc) noexcept
{
const std::lock_guard<Mutex> protect(dc.mutex);
return need_chunks(dc);
std::unique_lock<Mutex> lock(dc.mutex);
return NeedChunks(dc, lock);
}
MusicChunk *

View File

@@ -39,12 +39,12 @@ DecoderControl::~DecoderControl() noexcept
}
void
DecoderControl::WaitForDecoder() noexcept
DecoderControl::WaitForDecoder(std::unique_lock<Mutex> &lock) noexcept
{
assert(!client_is_waiting);
client_is_waiting = true;
client_cond.wait(mutex);
client_cond.wait(lock);
assert(client_is_waiting);
client_is_waiting = false;
@@ -88,7 +88,8 @@ DecoderControl::IsCurrentSong(const DetachedSong &_song) const noexcept
}
void
DecoderControl::Start(std::unique_ptr<DetachedSong> _song,
DecoderControl::Start(std::unique_lock<Mutex> &lock,
std::unique_ptr<DetachedSong> _song,
SongTime _start_time, SongTime _end_time,
MusicBuffer &_buffer,
std::shared_ptr<MusicPipe> _pipe) noexcept
@@ -103,25 +104,25 @@ DecoderControl::Start(std::unique_ptr<DetachedSong> _song,
pipe = std::move(_pipe);
ClearError();
SynchronousCommandLocked(DecoderCommand::START);
SynchronousCommandLocked(lock, DecoderCommand::START);
}
void
DecoderControl::Stop() noexcept
DecoderControl::Stop(std::unique_lock<Mutex> &lock) noexcept
{
if (command != DecoderCommand::NONE)
/* Attempt to cancel the current command. If it's too
late and the decoder thread is already executing
the old command, we'll call STOP again in this
function (see below). */
SynchronousCommandLocked(DecoderCommand::STOP);
SynchronousCommandLocked(lock, DecoderCommand::STOP);
if (state != DecoderState::STOP && state != DecoderState::ERROR)
SynchronousCommandLocked(DecoderCommand::STOP);
SynchronousCommandLocked(lock, DecoderCommand::STOP);
}
void
DecoderControl::Seek(SongTime t)
DecoderControl::Seek(std::unique_lock<Mutex> &lock, SongTime t)
{
assert(state != DecoderState::START);
assert(state != DecoderState::ERROR);
@@ -145,7 +146,7 @@ DecoderControl::Seek(SongTime t)
seek_time = t;
seek_error = false;
SynchronousCommandLocked(DecoderCommand::SEEK);
SynchronousCommandLocked(lock, DecoderCommand::SEEK);
if (seek_error)
throw std::runtime_error("Decoder failed to seek");

View File

@@ -207,8 +207,8 @@ public:
* is only valid in the decoder thread. The object must be locked
* prior to calling this function.
*/
void Wait() noexcept {
cond.wait(mutex);
void Wait(std::unique_lock<Mutex> &lock) noexcept {
cond.wait(lock);
}
/**
@@ -218,7 +218,7 @@ public:
*
* Caller must hold the lock.
*/
void WaitForDecoder() noexcept;
void WaitForDecoder(std::unique_lock<Mutex> &lock) noexcept;
bool IsIdle() const noexcept {
return state == DecoderState::STOP ||
@@ -318,9 +318,9 @@ private:
* To be called from the client thread. Caller must lock the
* object.
*/
void WaitCommandLocked() noexcept {
void WaitCommandLocked(std::unique_lock<Mutex> &lock) noexcept {
while (command != DecoderCommand::NONE)
WaitForDecoder();
WaitForDecoder(lock);
}
/**
@@ -330,10 +330,11 @@ private:
* To be called from the client thread. Caller must lock the
* object.
*/
void SynchronousCommandLocked(DecoderCommand cmd) noexcept {
void SynchronousCommandLocked(std::unique_lock<Mutex> &lock,
DecoderCommand cmd) noexcept {
command = cmd;
Signal();
WaitCommandLocked();
WaitCommandLocked(lock);
}
/**
@@ -344,9 +345,9 @@ private:
* object.
*/
void LockSynchronousCommand(DecoderCommand cmd) noexcept {
const std::lock_guard<Mutex> protect(mutex);
std::unique_lock<Mutex> lock(mutex);
ClearError();
SynchronousCommandLocked(cmd);
SynchronousCommandLocked(lock, cmd);
}
void LockAsynchronousCommand(DecoderCommand cmd) noexcept {
@@ -382,7 +383,8 @@ public:
* @param pipe the pipe which receives the decoded chunks (owned by
* the caller)
*/
void Start(std::unique_ptr<DetachedSong> song,
void Start(std::unique_lock<Mutex> &lock,
std::unique_ptr<DetachedSong> song,
SongTime start_time, SongTime end_time,
MusicBuffer &buffer,
std::shared_ptr<MusicPipe> pipe) noexcept;
@@ -390,14 +392,14 @@ public:
/**
* Caller must lock the object.
*/
void Stop() noexcept;
void Stop(std::unique_lock<Mutex> &lock) noexcept;
/**
* Throws #std::runtime_error on error.
*
* Caller must lock the object.
*/
void Seek(SongTime t);
void Seek(std::unique_lock<Mutex> &lock, SongTime t);
void Quit() noexcept;

View File

@@ -487,7 +487,7 @@ DecoderControl::RunThread() noexcept
{
SetThreadName("decoder");
const std::lock_guard<Mutex> protect(mutex);
std::unique_lock<Mutex> lock(mutex);
do {
assert(state == DecoderState::STOP ||
@@ -528,7 +528,7 @@ DecoderControl::RunThread() noexcept
break;
case DecoderCommand::NONE:
Wait();
Wait(lock);
break;
}
} while (command != DecoderCommand::NONE || !quit);