From patchwork Thu Dec 3 00:36:26 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 24327 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 4E85044B751 for ; Thu, 3 Dec 2020 02:37:04 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 37FC8689E50; Thu, 3 Dec 2020 02:37:04 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ej1-f66.google.com (mail-ej1-f66.google.com [209.85.218.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 91558689DBF for ; Thu, 3 Dec 2020 02:36:55 +0200 (EET) Received: by mail-ej1-f66.google.com with SMTP id f23so814619ejt.8 for ; Wed, 02 Dec 2020 16:36:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references:reply-to :mime-version:content-transfer-encoding; bh=SINA6xZT6yNs1+XOdlJIIxQVe/ruvcrRTioxoR/7bXo=; b=bBsQ3emrk87K5PMbPfuwiA+TPZnDaB/e9lnXz1ZZ45aIQZFHLOQHsTYdkKR0Ml5rQt EsJ2EME1me0jT6PeI0ZtfjKCPPirjRC4/jlX7TH4wcHYPIkNMaX8RuM2scqV8VyBwdlu 8r5Dv8NBaD+jkg+RO+yNmECW+kNWgn2dCMUIV1V0UeJBvn2aJOIDMkwdXjOufyAWdZYv Q9VLFOFyM1RNVzps0GIEWJ9izMEUzRotYPq7Z79wL4HsZ2fZ/RicBmkUwJWAiJbKe95f Bi9ragfiTTTYK+HkNuX37s0BlRcp7xvDpwB4Jm8m3rif19BOcg/KbJZEorYtOpCDjqZn NyuA== 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:reply-to:mime-version:content-transfer-encoding; bh=SINA6xZT6yNs1+XOdlJIIxQVe/ruvcrRTioxoR/7bXo=; b=FsLXifsYqIXURzmxhCkm5Ll2vUDfz2Mgy8IhTjX/7P4tVoHNTwFpgpFb3qLb+HwZEE vvUoQ/q7Wf2o1izFGaGXkTieDd49oIJD/G2WHrewkch7oAv+VeWG+gZuanA1P+h/nkox 20dEiKztRBoHJdypwtcHJE49+LTZ8OKqBBK0AI880PzvxBwBwc6d1J7TGGmHPCYRmTzS thtH6UElSyQ5Nz8eYdzMHd/D0w4rFi89Bzy1YqrDvP/c46DUnyJU+iz3N0k95nCqYbHX yFd0sBdjOq9c3zcIsE8AM/fURZlaSszjDVUskYcTWd0Tpy5e4lEVgdn0eSGk0mFn4JIT g+Ew== X-Gm-Message-State: AOAM5321rX15L0qMNM7W12tEVQ1x0RdV2ilBMXv+j8e9hUxMTQtzBl9N Ka/hf0PbphWGCupXqkGYT7UBdIH4k6qG7w== X-Google-Smtp-Source: ABdhPJx8gV2NO9lBDbHQOicaF031tQq0Yrfo2RNC8nI40aE29ddC+MIfFjzmH7JK5JS12sWFVX1xow== X-Received: by 2002:a17:906:c244:: with SMTP id bl4mr333921ejb.430.1606955814776; Wed, 02 Dec 2020 16:36:54 -0800 (PST) Received: from sblaptop.fritz.box (ipbcc1aa4b.dynamic.kabel-deutschland.de. [188.193.170.75]) by smtp.gmail.com with ESMTPSA id c12sm230777edw.55.2020.12.02.16.36.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Dec 2020 16:36:54 -0800 (PST) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Thu, 3 Dec 2020 01:36:26 +0100 Message-Id: <20201203003628.778278-4-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201127010249.2724610-1-andreas.rheinhardt@gmail.com> References: <20201127010249.2724610-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 221/223] avutil/cpu: Fix race condition in av_cpu_count() 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: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" av_cpu_count() intends to emit a debug message containing the number of logical cores when called the first time. The check currently works with a static volatile int; yet this does not help at all in case of concurrent accesses by multiple threads. So replace this with an atomic_int. Signed-off-by: Andreas Rheinhardt --- To test my patches I have modified avcodec_open2() to always open the desired AVCodecContext simultaneously from two different threads [1]. With this one easily runs into this issue when one uses a decoder with FF_CODEC_CAP_INIT_THREADSAFE that supports threading. [1]: https://pastebin.com/a1uEuQ81 libavutil/cpu.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/libavutil/cpu.c b/libavutil/cpu.c index 52f6b9a3bf..7b8a9045ab 100644 --- a/libavutil/cpu.c +++ b/libavutil/cpu.c @@ -274,7 +274,7 @@ int av_parse_cpu_caps(unsigned *flags, const char *s) int av_cpu_count(void) { - static volatile int printed; + static atomic_int printed = 0; int nb_cpus = 1; #if HAVE_WINRT @@ -306,10 +306,8 @@ int av_cpu_count(void) nb_cpus = sysinfo.dwNumberOfProcessors; #endif - if (!printed) { + if (!atomic_exchange_explicit(&printed, 1, memory_order_relaxed)) av_log(NULL, AV_LOG_DEBUG, "detected %d logical cores\n", nb_cpus); - printed = 1; - } return nb_cpus; }