From patchwork Wed Nov 23 19:21:56 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wan-Teh Chang X-Patchwork-Id: 1537 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.90.1 with SMTP id o1csp2914394vsb; Wed, 23 Nov 2016 11:29:13 -0800 (PST) X-Received: by 10.194.157.41 with SMTP id wj9mr4523662wjb.160.1479929353264; Wed, 23 Nov 2016 11:29:13 -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 y9si32773006wjg.132.2016.11.23.11.29.12; Wed, 23 Nov 2016 11:29:13 -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=@google.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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id D2479689A66; Wed, 23 Nov 2016 21:29:06 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pg0-f47.google.com (mail-pg0-f47.google.com [74.125.83.47]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 9AB3D680556 for ; Wed, 23 Nov 2016 21:29:00 +0200 (EET) Received: by mail-pg0-f47.google.com with SMTP id 3so9183760pgd.0 for ; Wed, 23 Nov 2016 11:29:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=from:to:subject:date:message-id; bh=Iz15TjwvU7cnWn99YfWGdu2oGEXsUdZDDYzJEp2cDKE=; b=mI8JM8xcDbSXndL5i8EBvKWRXFyQUyhzRyWMGbMlEYElYKECFtT3HhgCpjJAlc+YfV Pnx3CEW10pRHrgx6yjxt2SkdSl+69FDa8JGNQl1Pd/PhlxZ8U/+eLXAG73KwtUelblg+ K9p84zYOLBPdKKhg3UlM2YxJN2XFvFl6VcGNWxok4RP5RWC2yAHl3x3zInnxzDuZKjFG TMMMmKz3abS2XNQNTgaX5p2R6mFu4oJJ8xuVH66vExs4x7DamRkLBspuFpFWqlyVVstq 2M007VF3zFMvXsIVhUV+yvCf+86moAmlbh2BGM3O9FzN62+F3aObW6igDepcr8wxqBkP zzbQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:subject:date:message-id; bh=Iz15TjwvU7cnWn99YfWGdu2oGEXsUdZDDYzJEp2cDKE=; b=kV1G0AHtAVQSKMyHBQzqCVCZ5yRmc652HtGD1JK7xDi+UaNChdVmz3qPy5ROefdWwC tXsDIz5UkQd/YkLuCWr50xZrdZ+pIJjIeYe7yxbmYptE1bYTPOKRJ8zgdUdDNoD1q8qP 2ZqNx7JuPRLo15rXnS++KldDnuTz5ytind9SMR9oT9OqJq0S/qYH78BbmegLsuWJOUiL TdLJueVDX3GaPXvcvuzAvIGa2D7XdP37BanJkOcL8BUDjTr2Ip1k8Q5rkxhbiuzmA9kr EnSSF0924qEjDmVZBbj0WU0HdnxvPUuVpO6qKv4j7b03Q5iLV9YpoOdKP1TumYacRYXF qmyQ== X-Gm-Message-State: AKaTC01vbhX4hWFC+PJ+IusVjKjxvZ/QkiJNBhYLxuBS00B3uVQiuQaNoxZhBe23ZokK3iNj X-Received: by 10.98.20.131 with SMTP id 125mr4373911pfu.51.1479929023530; Wed, 23 Nov 2016 11:23:43 -0800 (PST) Received: from wtc-desktop.mtv.corp.google.com ([100.98.5.56]) by smtp.googlemail.com with ESMTPSA id a7sm54551870pfl.87.2016.11.23.11.23.42 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 23 Nov 2016 11:23:43 -0800 (PST) From: Wan-Teh Chang To: ffmpeg-devel@ffmpeg.org Date: Wed, 23 Nov 2016 11:21:56 -0800 Message-Id: <1479928916-29279-1-git-send-email-wtc@google.com> X-Mailer: git-send-email 2.8.0.rc3.226.g39d4020 Subject: [FFmpeg-devel] [PATCH] avutil/cpu: remove the |checked| static variable 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 MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Remove the |checked| variable because the invalid value of -1 for |flags| can be used to indicate the same condition. Also rename |flags| to |cpu_flags| because there are a local variable and a function parameter named |flags| in the same file. Add a test program libavutil/tests/cpu_init.c to check whether the one-time initialization in av_get_cpu_flags() has data races. Co-author: Dmitry Vyukov of Google Signed-off-by: Wan-Teh Chang --- libavutil/Makefile | 1 + libavutil/cpu.c | 40 ++++++++++++++------------ libavutil/tests/cpu_init.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 19 deletions(-) create mode 100644 libavutil/tests/cpu_init.c diff --git a/libavutil/Makefile b/libavutil/Makefile index 0fa90fe..732961a 100644 --- a/libavutil/Makefile +++ b/libavutil/Makefile @@ -187,6 +187,7 @@ TESTPROGS = adler32 \ camellia \ color_utils \ cpu \ + cpu_init \ crc \ des \ dict \ diff --git a/libavutil/cpu.c b/libavutil/cpu.c index f5785fc..73317c4 100644 --- a/libavutil/cpu.c +++ b/libavutil/cpu.c @@ -44,7 +44,20 @@ #include #endif -static int flags, checked; +static int cpu_flags = -1; + +static int get_cpu_flags(void) +{ + if (ARCH_AARCH64) + return ff_get_cpu_flags_aarch64(); + if (ARCH_ARM) + return ff_get_cpu_flags_arm(); + if (ARCH_PPC) + return ff_get_cpu_flags_ppc(); + if (ARCH_X86) + return ff_get_cpu_flags_x86(); + return 0; +} void av_force_cpu_flags(int arg){ if ( (arg & ( AV_CPU_FLAG_3DNOW | @@ -69,33 +82,22 @@ void av_force_cpu_flags(int arg){ arg |= AV_CPU_FLAG_MMX; } - flags = arg; - checked = arg != -1; + cpu_flags = arg; } int av_get_cpu_flags(void) { - if (checked) - return flags; - - if (ARCH_AARCH64) - flags = ff_get_cpu_flags_aarch64(); - if (ARCH_ARM) - flags = ff_get_cpu_flags_arm(); - if (ARCH_PPC) - flags = ff_get_cpu_flags_ppc(); - if (ARCH_X86) - flags = ff_get_cpu_flags_x86(); - - checked = 1; + int flags = cpu_flags; + if (flags == -1) { + flags = get_cpu_flags(); + cpu_flags = flags; + } return flags; } void av_set_cpu_flags_mask(int mask) { - checked = 0; - flags = av_get_cpu_flags() & mask; - checked = 1; + cpu_flags = get_cpu_flags() & mask; } int av_parse_cpu_flags(const char *s) diff --git a/libavutil/tests/cpu_init.c b/libavutil/tests/cpu_init.c new file mode 100644 index 0000000..5a5ecda --- /dev/null +++ b/libavutil/tests/cpu_init.c @@ -0,0 +1,72 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/* + * This test program tests whether the one-time initialization in + * av_get_cpu_flags() has data races. + */ + +#include +#include + +#include "config.h" + +#include "libavutil/cpu.h" +#include "libavutil/thread.h" + +#if HAVE_PTHREADS +static void *thread_main(void *arg) +{ + int *flags = arg; + + *flags = av_get_cpu_flags(); + return NULL; +} +#endif + + +int main(int argc, char **argv) +{ +#if HAVE_PTHREADS + int cpu_flags1; + int cpu_flags2; + int ret; + pthread_t thread1; + pthread_t thread2; + + if ((ret = pthread_create(&thread1, NULL, thread_main, &cpu_flags1))) { + fprintf(stderr, "pthread_create failed: %s.\n", strerror(ret)); + return 1; + } + if ((ret = pthread_create(&thread2, NULL, thread_main, &cpu_flags2))) { + fprintf(stderr, "pthread_create failed: %s.\n", strerror(ret)); + return 1; + } + pthread_join(thread1, NULL); + pthread_join(thread2, NULL); + + if (cpu_flags1 < 0) + return 2; + if (cpu_flags2 < 0) + return 2; + if (cpu_flags1 != cpu_flags2) + return 3; +#endif + + return 0; +}