Commit a9b62a2e authored by Max Kellermann's avatar Max Kellermann

PlayerControl: add second Cond object

This fixes a deadlock bug introduced by 18076ac9. After all, the second Cond was necessary. The problem: two threads can wait for a signal at the same time. The player thread waits for the output thread to finish playback. The main thread waits for the player thread to complete a command. The output thread finishes playback, and sends a signal, which unfortunately does not wake up the player thread, but the main thread. The main thread sees that the command is still not finished, and waits again. The signal is lost forever, and MPD is deadlocked.
parent 49567f1f
......@@ -60,7 +60,7 @@ static void
player_command_wait_locked(struct player_control *pc)
{
while (pc->command != PLAYER_COMMAND_NONE)
pc->cond.wait(pc->mutex);
pc->ClientWait();
}
static void
......
......@@ -108,6 +108,13 @@ struct player_control {
*/
Cond cond;
/**
* This object gets signalled when the player thread has
* finished the #command. It wakes up the client that waits
* (i.e. the main thread).
*/
Cond client_cond;
enum player_command command;
enum player_state state;
......@@ -191,10 +198,35 @@ struct player_control {
* prior to calling this function.
*/
void Wait() {
assert(thread == g_thread_self());
cond.wait(mutex);
}
/**
* Wake up the client waiting for command completion.
*
* Caller must lock the object.
*/
void ClientSignal() {
assert(thread == g_thread_self());
client_cond.signal();
}
/**
* The client calls this method to wait for command
* completion.
*
* Caller must lock the object.
*/
void ClientWait() {
assert(thread != g_thread_self());
client_cond.wait(mutex);
}
/**
* @param song the song to be queued; the given instance will
* be owned and freed by the player
*/
......
......@@ -147,7 +147,7 @@ player_command_finished_locked(struct player_control *pc)
assert(pc->command != PLAYER_COMMAND_NONE);
pc->command = PLAYER_COMMAND_NONE;
pc->cond.signal();
pc->ClientSignal();
}
static void
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment