From patchwork Fri Dec 2 08:22:01 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matt Oliver X-Patchwork-Id: 1656 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.65.86 with SMTP id o83csp162370vsa; Fri, 2 Dec 2016 00:22:12 -0800 (PST) X-Received: by 10.28.49.3 with SMTP id x3mr1644957wmx.121.1480666932254; Fri, 02 Dec 2016 00:22:12 -0800 (PST) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id u200si2030343wmd.19.2016.12.02.00.22.11; Fri, 02 Dec 2016 00:22:12 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 9E55C689EC0; Fri, 2 Dec 2016 10:22:01 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-io0-f171.google.com (mail-io0-f171.google.com [209.85.223.171]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id D158C689A48 for ; Fri, 2 Dec 2016 10:21:54 +0200 (EET) Received: by mail-io0-f171.google.com with SMTP id c21so429348761ioj.1 for ; Fri, 02 Dec 2016 00:22:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:from:date:message-id:subject:to; bh=9aKsJ/lqhgWF9bKWVeUTVO8V8nADuSLZnv1XGDrSFtI=; b=GLhFPbEvraobVAcVrZLatDGeuiYLoRhMZ4e/6yHjbitC/mL8YTyyRehGyK1fJm9CHg 6+H46wBSNNzZg1fyGbIUzfZfw/lH7Kl7ZdreAcGL5gJlkz/Ztky3KNxb4PN7Fr+53SAV 2jH3wlD8lnvmUtzSPZHCv8bA+0MSShG+kJFNy7HisqWALF8B/UVl3UuqOjF3jbbZePOp Fb7h/FWTdA92KSwedA0M3CSTgBbB0iJuCEGeeWvt9z/1ZYRg1GpxZpcmV5cq/ApHuvE/ YTLY87s/IvxPEwvLk7B+nTk6t8fE3yFiAJexZUNK2iD+Kd0S/KJ41Sh9P6q9wtxjQmj6 u5Kw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=9aKsJ/lqhgWF9bKWVeUTVO8V8nADuSLZnv1XGDrSFtI=; b=TmWChHQyZmywf3Qks7wyozm1fi1DFnKM3L7O+4jV+WkiK+UIv8z3hIgYmAmXAf4d2D WYMfMKHQGRCuS9RHdyg/XxZKnnD1f31D8Z+qefHkiuPyZXSdMsONhyAYN5E69A1PZbaq gu4pL8YWYIsIO2ReLefz8/2b5ZnTLb+yqzHs5qDV9rCBeQ4QSffea63nBkaobnPQDXD2 KLmdezwh7u73Pj21atbQqITCCMc9aWLIPhrwlyKCHqx7xAjPsKoYBv/m8FObjgxA+BFD 4II9nY6AwX4RjTtnpBO7ooQyXTd4oGpnoz4kTuM54OZt0G09Z1ZCUavVdrUcDrPMqL4F Ozbg== X-Gm-Message-State: AKaTC03tIuipVf1sA7B02xvbfsTf019znE00lZOY8R/l/3hX/r0MWeCtd2p3mxIo4gUp7k3V6QXUZeuFdqdS5Q== X-Received: by 10.36.86.195 with SMTP id o186mr1656519itb.10.1480666921678; Fri, 02 Dec 2016 00:22:01 -0800 (PST) MIME-Version: 1.0 Received: by 10.36.70.138 with HTTP; Fri, 2 Dec 2016 00:22:01 -0800 (PST) From: Matt Oliver Date: Fri, 2 Dec 2016 19:22:01 +1100 Message-ID: To: FFmpeg development discussions and patches X-Content-Filtered-By: Mailman/MimeDel 2.1.20 Subject: [FFmpeg-devel] [PATCH 2/2] avformat/udp: Replace use of pthread_cancel. X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" --- configure | 6 ------ libavformat/udp.c | 48 +++++++++++++++++++----------------------------- 2 files changed, 19 insertions(+), 35 deletions(-) if (ff_neterrno() != AVERROR(EAGAIN) && ff_neterrno() != AVERROR(EINTR)) { @@ -564,7 +556,6 @@ static void *circular_buffer_task_tx( void *_URLContext) int64_t burst_interval = s->bitrate ? (s->burst_bits * 1000000 / s->bitrate) : 0; int64_t max_delay = s->bitrate ? ((int64_t)h->max_packet_size * 8 * 1000000 / s->bitrate + 1) : 0; - pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_cancelstate); pthread_mutex_lock(&s->mutex); if (ff_socket_nonblock(s->udp_fd, 0) < 0) { @@ -599,7 +590,6 @@ static void *circular_buffer_task_tx( void *_URLContext) av_fifo_generic_read(s->fifo, s->tmp, len, NULL); pthread_mutex_unlock(&s->mutex); - pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old_cancelstate); if (s->bitrate) { timestamp = av_gettime_relative(); @@ -645,7 +635,6 @@ static void *circular_buffer_task_tx( void *_URLContext) } } - pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_cancelstate); pthread_mutex_lock(&s->mutex); } @@ -730,7 +719,7 @@ static int udp_open(URLContext *h, const char *uri, int flags) /* assume if no digits were found it is a request to enable it */ if (buf == endptr) s->overrun_nonfatal = 1; - if (!HAVE_PTHREAD_CANCEL) + if (!HAVE_THREADS) av_log(h, AV_LOG_WARNING, "'overrun_nonfatal' option was set but it is not supported " "on this build (pthread support is required)\n"); @@ -758,14 +747,14 @@ static int udp_open(URLContext *h, const char *uri, int flags) } if (av_find_info_tag(buf, sizeof(buf), "fifo_size", p)) { s->circular_buffer_size = strtol(buf, NULL, 10); - if (!HAVE_PTHREAD_CANCEL) + if (!HAVE_THREADS) av_log(h, AV_LOG_WARNING, "'circular_buffer_size' option was set but it is not supported " "on this build (pthread support is required)\n"); } if (av_find_info_tag(buf, sizeof(buf), "bitrate", p)) { s->bitrate = strtoll(buf, NULL, 10); - if (!HAVE_PTHREAD_CANCEL) + if (!HAVE_THREADS) av_log(h, AV_LOG_WARNING, "'bitrate' option was set but it is not supported " "on this build (pthread support is required)\n"); @@ -951,7 +940,7 @@ static int udp_open(URLContext *h, const char *uri, int flags) s->udp_fd = udp_fd; -#if HAVE_PTHREAD_CANCEL +#if HAVE_THREADS /* Create thread in case of: 1. Input and circular_buffer_size is set @@ -988,7 +977,7 @@ static int udp_open(URLContext *h, const char *uri, int flags) #endif return 0; -#if HAVE_PTHREAD_CANCEL +#if HAVE_THREADS thread_fail: pthread_cond_destroy(&s->cond); cond_fail: @@ -1019,7 +1008,7 @@ static int udp_read(URLContext *h, uint8_t *buf, int size) { UDPContext *s = h->priv_data; int ret; -#if HAVE_PTHREAD_CANCEL +#if HAVE_THREADS int avail, nonblock = h->flags & AVIO_FLAG_NONBLOCK; if (s->fifo) { @@ -1051,12 +1040,13 @@ static int udp_read(URLContext *h, uint8_t *buf, int size) else { /* FIXME: using the monotonic clock would be better, but it does not exist on all supported platforms. */ + int ret; int64_t t = av_gettime() + 100000; struct timespec tv = { .tv_sec = t / 1000000, .tv_nsec = (t % 1000000) * 1000 }; - if (pthread_cond_timedwait(&s->cond, &s->mutex, &tv) < 0) { + if (ret = pthread_cond_timedwait(&s->cond, &s->mutex, &tv) < 0) { pthread_mutex_unlock(&s->mutex); - return AVERROR(errno == ETIMEDOUT ? EAGAIN : errno); + return AVERROR(ret == ETIMEDOUT ? EAGAIN : ret); } nonblock = 1; } @@ -1079,7 +1069,7 @@ static int udp_write(URLContext *h, const uint8_t *buf, int size) UDPContext *s = h->priv_data; int ret; -#if HAVE_PTHREAD_CANCEL +#if HAVE_THREADS if (s->fifo) { uint8_t tmp[4]; @@ -1128,7 +1118,7 @@ static int udp_close(URLContext *h) { UDPContext *s = h->priv_data; -#if HAVE_PTHREAD_CANCEL +#if HAVE_THREADS // Request close once writing is finished if (s->thread_started && !(h->flags & AVIO_FLAG_READ)) { pthread_mutex_lock(&s->mutex); @@ -1140,12 +1130,12 @@ static int udp_close(URLContext *h) if (s->is_multicast && (h->flags & AVIO_FLAG_READ)) udp_leave_multicast_group(s->udp_fd, (struct sockaddr *)&s->dest_addr,(struct sockaddr *)&s->local_addr_storage); -#if HAVE_PTHREAD_CANCEL +#if HAVE_THREADS if (s->thread_started) { int ret; // Cancel only read, as write has been signaled as success to the user if (h->flags & AVIO_FLAG_READ) - pthread_cancel(s->circular_buffer_thread); + s->close_req = 1; ret = pthread_join(s->circular_buffer_thread, NULL); if (ret != 0) av_log(h, AV_LOG_ERROR, "pthread_join(): %s\n", strerror(ret)); diff --git a/configure b/configure index b5bfad6..cec94c4 100755 --- a/configure +++ b/configure @@ -1934,7 +1934,6 @@ SYSTEM_FUNCS=" nanosleep PeekNamedPipe posix_memalign - pthread_cancel sched_getaffinity SetConsoleTextAttribute SetConsoleCtrlHandler @@ -5623,11 +5622,6 @@ if ! disabled pthreads && ! enabled w32threads && ! enabled os2threads; then check_code cc "pthread.h" "static pthread_mutex_t atomic_lock = PTHREAD_MUTEX_INITIALIZER" || disable pthreads fi - -if enabled pthreads; then - check_func pthread_cancel -fi - enabled pthreads && check_builtin sem_timedwait semaphore.h "sem_t *s; sem_init(s,0,0); sem_timedwait(s,0); sem_destroy(s)" diff --git a/libavformat/udp.c b/libavformat/udp.c index 3835f98..857a979 100644 --- a/libavformat/udp.c +++ b/libavformat/udp.c @@ -60,12 +60,8 @@ #define IPPROTO_UDPLITE 136 #endif -#if HAVE_PTHREAD_CANCEL -#include -#endif - -#ifndef HAVE_PTHREAD_CANCEL -#define HAVE_PTHREAD_CANCEL 0 +#if HAVE_THREADS +#include "libavutil/thread.h" #endif #ifndef IPV6_ADD_MEMBERSHIP @@ -100,7 +96,7 @@ typedef struct UDPContext { int64_t bitrate; /* number of bits to send per second */ int64_t burst_bits; int close_req; -#if HAVE_PTHREAD_CANCEL +#if HAVE_THREADS pthread_t circular_buffer_thread; pthread_mutex_t mutex; pthread_cond_t cond; @@ -495,14 +491,13 @@ static int udp_get_file_handle(URLContext *h) return s->udp_fd; } -#if HAVE_PTHREAD_CANCEL +#if HAVE_THREADS static void *circular_buffer_task_rx( void *_URLContext) { URLContext *h = _URLContext; UDPContext *s = h->priv_data; int old_cancelstate; - pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_cancelstate); pthread_mutex_lock(&s->mutex); if (ff_socket_nonblock(s->udp_fd, 0) < 0) { av_log(h, AV_LOG_ERROR, "Failed to set blocking mode"); @@ -511,14 +506,11 @@ static void *circular_buffer_task_rx( void *_URLContext) } while(1) { int len; + if (s->close_req) + goto end; pthread_mutex_unlock(&s->mutex); - /* Blocking operations are always cancellation points; - see "General Information" / "Thread Cancelation Overview" - in Single Unix. */ - pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old_cancelstate); len = recv(s->udp_fd, s->tmp+4, sizeof(s->tmp)-4, 0); - pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_cancelstate); pthread_mutex_lock(&s->mutex); if (len < 0) {