DecoderControl: convert functions to methods

This commit is contained in:
Max Kellermann 2013-09-27 09:18:03 +02:00
parent ada67a6a4f
commit ef663810a2
2 changed files with 52 additions and 40 deletions

View File

@ -48,41 +48,6 @@ decoder_control::~decoder_control()
g_free(mixramp_prev_end); g_free(mixramp_prev_end);
} }
static void
dc_command_wait_locked(struct decoder_control *dc)
{
while (dc->command != DECODE_COMMAND_NONE)
dc->WaitForDecoder();
}
static void
dc_command_locked(struct decoder_control *dc, enum decoder_command cmd)
{
dc->command = cmd;
dc->Signal();
dc_command_wait_locked(dc);
}
static void
dc_command(struct decoder_control *dc, enum decoder_command cmd)
{
dc->Lock();
dc->ClearError();
dc_command_locked(dc, cmd);
dc->Unlock();
}
static void
dc_command_async(struct decoder_control *dc, enum decoder_command cmd)
{
dc->Lock();
dc->command = cmd;
dc->Signal();
dc->Unlock();
}
bool bool
decoder_control::IsCurrentSong(const Song *_song) const decoder_control::IsCurrentSong(const Song *_song) const
{ {
@ -119,7 +84,7 @@ decoder_control::Start(Song *_song,
buffer = &_buffer; buffer = &_buffer;
pipe = &_pipe; pipe = &_pipe;
dc_command(this, DECODE_COMMAND_START); LockSynchronousCommand(DECODE_COMMAND_START);
} }
void void
@ -132,10 +97,10 @@ decoder_control::Stop()
late and the decoder thread is already executing late and the decoder thread is already executing
the old command, we'll call STOP again in this the old command, we'll call STOP again in this
function (see below). */ function (see below). */
dc_command_locked(this, DECODE_COMMAND_STOP); SynchronousCommandLocked(DECODE_COMMAND_STOP);
if (state != DECODE_STATE_STOP && state != DECODE_STATE_ERROR) if (state != DECODE_STATE_STOP && state != DECODE_STATE_ERROR)
dc_command_locked(this, DECODE_COMMAND_STOP); SynchronousCommandLocked(DECODE_COMMAND_STOP);
Unlock(); Unlock();
} }
@ -152,7 +117,7 @@ decoder_control::Seek(double where)
seek_where = where; seek_where = where;
seek_error = false; seek_error = false;
dc_command(this, DECODE_COMMAND_SEEK); SynchronousCommandLocked(DECODE_COMMAND_SEEK);
return !seek_error; return !seek_error;
} }
@ -163,7 +128,7 @@ decoder_control::Quit()
assert(thread != nullptr); assert(thread != nullptr);
quit = true; quit = true;
dc_command_async(this, DECODE_COMMAND_STOP); LockAsynchronousCommand(DECODE_COMMAND_STOP);
g_thread_join(thread); g_thread_join(thread);
thread = nullptr; thread = nullptr;

View File

@ -278,6 +278,53 @@ struct decoder_control {
return result; return result;
} }
private:
/**
* Wait for the command to be finished by the decoder thread.
*
* To be called from the client thread. Caller must lock the
* object.
*/
void WaitCommandLocked() {
while (command != DECODE_COMMAND_NONE)
WaitForDecoder();
}
/**
* Send a command to the decoder thread and synchronously wait
* for it to finish.
*
* To be called from the client thread. Caller must lock the
* object.
*/
void SynchronousCommandLocked(decoder_command cmd) {
command = cmd;
Signal();
WaitCommandLocked();
}
/**
* Send a command to the decoder thread and synchronously wait
* for it to finish.
*
* To be called from the client thread. This method locks the
* object.
*/
void LockSynchronousCommand(decoder_command cmd) {
Lock();
ClearError();
SynchronousCommandLocked(cmd);
Unlock();
}
void LockAsynchronousCommand(decoder_command cmd) {
Lock();
command = cmd;
Signal();
Unlock();
}
public:
/** /**
* Start the decoder. * Start the decoder.
* *