From 2d44dfaba9d5fe47c35ab6b5d552a25f02bd0bd5 Mon Sep 17 00:00:00 2001 From: wm4 Date: Mon, 11 Jul 2016 22:20:23 +0200 Subject: [PATCH] cache: minor simplification Every cache_wakeup_and_wait() caller has to deal with asynchronous stream abort, so why not make it somehow part of the function. --- stream/cache.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/stream/cache.c b/stream/cache.c index 02d618f18d..3aad35d95d 100644 --- a/stream/cache.c +++ b/stream/cache.c @@ -133,7 +133,8 @@ enum { // Used by the main thread to wakeup the cache thread, and to wait for the // cache thread. The cache mutex has to be locked when calling this function. // *retry_time should be set to 0 on the first call. -static void cache_wakeup_and_wait(struct priv *s, double *retry_time) +// Return false if the stream has been aborted. +static bool cache_wakeup_and_wait(struct priv *s, double *retry_time) { double start = mp_time_sec(); if (*retry_time >= CACHE_WAIT_TIME) { @@ -147,6 +148,8 @@ static void cache_wakeup_and_wait(struct priv *s, double *retry_time) if (*retry_time >= 0) *retry_time += mp_time_sec() - start; + + return !mp_cancel_test(s->cache->cancel); } // Runs in the cache thread @@ -551,9 +554,8 @@ static int cache_fill_buffer(struct stream *cache, char *buffer, int max_len) if (s->eof && s->read_filepos >= s->max_filepos && s->reads >= retry) break; s->idle = false; - if (mp_cancel_test(s->cache->cancel)) + if (!cache_wakeup_and_wait(s, &retry_time)) break; - cache_wakeup_and_wait(s, &retry_time); } } @@ -587,8 +589,10 @@ static int cache_seek(stream_t *cache, int64_t pos) s->control = CACHE_CTRL_SEEK; s->control_res = 0; double retry = 0; - while (s->control != CACHE_CTRL_NONE && !mp_cancel_test(s->cache->cancel)) - cache_wakeup_and_wait(s, &retry); + while (s->control != CACHE_CTRL_NONE) { + if (!cache_wakeup_and_wait(s, &retry)) + break; + } r = s->control_res; pthread_cond_signal(&s->wakeup); } @@ -617,12 +621,11 @@ static int cache_control(stream_t *cache, int cmd, void *arg) s->control_arg = arg; double retry = 0; while (s->control != CACHE_CTRL_NONE) { - if (mp_cancel_test(s->cache->cancel)) { + if (!cache_wakeup_and_wait(s, &retry)) { s->eof = 1; r = STREAM_UNSUPPORTED; goto done; } - cache_wakeup_and_wait(s, &retry); } r = s->control_res; if (s->control_flush) {