From a7f3cf473707b19bff9ea80b227490c8b305b601 Mon Sep 17 00:00:00 2001 From: wm4 Date: Fri, 9 Mar 2018 06:00:51 +0100 Subject: client API: add mpv_create_weak_client() --- player/client.c | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) (limited to 'player/client.c') diff --git a/player/client.c b/player/client.c index a887a16d2a..d347033fe5 100644 --- a/player/client.c +++ b/player/client.c @@ -136,6 +136,7 @@ struct mpv_handle { uint64_t property_event_masks; // or-ed together event masks of all properties bool fuzzy_initialized; // see scripting.c wait_loaded() + bool is_weak; // can not keep core alive on its own struct mp_log_buffer *messages; }; @@ -269,6 +270,13 @@ struct mpv_handle *mp_new_client(struct mp_client_api *clients, const char *name return client; } +void mp_client_set_weak(struct mpv_handle *ctx) +{ + pthread_mutex_lock(&ctx->lock); + ctx->is_weak = true; + pthread_mutex_unlock(&ctx->lock); +} + const char *mpv_client_name(mpv_handle *ctx) { return ctx->name; @@ -416,8 +424,11 @@ static void mp_destroy_client(mpv_handle *ctx, bool terminate) if (mpctx->is_cli) { terminate = false; } else { - // If the last mpv_handle got destroyed, destroy the core. - if (clients->num_clients == 0) + // If the last strong mpv_handle got destroyed, destroy the core. + bool has_strong_ref = false; + for (int n = 0; n < clients->num_clients; n++) + has_strong_ref |= !clients->clients[n]->is_weak; + if (!has_strong_ref) terminate = true; // Reserve the right to destroy mpctx for us. @@ -550,6 +561,14 @@ mpv_handle *mpv_create_client(mpv_handle *ctx, const char *name) return new; } +mpv_handle *mpv_create_weak_client(mpv_handle *ctx, const char *name) +{ + mpv_handle *new = mpv_create_client(ctx, name); + if (new) + mp_client_set_weak(new); + return new; +} + int mpv_initialize(mpv_handle *ctx) { lock_core(ctx); -- cgit v1.2.3