From: Jaroslav Kysela Date: Thu, 19 Nov 2015 21:03:02 +0000 (+0100) Subject: IPTV: http - add safe global lock X-Git-Tag: v4.2.1~1504 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=2a249a42f835ccc9810490446f3a317463f5fcd5;p=thirdparty%2Ftvheadend.git IPTV: http - add safe global lock --- diff --git a/src/input/mpegts/iptv/iptv_http.c b/src/input/mpegts/iptv/iptv_http.c index b4b1b4154..4e71be4a1 100644 --- a/src/input/mpegts/iptv/iptv_http.c +++ b/src/input/mpegts/iptv/iptv_http.c @@ -38,6 +38,34 @@ typedef struct http_priv { time_t hls_last_si; } http_priv_t; +/* + * + */ +static int +iptv_http_safe_global_lock( iptv_mux_t *im ) +{ + int r; + + while (1) { + if (im->mm_active == NULL) + return 0; + r = pthread_mutex_trylock(&global_lock); + if (r == 0) + break; + if (r != EBUSY) + continue; + sched_yield(); + if (im->mm_active == NULL) + return 0; + r = pthread_mutex_trylock(&global_lock); + if (r == 0) + break; + if (r == EBUSY) + usleep(10000); + } + return 1; +} + /* * */ @@ -166,14 +194,15 @@ iptv_http_header ( http_client_t *hc ) hp->m3u_header = 0; hp->off = 0; - pthread_mutex_lock(&global_lock); - if (!hp->started) { - iptv_input_mux_started(hp->im); - } else { - iptv_input_recv_flush(hp->im); + if (iptv_http_safe_global_lock(hp->im)) { + if (!hp->started) { + iptv_input_mux_started(hp->im); + } else { + iptv_input_recv_flush(hp->im); + } + pthread_mutex_unlock(&global_lock); + hp->started = 1; } - pthread_mutex_unlock(&global_lock); - hp->started = 1; return 0; } @@ -238,8 +267,7 @@ next: pthread_mutex_unlock(&iptv_lock); - if (pause) { - pthread_mutex_lock(&global_lock); + if (pause && iptv_http_safe_global_lock(im)) { if (im->mm_active) gtimer_arm(&im->im_pause_timer, iptv_input_unpause, im, 1); pthread_mutex_unlock(&global_lock); @@ -262,6 +290,7 @@ iptv_http_complete if (hp == NULL || hp->im == NULL) return 0; + if (hp->m3u_header) { hp->m3u_header = 0;