From ba8160cf59e4c13c6619c131ae14b29046eb212c Mon Sep 17 00:00:00 2001 From: wm4 Date: Fri, 30 Jun 2017 13:14:39 +0200 Subject: client API: change mpv_create() behavior, run init always on mpv thread This takes car eof unsubtle bugs if something at init does not work (specifically if mp_new_client() returns NULL). It also removes the need for that PMP MF hack. --- player/client.c | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/player/client.c b/player/client.c index e01f121a0c..c41465419a 100644 --- a/player/client.c +++ b/player/client.c @@ -31,6 +31,7 @@ #include "input/cmd_list.h" #include "misc/ctype.h" #include "misc/dispatch.h" +#include "misc/rendezvous.h" #include "options/m_config.h" #include "options/m_option.h" #include "options/m_property.h" @@ -440,12 +441,25 @@ void mpv_terminate_destroy(mpv_handle *ctx) pthread_join(playthread, NULL); } -static void *playback_thread(void *p) +static void *core_thread(void *tag) { - struct MPContext *mpctx = p; + mpthread_set_name("mpv core"); + + struct MPContext *mpctx = mp_create(); mpctx->autodetach = true; + mpv_handle *ctx = mp_new_client(mpctx->clients, "main"); + if (ctx) { + ctx->owner = true; + ctx->fuzzy_initialized = true; + m_config_set_profile(mpctx->mconfig, "libmpv", 0); + } else { + mp_destroy(mpctx); + } - mpthread_set_name("mpv core"); + // Let mpv_create() return, and pass it the handle. + mp_rendezvous(tag, (intptr_t)(void *)ctx); + if (!ctx) + return NULL; while (!mpctx->initialized && mpctx->stop_play != PT_QUIT) mp_idle(mpctx); @@ -477,23 +491,17 @@ mpv_handle *mpv_create(void) "Call 'setlocale(LC_NUMERIC, \"C\");' in your code.\n"); return NULL; } - struct MPContext *mpctx = mp_create(); - mpv_handle *ctx = mp_new_client(mpctx->clients, "main"); - if (ctx) { - ctx->owner = true; - ctx->fuzzy_initialized = true; - m_config_set_profile(mpctx->mconfig, "libmpv", 0); - } else { - mp_destroy(mpctx); - } + char tag; pthread_t thread; - if (pthread_create(&thread, NULL, playback_thread, ctx->mpctx) != 0) { - mpv_terminate_destroy(ctx); + if (pthread_create(&thread, NULL, core_thread, &tag) != 0) return NULL; - } - return ctx; + mpv_handle *res = (void *)mp_rendezvous(&tag, 0); + if (!res) + pthread_join(thread, NULL); + + return res; } mpv_handle *mpv_create_client(mpv_handle *ctx, const char *name) -- cgit v1.2.3