From a0308d31697c5dd601ee036acbc937c361746cc3 Mon Sep 17 00:00:00 2001 From: wm4 Date: Sun, 6 May 2018 13:32:50 +0200 Subject: client: merge can_terminate() function This has some tricky interactions. In particular, it requires the core to be locked due to reading outstanding_async, which is documented on the only caller only. It's probably better to merge it with its only caller. The new code should be strictly equivalent, other than the fact that it doesn't temporarily unlock+lock when entering the loop for the first time (which doesn't matter here). --- player/client.c | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) (limited to 'player') diff --git a/player/client.c b/player/client.c index 2c21dd727c..02a8db2911 100644 --- a/player/client.c +++ b/player/client.c @@ -483,32 +483,29 @@ void mpv_terminate_destroy(mpv_handle *ctx) mp_destroy_client(ctx, true); } -static bool can_terminate(struct MPContext *mpctx) -{ - struct mp_client_api *clients = mpctx->clients; - - pthread_mutex_lock(&clients->lock); - bool ok = clients->num_clients == 0 && mpctx->outstanding_async == 0 && - (mpctx->is_cli || clients->terminate_core_thread); - pthread_mutex_unlock(&clients->lock); - - return ok; -} - // Can be called on the core thread only. Idempotent. void mp_shutdown_clients(struct MPContext *mpctx) { struct mp_client_api *clients = mpctx->clients; - // Prevent that new clients can appear. pthread_mutex_lock(&clients->lock); + + // Prevent that new clients can appear. clients->shutting_down = true; - pthread_mutex_unlock(&clients->lock); - while (!can_terminate(mpctx)) { + // Wait until we can terminate. + while (clients->num_clients || mpctx->outstanding_async || + !(mpctx->is_cli || clients->terminate_core_thread)) + { + pthread_mutex_unlock(&clients->lock); + mp_client_broadcast_event(mpctx, MPV_EVENT_SHUTDOWN, NULL); mp_wait_events(mpctx); + + pthread_mutex_lock(&clients->lock); } + + pthread_mutex_unlock(&clients->lock); } static void *core_thread(void *p) -- cgit v1.2.3