From patchwork Sat Dec 23 23:51:20 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: wm4 X-Patchwork-Id: 6940 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.2.79.195 with SMTP id r64csp4096812jad; Sat, 23 Dec 2017 15:52:05 -0800 (PST) X-Google-Smtp-Source: ACJfBouWDMwa1ShDKlWI+IGvH73kQq3dz5X20ESwOobTRyp1okqNSO632VnCCCn8UUDn2W6dyqvz X-Received: by 10.28.220.215 with SMTP id t206mr15357595wmg.75.1514073125835; Sat, 23 Dec 2017 15:52:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1514073125; cv=none; d=google.com; s=arc-20160816; b=QVRB/6kAksClxZjfrtaLxYXJNDuh4T2oslcfU2BiOhDEx7olVvGj21APeLT+a4q6x4 8maiFKMK+Cz3Lo7pLPWhDl5TS1GjekYBoCCfmPa2TEBCVHv5tDeNGYMDg8zxJjeUTT21 9mSyeXoY+miRBiClI5xzjKVrXSIHQxFS+gEDAJKrFORTixXTP4VU/2vJ746YGeUIR5of IjEtlbOGf/vCtqitk3FopOHKQH6iI6G1mM0a4krGJT7/aa69jga2yWxaJSotqEhmb7I8 Z6LO0Y/juNCfxgTCI4auMd1U15p4fe1M9+EVEY6GULfJu3tCTjaq4VmUpr9EoqPkF41u RAhA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:references:in-reply-to:message-id:date :to:from:dkim-signature:delivered-to:arc-authentication-results; bh=2lEmkxSg+mkm+9n2Q+F7CcwtHVNPcuqg3Mzw8duNoRg=; b=tfNnjF0x/3DAHX99Eo+eS530MG1jEqnhXp4vnB54a/fOiTHUXDj3WEh/uN+Rg8iFnb YlYjt/L4c/Qj3MtT/5Hb8o0xWHpcCmJLmg63S0yKabwVMLCgFdQJaSZkwTGHp77OHbGt Pg6hYrzpMnSz/+5X26AVw6B6yfUyYnftZgcQ/Ud0GI2Q85G9qtxYhTeEUJ2bM9lowHwj afdhX6NOz21anpZZ/Py8fL1FSmyI6qSds7H3089vymBZyobYD47Cc9cd387D3xtilBO0 1034KNwENLRSa+t5tOdPFovJcICPSFlQbU0Mr2mHyjl01NBpcoZ9W5cm2xbS7/Sh2HSQ zQ5A== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@googlemail.com header.s=20161025 header.b=kw7ZyGob; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=googlemail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id s127si8355768wmf.89.2017.12.23.15.52.05; Sat, 23 Dec 2017 15:52:05 -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=@googlemail.com header.s=20161025 header.b=kw7ZyGob; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=googlemail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 67BDC6883D2; Sun, 24 Dec 2017 01:51:22 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm0-f67.google.com (mail-wm0-f67.google.com [74.125.82.67]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id E5A2768836C for ; Sun, 24 Dec 2017 01:51:13 +0200 (EET) Received: by mail-wm0-f67.google.com with SMTP id f140so27371205wmd.2 for ; Sat, 23 Dec 2017 15:51:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=E3Ra57IlhSj4aIICnSBis5bypvoaQkByFaZBFg1oTaw=; b=kw7ZyGobZBoIAJMGn7dMyW+lULDDPywxZu/dT7e4xhQNeOIbNkghzBDduytuSsz7cp dS4v0NmTtiB6cj6EpwcAOoPaO8OzRpXBpwxeev8eoiCLGrZQSXfufsjE1Q1LxZguGRp6 mpq7N97Q07Aqq9LlwuPO8+hMPHimprXfgd6mLVI+H/pYMRK2X+pZ9ImkKVUMsal/CwbN 87wJjMtRjtiIy2ADSGkbnGtcgA/4u0qeXNXcUDmb51MavHEbUpGkE42Ag74d17UWomlc lUN7ul4+jqvbUbeVLmPf4rPnWnWnPu4YV43z7cDdEOi/whcu1XGPXSC3KZNfk2x8+xKf Eu6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=E3Ra57IlhSj4aIICnSBis5bypvoaQkByFaZBFg1oTaw=; b=SvT0200rvSrgQAQG+gaEVIC4dPxZiAi7fFpW/7v4dwgisoJdTmGh9hxwjHKfwnt9NY XhtZT8J4eaJ38NHFnsjR8Je5LrlXGeKx0Omg2kmJiT+2ibRurriVUXmGi5Gus0aB27HK /uY2WyIMAcI4Vuz/wCu6TZvHlzUJ7wKygE5qIp/83GZUUXTDkDKbSQOvOyy7x6eE4Fin xfbycwDcxybESycYBOXi+BMkU+Q07Y8mbXlPtQ1soOyMHFEo/snWigZlE5LrLU7Lp6Kp ZMFlxhR46LZka1PPenPQJ7hdJ7hg9J9r1Ihb7MEszAu9kC0k+dJw+Ij7dswyRNdS2TUf Tdyw== X-Gm-Message-State: AKGB3mL0uiprZRLWLhhJMM1924jelQbv1YHhYXt7wuk08cPpgSHlxGK2 ZFHzqh7aB/Kfz2TtgyaWqzWNKw== X-Received: by 10.28.230.208 with SMTP id e77mr16576213wmi.86.1514073085670; Sat, 23 Dec 2017 15:51:25 -0800 (PST) Received: from debian.speedport.ip (p2003006CCD4EDC353C06725800124D49.dip0.t-ipconnect.de. [2003:6c:cd4e:dc35:3c06:7258:12:4d49]) by smtp.googlemail.com with ESMTPSA id i66sm7743406wmf.3.2017.12.23.15.51.24 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 23 Dec 2017 15:51:24 -0800 (PST) From: wm4 To: ffmpeg-devel@ffmpeg.org Date: Sun, 24 Dec 2017 00:51:20 +0100 Message-Id: <20171223235120.31578-5-nfxjfg@googlemail.com> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20171223235120.31578-1-nfxjfg@googlemail.com> References: <20171221222224.18577-1-nfxjfg@googlemail.com> <20171223235120.31578-1-nfxjfg@googlemail.com> Subject: [FFmpeg-devel] [PATCH v3 5/5] lavc: remove complex debug code around avcodec init locking 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 Cc: wm4 MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" This is just a lot of complicated and confusing code that had no purpose anymore. Also, the functions return values were checked only sometimes. Locking shouldn't fail anyway, so remove the return values. Barely any other pthread lock calls check the return value (including more important code that is more likely to fail horribly if locking fails). It could be argued that it might be helpful in some debugging situations, or when the user built FFmpeg without thread support against all good advice. But there are dummy atomics too, so the atomic check won't help with ensuring correctness absolutely. You gain very little. Also, for debugging, you can just raise the ASSERT_LEVEL, and then libavutil/thread.h will redefine the locking functions to explicitly check the return values. --- libavcodec/internal.h | 4 ---- libavcodec/utils.c | 55 +++++++++++++-------------------------------------- 2 files changed, 14 insertions(+), 45 deletions(-) diff --git a/libavcodec/internal.h b/libavcodec/internal.h index bf58f36ad3..1c901a7cbc 100644 --- a/libavcodec/internal.h +++ b/libavcodec/internal.h @@ -241,10 +241,6 @@ int ff_init_buffer_info(AVCodecContext *s, AVFrame *frame); void ff_color_frame(AVFrame *frame, const int color[4]); -extern volatile int ff_avcodec_locked; -int ff_lock_avcodec(AVCodecContext *log_ctx, const AVCodec *codec); -int ff_unlock_avcodec(const AVCodec *codec); - /** * Maximum size in bytes of extradata. * This value was chosen such that every bit of the buffer is diff --git a/libavcodec/utils.c b/libavcodec/utils.c index 9c631c4fb0..dfbfe98d63 100644 --- a/libavcodec/utils.c +++ b/libavcodec/utils.c @@ -67,8 +67,6 @@ #include "libavutil/ffversion.h" const char av_codec_ffversion[] = "FFmpeg version " FFMPEG_VERSION; -volatile int ff_avcodec_locked; -static atomic_int entangled_thread_counter = ATOMIC_VAR_INIT(0); static AVMutex codec_mutex = AV_MUTEX_INITIALIZER; void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size) @@ -550,6 +548,19 @@ static int64_t get_bit_rate(AVCodecContext *ctx) return bit_rate; } + +static void ff_lock_avcodec(AVCodecContext *log_ctx, const AVCodec *codec) +{ + if (!(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE) && codec->init) + ff_mutex_lock(&codec_mutex); +} + +static void ff_unlock_avcodec(const AVCodec *codec) +{ + if (!(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE) && codec->init) + ff_mutex_unlock(&codec_mutex); +} + int attribute_align_arg ff_codec_open2_recursive(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options) { int ret = 0; @@ -589,9 +600,7 @@ int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *code if (options) av_dict_copy(&tmp, *options, 0); - ret = ff_lock_avcodec(avctx, codec); - if (ret < 0) - return ret; + ff_lock_avcodec(avctx, codec); avctx->internal = av_mallocz(sizeof(AVCodecInternal)); if (!avctx->internal) { @@ -1867,42 +1876,6 @@ int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op)) } #endif -int ff_lock_avcodec(AVCodecContext *log_ctx, const AVCodec *codec) -{ - if (codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE || !codec->init) - return 0; - - if (ff_mutex_lock(&codec_mutex)) - return -1; - - if (atomic_fetch_add(&entangled_thread_counter, 1)) { - av_log(log_ctx, AV_LOG_ERROR, - "Insufficient thread locking. At least %d threads are " - "calling avcodec_open2() at the same time right now.\n", - atomic_load(&entangled_thread_counter)); - ff_avcodec_locked = 1; - ff_unlock_avcodec(codec); - return AVERROR(EINVAL); - } - av_assert0(!ff_avcodec_locked); - ff_avcodec_locked = 1; - return 0; -} - -int ff_unlock_avcodec(const AVCodec *codec) -{ - if (codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE || !codec->init) - return 0; - - av_assert0(ff_avcodec_locked); - ff_avcodec_locked = 0; - atomic_fetch_add(&entangled_thread_counter, -1); - if (ff_mutex_unlock(&codec_mutex)) - return -1; - - return 0; -} - unsigned int avpriv_toupper4(unsigned int x) { return av_toupper(x & 0xFF) +