diff options
author | Max Kellermann <max@duempel.org> | 2009-10-29 17:06:40 +0100 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2009-10-29 17:06:40 +0100 |
commit | bde3d1433997af8cc430f4b9d38e5bde97d3b760 (patch) | |
tree | 59ed180c3d0bf9f7b37065eea3fa13d7ffd72286 /src/output_control.c | |
parent | 1403172ef397c3dfc58a64c999a362cca977241b (diff) | |
download | mpd-bde3d1433997af8cc430f4b9d38e5bde97d3b760.tar.gz mpd-bde3d1433997af8cc430f4b9d38e5bde97d3b760.tar.xz mpd-bde3d1433997af8cc430f4b9d38e5bde97d3b760.zip |
output: consistently lock audio output objects
Always keep the audio_output object locked within the output thread,
unless a plugin method is called. This fixes several race conditions.
Diffstat (limited to 'src/output_control.c')
-rw-r--r-- | src/output_control.c | 51 |
1 files changed, 39 insertions, 12 deletions
diff --git a/src/output_control.c b/src/output_control.c index 6512cbe74..46d8f8696 100644 --- a/src/output_control.c +++ b/src/output_control.c @@ -24,6 +24,7 @@ #include "mixer_control.h" #include "mixer_plugin.h" #include "filter_plugin.h" +#include "notify.h" #include <assert.h> #include <stdlib.h> @@ -39,8 +40,10 @@ struct notify audio_output_client_notify; static void ao_command_wait(struct audio_output *ao) { while (ao->command != AO_COMMAND_NONE) { - notify_signal(&ao->notify); + g_cond_signal(ao->cond); + g_mutex_unlock(ao->mutex); notify_wait(&audio_output_client_notify); + g_mutex_lock(ao->mutex); } } @@ -56,7 +59,7 @@ static void ao_command_async(struct audio_output *ao, { assert(ao->command == AO_COMMAND_NONE); ao->command = cmd; - notify_signal(&ao->notify); + g_cond_signal(ao->cond); } void @@ -74,7 +77,9 @@ audio_output_enable(struct audio_output *ao) audio_output_thread_start(ao); } + g_mutex_lock(ao->mutex); ao_command(ao, AO_COMMAND_ENABLE); + g_mutex_unlock(ao->mutex); } void @@ -91,7 +96,9 @@ audio_output_disable(struct audio_output *ao) return; } + g_mutex_lock(ao->mutex); ao_command(ao, AO_COMMAND_DISABLE); + g_mutex_unlock(ao->mutex); } static bool @@ -157,23 +164,31 @@ audio_output_update(struct audio_output *ao, { assert(mp != NULL); + g_mutex_lock(ao->mutex); + if (ao->enabled && ao->really_enabled) { if (ao->fail_timer == NULL || - g_timer_elapsed(ao->fail_timer, NULL) > REOPEN_AFTER) - return audio_output_open(ao, audio_format, mp); + g_timer_elapsed(ao->fail_timer, NULL) > REOPEN_AFTER) { + bool success = audio_output_open(ao, audio_format, mp); + g_mutex_unlock(ao->mutex); + return success; + } } else if (audio_output_is_open(ao)) audio_output_close(ao); + g_mutex_unlock(ao->mutex); return false; } void audio_output_play(struct audio_output *ao) { - if (!ao->open) - return; + g_mutex_lock(ao->mutex); - notify_signal(&ao->notify); + if (audio_output_is_open(ao)) + g_cond_signal(ao->cond); + + g_mutex_unlock(ao->mutex); } void audio_output_pause(struct audio_output *ao) @@ -184,27 +199,37 @@ void audio_output_pause(struct audio_output *ao) mixer_auto_close()) */ mixer_auto_close(ao->mixer); - ao_command_async(ao, AO_COMMAND_PAUSE); + g_mutex_lock(ao->mutex); + if (audio_output_is_open(ao)) + ao_command_async(ao, AO_COMMAND_PAUSE); + g_mutex_unlock(ao->mutex); } void audio_output_cancel(struct audio_output *ao) { - ao_command_async(ao, AO_COMMAND_CANCEL); + g_mutex_lock(ao->mutex); + if (audio_output_is_open(ao)) + ao_command_async(ao, AO_COMMAND_CANCEL); + g_mutex_unlock(ao->mutex); } void audio_output_close(struct audio_output *ao) { - assert(!ao->open || ao->fail_timer == NULL); - if (ao->mixer != NULL) mixer_auto_close(ao->mixer); + g_mutex_lock(ao->mutex); + + assert(!ao->open || ao->fail_timer == NULL); + if (ao->open) ao_command(ao, AO_COMMAND_CLOSE); else if (ao->fail_timer != NULL) { g_timer_destroy(ao->fail_timer); ao->fail_timer = NULL; } + + g_mutex_unlock(ao->mutex); } void audio_output_finish(struct audio_output *ao) @@ -214,7 +239,9 @@ void audio_output_finish(struct audio_output *ao) assert(ao->fail_timer == NULL); if (ao->thread != NULL) { + g_mutex_lock(ao->mutex); ao_command(ao, AO_COMMAND_KILL); + g_mutex_unlock(ao->mutex); g_thread_join(ao->thread); } @@ -223,7 +250,7 @@ void audio_output_finish(struct audio_output *ao) ao_plugin_finish(ao->plugin, ao->data); - notify_deinit(&ao->notify); + g_cond_free(ao->cond); g_mutex_free(ao->mutex); filter_free(ao->filter); |