From fc4e59f25d68aeb2a33333b01f12a440d5b737e6 Mon Sep 17 00:00:00 2001 From: wm4 Date: Sat, 18 May 2019 00:01:07 +0200 Subject: [PATCH] demux: cleaner mutex usage The demuxer layer can start a thread to decouple the rest of the player from blocking I/O (such as network accesses). But this particular function does not support running with the thread enabled. The mutex use within it is only since thread_work() may temporarily unlock the mutex, and unlocking an unlocked mutex is not allowed. Most of the rest of the code still does proper locking, even if it's pointless and effectively single-threaded. To make this look slightly cleaner, extend the mutex around the rest of the code (like threaded code would have to do). This is mostly a cosmetic change. --- demux/demux.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/demux/demux.c b/demux/demux.c index 3022469b18..ae6d3a96f6 100644 --- a/demux/demux.c +++ b/demux/demux.c @@ -2003,26 +2003,27 @@ int demux_read_packet_async(struct sh_stream *sh, struct demux_packet **out_pkt) struct demux_packet *demux_read_any_packet(struct demuxer *demuxer) { struct demux_internal *in = demuxer->in; + pthread_mutex_lock(&in->lock); assert(!in->threading); // doesn't work with threading + struct demux_packet *out_pkt = NULL; bool read_more = true; while (read_more && !in->blocked) { bool all_eof = true; for (int n = 0; n < in->num_streams; n++) { in->reading = true; // force read_packet() to read - struct demux_packet *out_pkt = NULL; int r = dequeue_packet(in->streams[n]->ds, &out_pkt); if (r > 0) - return out_pkt; + goto done; if (r == 0) all_eof = false; } // retry after calling this - pthread_mutex_lock(&in->lock); // lock only because thread_work unlocks read_more = thread_work(in); read_more &= !all_eof; - pthread_mutex_unlock(&in->lock); } - return NULL; +done: + pthread_mutex_unlock(&in->lock); + return out_pkt; } void demuxer_help(struct mp_log *log)