diff --git a/addons/acodec/helper.c b/addons/acodec/helper.c index 26194ca7d..bcf16c05c 100644 --- a/addons/acodec/helper.c +++ b/addons/acodec/helper.c @@ -11,19 +11,23 @@ void _al_acodec_start_feed_thread(ALLEGRO_AUDIO_STREAM *stream) stream->feed_thread_started_cond = al_create_cond(); stream->feed_thread_started_mutex = al_create_mutex(); al_start_thread(stream->feed_thread); -} - -void _al_acodec_stop_feed_thread(ALLEGRO_AUDIO_STREAM *stream) -{ - ALLEGRO_EVENT quit_event; /* Need to wait for the thread to start, otherwise the quit event may be - * sent before the event source is registered with the queue. */ + * sent before the event source is registered with the queue. + * + * This also makes the pre-fill system thread safe, as it needs to operate + * before the mutexes are set up. + */ al_lock_mutex(stream->feed_thread_started_mutex); while (!stream->feed_thread_started) { al_wait_cond(stream->feed_thread_started_cond, stream->feed_thread_started_mutex); } al_unlock_mutex(stream->feed_thread_started_mutex); +} + +void _al_acodec_stop_feed_thread(ALLEGRO_AUDIO_STREAM *stream) +{ + ALLEGRO_EVENT quit_event; quit_event.type = _KCM_STREAM_FEEDER_QUIT_EVENT_TYPE; al_emit_user_event(al_get_audio_stream_event_source(stream), &quit_event, NULL); diff --git a/addons/audio/kcm_stream.c b/addons/audio/kcm_stream.c index 8d784fa9a..2b8487c27 100644 --- a/addons/audio/kcm_stream.c +++ b/addons/audio/kcm_stream.c @@ -692,11 +692,6 @@ void *_al_kcm_feed_stream(ALLEGRO_THREAD *self, void *vstream) queue = al_create_event_queue(); al_register_event_source(queue, &stream->spl.es); - al_lock_mutex(stream->feed_thread_started_mutex); - stream->feed_thread_started = true; - al_broadcast_cond(stream->feed_thread_started_cond); - al_unlock_mutex(stream->feed_thread_started_mutex); - stream->quit_feed_thread = false; while (!stream->quit_feed_thread) { @@ -708,7 +703,6 @@ void *_al_kcm_feed_stream(ALLEGRO_THREAD *self, void *vstream) if ((prefill || event.type == ALLEGRO_EVENT_AUDIO_STREAM_FRAGMENT) && !stream->is_draining) { - prefill = false; unsigned long bytes; unsigned long bytes_written; ALLEGRO_MUTEX *stream_mutex; @@ -784,6 +778,13 @@ void *_al_kcm_feed_stream(ALLEGRO_THREAD *self, void *vstream) fin_event.user.timestamp = al_get_time(); al_emit_user_event(&stream->spl.es, &fin_event, NULL); } + if (prefill) { + al_lock_mutex(stream->feed_thread_started_mutex); + stream->feed_thread_started = true; + al_broadcast_cond(stream->feed_thread_started_cond); + al_unlock_mutex(stream->feed_thread_started_mutex); + } + prefill = false; } al_destroy_event_queue(queue);