From 953ff6b3908bcfdd69a9189c991e55f362d2d2dc Mon Sep 17 00:00:00 2001 From: wm4 Date: Wed, 9 Mar 2016 23:55:16 +0100 Subject: demux: replace demux_pause/demux_unpause with demux_run_on_thread This pause stuff is bothersome and is needed only for a few corner- cases. This commit removes it from the demuxer public API and replaces it with a demux_run_on_thread() function and refactors the code which needed demux_pause(). The next commit will change the implementation. --- demux/demux.c | 56 ++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 42 insertions(+), 14 deletions(-) (limited to 'demux/demux.c') diff --git a/demux/demux.c b/demux/demux.c index a7241d9a9f..2770413938 100644 --- a/demux/demux.c +++ b/demux/demux.c @@ -1533,20 +1533,22 @@ static int cached_demux_control(struct demux_internal *in, int cmd, void *arg) return DEMUXER_CTRL_DONTKNOW; } -int demux_control(demuxer_t *demuxer, int cmd, void *arg) +struct demux_control_args { + struct demuxer *demuxer; + int cmd; + void *arg; + int *r; +}; + +static void thread_demux_control(void *p) { + struct demux_control_args *args = p; + struct demuxer *demuxer = args->demuxer; + int cmd = args->cmd; + void *arg = args->arg; struct demux_internal *in = demuxer->in; - - if (in->threading) { - pthread_mutex_lock(&in->lock); - int cr = cached_demux_control(in, cmd, arg); - pthread_mutex_unlock(&in->lock); - if (cr != DEMUXER_CTRL_DONTKNOW) - return cr; - } - int r = DEMUXER_CTRL_NOTIMPL; - demux_pause(demuxer); + if (cmd == DEMUXER_CTRL_STREAM_CTRL) { struct demux_ctrl_stream_ctrl *c = arg; if (in->threading) @@ -1561,7 +1563,26 @@ int demux_control(demuxer_t *demuxer, int cmd, void *arg) if (demuxer->desc->control) r = demuxer->desc->control(demuxer->in->d_thread, cmd, arg); } - demux_unpause(demuxer); + + *args->r = r; +} + +int demux_control(demuxer_t *demuxer, int cmd, void *arg) +{ + struct demux_internal *in = demuxer->in; + + if (in->threading) { + pthread_mutex_lock(&in->lock); + int cr = cached_demux_control(in, cmd, arg); + pthread_mutex_unlock(&in->lock); + if (cr != DEMUXER_CTRL_DONTKNOW) + return cr; + } + + int r = 0; + struct demux_control_args args = {demuxer, cmd, arg, &r}; + demux_run_on_thread(demuxer, thread_demux_control, &args); + return r; } @@ -1575,7 +1596,7 @@ int demux_stream_control(demuxer_t *demuxer, int ctrl, void *arg) // Make the demuxer thread stop doing anything. // demux_unpause() wakes up the thread again. // Can be nested with other calls, but trying to read packets may deadlock. -void demux_pause(demuxer_t *demuxer) +static void demux_pause(demuxer_t *demuxer) { struct demux_internal *in = demuxer->in; assert(demuxer == in->d_user); @@ -1593,7 +1614,7 @@ void demux_pause(demuxer_t *demuxer) pthread_mutex_unlock(&in->lock); } -void demux_unpause(demuxer_t *demuxer) +static void demux_unpause(demuxer_t *demuxer) { struct demux_internal *in = demuxer->in; assert(demuxer == in->d_user); @@ -1608,6 +1629,13 @@ void demux_unpause(demuxer_t *demuxer) pthread_mutex_unlock(&in->lock); } +void demux_run_on_thread(struct demuxer *demuxer, void (*fn)(void *), void *ctx) +{ + demux_pause(demuxer); + fn(ctx); + demux_unpause(demuxer); +} + bool demux_cancel_test(struct demuxer *demuxer) { return mp_cancel_test(demuxer->stream->cancel); -- cgit v1.2.3