From patchwork Sat Jun 5 14:27:40 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 28088 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:b214:0:0:0:0:0 with SMTP id b20csp2010838iof; Sat, 5 Jun 2021 07:28:01 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyXAt7k5aGplXUGF9iES8QvRAWefD/i6TCpBIx728A5N7kHGR7B1lOmT8j0MIUkmjF83d4z X-Received: by 2002:a17:907:c0d:: with SMTP id ga13mr4796310ejc.325.1622903281037; Sat, 05 Jun 2021 07:28:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1622903281; cv=none; d=google.com; s=arc-20160816; b=fAHAr6szKga3rerkHQ61pfwZdHCgEV5IWUj9gO5LRggovq4hx5nqhor/7MXFx/G5MU MWZyp11JqrtmDt2WZnd8nJJL5X33tSaFB34x3xjIbKWG8hh/ReKeIWrshyLZcD15evRt HSRogcEpy01Q9DWxXIwIYI5TpsZ1VAlDddFeEzLxwoeCO/8HXW5pkMydW84c2VpbOMO/ rWn+XjJ0nYiS722Ux+FSpBpHAAzB1sW63RqUTdarya0FyeupFWs6slSCWW4fb8T0zNun o2tHaoOl6ENtngO2yqhBrVNVnzreu2iXKid7C2eQHgz5CZWna0Y5qd5RmWut6EHSSdjA oIvA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:message-id:date:to:from :delivered-to; bh=3KM8wlX7nbh9/DR5USJ6uaj+EgupGS0NCWakm7VYciQ=; b=FXuX7j24UHcTXTK43Wbou2nTnCsaalc+ngydTh+zZD7TOL4S5k/Xkxb9I4t2BEKTOm JTmVevBDm6ic+e522JBcv3iaFAcf+dGFIrQg/41rjGV/NCh7/gieNB4HgYhuxZTq3l6G kwFiEyXmYBz6EQf3ckKiXE9mPCwGPnPqFV5EGkXzCWGzYEPJ0TOi3FlxSugZoza4d5lA 6ApAX7hFxQ8rV+df0SQLG9LPJzBcd1I83B8X2s3ibVQK6XiKHhrll4YFwjWpMOzn2b3Q 6Kr4ReeC5c9vaNuyZKtNNiP89CSYa3LVyBBvp6QlG49e9z7PbNgXZQiq8eQS8HkKOy1A jo7g== ARC-Authentication-Results: i=1; mx.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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id bi15si8707340edb.459.2021.06.05.07.28.00; Sat, 05 Jun 2021 07:28:01 -0700 (PDT) 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; 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 ACAB968A4A3; Sat, 5 Jun 2021 17:27:57 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id C086968A369 for ; Sat, 5 Jun 2021 17:27:50 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 51751240476 for ; Sat, 5 Jun 2021 16:27:50 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id VUk9yNNJKjaP for ; Sat, 5 Jun 2021 16:27:45 +0200 (CEST) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id 072A224042A for ; Sat, 5 Jun 2021 16:27:45 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id 0906B3A125B; Sat, 5 Jun 2021 16:27:45 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Sat, 5 Jun 2021 16:27:40 +0200 Message-Id: <20210605142740.7996-1-anton@khirnov.net> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] Stop using _explicit atomic operations where not necessary. X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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" X-TUID: iOtqersBBFLx Memory ordering constraints other than the default (sequentially consistent) can behave in very unintuitive and unexpected ways, and so should be avoided unless there is a strong (typically performance) reason for using them. This holds especially for memory_order_relaxed, which imposes no ordering constraints. --- libavformat/fifo.c | 8 ++++---- libavutil/cpu.c | 8 ++++---- libavutil/mem.c | 10 +++++----- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/libavformat/fifo.c b/libavformat/fifo.c index 50656f78b7..2ac14207b8 100644 --- a/libavformat/fifo.c +++ b/libavformat/fifo.c @@ -185,7 +185,7 @@ static int fifo_thread_write_packet(FifoThreadContext *ctx, AVPacket *pkt) int ret, s_idx; if (fifo->timeshift && pkt->dts != AV_NOPTS_VALUE) - atomic_fetch_sub_explicit(&fifo->queue_duration, next_duration(avf, pkt, &ctx->last_received_dts), memory_order_relaxed); + atomic_fetch_sub(&fifo->queue_duration, next_duration(avf, pkt, &ctx->last_received_dts)); if (ctx->drop_until_keyframe) { if (pkt->flags & AV_PKT_FLAG_KEY) { @@ -454,7 +454,7 @@ static void *fifo_consumer_thread(void *data) av_log(avf, AV_LOG_INFO, "FIFO queue flushed\n"); if (fifo->timeshift) - while (atomic_load_explicit(&fifo->queue_duration, memory_order_relaxed) < fifo->timeshift) + while (atomic_load(&fifo->queue_duration) < fifo->timeshift) av_usleep(10000); ret = av_thread_message_queue_recv(queue, &msg, 0); @@ -594,7 +594,7 @@ static int fifo_write_packet(AVFormatContext *avf, AVPacket *pkt) } if (fifo->timeshift && pkt && pkt->dts != AV_NOPTS_VALUE) - atomic_fetch_add_explicit(&fifo->queue_duration, next_duration(avf, pkt, &fifo->last_sent_dts), memory_order_relaxed); + atomic_fetch_add(&fifo->queue_duration, next_duration(avf, pkt, &fifo->last_sent_dts)); return ret; fail: @@ -621,7 +621,7 @@ static int fifo_write_trailer(AVFormatContext *avf) } else { now += delay; } - atomic_fetch_add_explicit(&fifo->queue_duration, delay, memory_order_relaxed); + atomic_fetch_add(&fifo->queue_duration, delay); elapsed += delay; if (elapsed > fifo->timeshift) break; diff --git a/libavutil/cpu.c b/libavutil/cpu.c index 8960415d00..8a6af81ae4 100644 --- a/libavutil/cpu.c +++ b/libavutil/cpu.c @@ -89,15 +89,15 @@ void av_force_cpu_flags(int arg){ arg |= AV_CPU_FLAG_MMX; } - atomic_store_explicit(&cpu_flags, arg, memory_order_relaxed); + atomic_store(&cpu_flags, arg); } int av_get_cpu_flags(void) { - int flags = atomic_load_explicit(&cpu_flags, memory_order_relaxed); + int flags = atomic_load(&cpu_flags); if (flags == -1) { flags = get_cpu_flags(); - atomic_store_explicit(&cpu_flags, flags, memory_order_relaxed); + atomic_store(&cpu_flags, flags); } return flags; } @@ -221,7 +221,7 @@ int av_cpu_count(void) nb_cpus = sysinfo.dwNumberOfProcessors; #endif - if (!atomic_exchange_explicit(&printed, 1, memory_order_relaxed)) + if (!atomic_exchange(&printed, 1)) av_log(NULL, AV_LOG_DEBUG, "detected %d logical cores\n", nb_cpus); return nb_cpus; diff --git a/libavutil/mem.c b/libavutil/mem.c index a52d33d4a6..c216c0314f 100644 --- a/libavutil/mem.c +++ b/libavutil/mem.c @@ -72,14 +72,14 @@ void free(void *ptr); static atomic_size_t max_alloc_size = ATOMIC_VAR_INIT(INT_MAX); void av_max_alloc(size_t max){ - atomic_store_explicit(&max_alloc_size, max, memory_order_relaxed); + atomic_store(&max_alloc_size, max); } void *av_malloc(size_t size) { void *ptr = NULL; - if (size > atomic_load_explicit(&max_alloc_size, memory_order_relaxed)) + if (size > atomic_load(&max_alloc_size)) return NULL; #if HAVE_POSIX_MEMALIGN @@ -135,7 +135,7 @@ void *av_malloc(size_t size) void *av_realloc(void *ptr, size_t size) { void *ret; - if (size > atomic_load_explicit(&max_alloc_size, memory_order_relaxed)) + if (size > atomic_load(&max_alloc_size)) return NULL; #if HAVE_ALIGNED_MALLOC @@ -489,7 +489,7 @@ void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size) if (min_size <= *size) return ptr; - max_size = atomic_load_explicit(&max_alloc_size, memory_order_relaxed); + max_size = atomic_load(&max_alloc_size); if (min_size > max_size) { *size = 0; @@ -521,7 +521,7 @@ static inline void fast_malloc(void *ptr, unsigned int *size, size_t min_size, i return; } - max_size = atomic_load_explicit(&max_alloc_size, memory_order_relaxed); + max_size = atomic_load(&max_alloc_size); if (min_size > max_size) { av_freep(ptr);