From patchwork Wed Apr 5 15:26:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frank Plowman X-Patchwork-Id: 41000 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:4645:b0:e3:3194:9d20 with SMTP id eb5csp474971pzb; Wed, 5 Apr 2023 08:26:55 -0700 (PDT) X-Google-Smtp-Source: AKy350bJQQABBP2L8O1eMTUnt9FtTPcu3tlpOf7k2S5lqeNYdubevrOXvYP48sQ/+AK2xN1maGu/ X-Received: by 2002:a17:906:150a:b0:932:4255:5902 with SMTP id b10-20020a170906150a00b0093242555902mr3136611ejd.76.1680708415154; Wed, 05 Apr 2023 08:26:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680708415; cv=none; d=google.com; s=arc-20160816; b=vIixG6gkKOksHyBYvRXmsTc8j/wdFMSRymQkp4Toajw+nTXdAhCQU5kC3xbRjSUCxn SGN3VBfMn7n41E3FbsEznOYiqGs0+grTM6aavHPrGoTh/ituX1uDj0S5twW+BFIkRZvK JVAcVKvei+WQp2uZqrtba4pPC5iVDZrqXxL4O5PUVUYKURvJpSUHR1B8WvAnG2n6kcpt +TR1pQ1zOef3i250OvIAo8EvZ5iOCAoT57blm9bGIEkzBMKGsSzAZZHEDT2GOHipXONP 3B+8gZH1DgflZkx88fl29yi77ZmZO5V9ZLb3gg+QKopNDO76XBKWUUVT7PYbPwnnAF3i B6mg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:message-id:date:to:from :dkim-signature:delivered-to; bh=o1k5k9Flfqvm3BqL/+CgdLXAscTj8+DIrqkQTHSNGjc=; b=NGJ2BmORZyp003xeUOS6gMNyzRmBA8hQ/CY6fj1jNsoTEHyxu6MZ0rtV6VnEvpCdlo uWyb4ArS3UesM5hEB68QopXXjLwj32yujXwsBgLPmTVH8P+KuYM6NqQW+wAwV1aEsWct Tls9OUqdRR1vqf50GG5iD96ld7fq9RAc5Ff6fwVAqCw3jmfvgei+yiYu0PwDVGQ1tNKY X+Z5On3Y0tIrd2D7i9vx98nsQAn7/T71CfIFyDUCDvM4SBRbwcEoD4cZI0Ag8TxmBGCB RdA4W0Zw3Y9TWzvkXTuZR3C0NkfVGT7syCSiB9ivJkijpIqfLD9E4THOXx8fZsIzaQgj 4eYg== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@frankplowman.com header.s=mail header.b=Q9lr5Xqs; 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 sp=NONE dis=NONE) header.from=frankplowman.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id lv12-20020a170906bc8c00b0092be21ff08dsi5876175ejb.536.2023.04.05.08.26.49; Wed, 05 Apr 2023 08:26:55 -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; dkim=neutral (body hash did not verify) header.i=@frankplowman.com header.s=mail header.b=Q9lr5Xqs; 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 sp=NONE dis=NONE) header.from=frankplowman.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id A10CF68996C; Wed, 5 Apr 2023 18:26:44 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from frankplowman.com (frankplowman.com [51.89.148.29]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id BDA8B687FA6 for ; Wed, 5 Apr 2023 18:26:36 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=frankplowman.com; s=mail; h=Subject:To:From; bh=vOyBVTLyefyqlAj4gPNGQB3XHs+azyCPzGW0kFIrmVM=; b=Q9lr5XqsAirXe7yT4azTNzyNcV 4ndAbI+a2ckGeLhzXZ8Eg6pXaWrQMZr8Pn4w8XLbdTiWkn9gy+G57UfiOxFlFzWsfn1PZARTMq06W JcfeUqkn4/KL2VgNtqYQz+W2FwRvps5f1xIfLJyca6l21TS0sJP/zsuBmOzShtwX2wG8=; Received: from [149.34.153.112] (port=50761 helo=air.communityfibre.co.uk) by frankplowman.com with esmtpsa (TLS1.3) tls TLS_CHACHA20_POLY1305_SHA256 (Exim 4.96) (envelope-from ) id 1pk51v-004xMg-1X; Wed, 05 Apr 2023 17:26:36 +0200 From: Frank Plowman To: ffmpeg-devel@ffmpeg.org Date: Wed, 5 Apr 2023 16:26:29 +0100 Message-Id: <20230405152629.35740-1-post@frankplowman.com> X-Mailer: git-send-email 2.40.0 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] compat/atomics/win32: improve similarity to stdatomic.h 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 Cc: Frank Plowman Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: yw5d0hasfOO7 Some preliminary info: * The win32 atomic compatibility header is based on VLC's (c91e72ed52). This patch makes the header more in line with what it was before they got rid of this way of doing things: https://code.videolan.org/videolan/vlc/-/blob/ce150f3849cebe97bc7fc028674d3c7f8c73f64d/include/vlc_atomic.h * The Windows API does not support atomic operations on 8-bit types and only has functions for atomic operations on 16-bit types on Windows Desktop. * There is nowhere in the FFmpeg codebase which currently uses 8- or 16-bit atomic types This patch: * Makes atomic types the same size as their non-atomic counterparts. Previously, all atomic types were intptr_t to get around the lack of 8- and 16-bit atomic operations. This could lead to overreads. Now, each atomic type is the same size as its non-atomic counterpart, in line with the C11 specification. * Dispatches Windows API functions based on operand size to avoid overwrites. Now there are a variety of sizes of atomic types, the correct Windows API function must be selected based on the object's sizes. Feedback is appreciated on how best to handle the failure case in the atomic_helper macro. * Removes 8- and 16-bit types not supported by all versions of winnt.h. None of these were being used anywhere in FFmpeg. As these cannot be operated on atomically, they have been removed. Alternatives to this recommended. (PS: This is my first submitted patch, please be nice although I'm sure you will) Signed-off-by: Frank Plowman --- compat/atomics/win32/stdatomic.h | 104 +++++++++++-------------------- 1 file changed, 36 insertions(+), 68 deletions(-) diff --git a/compat/atomics/win32/stdatomic.h b/compat/atomics/win32/stdatomic.h index 28a627bfd3..a0475fda7d 100644 --- a/compat/atomics/win32/stdatomic.h +++ b/compat/atomics/win32/stdatomic.h @@ -43,42 +43,26 @@ do { \ #define atomic_is_lock_free(obj) 0 -typedef intptr_t atomic_flag; -typedef intptr_t atomic_bool; -typedef intptr_t atomic_char; -typedef intptr_t atomic_schar; -typedef intptr_t atomic_uchar; -typedef intptr_t atomic_short; -typedef intptr_t atomic_ushort; -typedef intptr_t atomic_int; -typedef intptr_t atomic_uint; -typedef intptr_t atomic_long; -typedef intptr_t atomic_ulong; -typedef intptr_t atomic_llong; -typedef intptr_t atomic_ullong; -typedef intptr_t atomic_wchar_t; -typedef intptr_t atomic_int_least8_t; -typedef intptr_t atomic_uint_least8_t; -typedef intptr_t atomic_int_least16_t; -typedef intptr_t atomic_uint_least16_t; -typedef intptr_t atomic_int_least32_t; -typedef intptr_t atomic_uint_least32_t; -typedef intptr_t atomic_int_least64_t; -typedef intptr_t atomic_uint_least64_t; -typedef intptr_t atomic_int_fast8_t; -typedef intptr_t atomic_uint_fast8_t; -typedef intptr_t atomic_int_fast16_t; -typedef intptr_t atomic_uint_fast16_t; -typedef intptr_t atomic_int_fast32_t; -typedef intptr_t atomic_uint_fast32_t; -typedef intptr_t atomic_int_fast64_t; -typedef intptr_t atomic_uint_fast64_t; -typedef intptr_t atomic_intptr_t; -typedef intptr_t atomic_uintptr_t; -typedef intptr_t atomic_size_t; -typedef intptr_t atomic_ptrdiff_t; -typedef intptr_t atomic_intmax_t; -typedef intptr_t atomic_uintmax_t; +typedef int atomic_int; +typedef unsigned int atomic_uint; +typedef long atomic_long; +typedef unsigned long atomic_ulong; +typedef long long atomic_llong; +typedef unsigned long long atomic_ullong; +typedef int_least32_t atomic_int_least32_t; +typedef uint_least32_t atomic_uint_least32_t; +typedef int_least64_t atomic_int_least64_t; +typedef uint_least64_t atomic_uint_least64_t; +typedef int_fast32_t atomic_int_fast32_t; +typedef uint_fast32_t atomic_uint_fast32_t; +typedef int_fast64_t atomic_int_fast64_t; +typedef uint_fast64_t atomic_uint_fast64_t; +typedef intptr_t atomic_intptr_t; +typedef uintptr_t atomic_uintptr_t; +typedef size_t atomic_size_t; +typedef ptrdiff_t atomic_ptrdiff_t; +typedef intmax_t atomic_intmax_t; +typedef uintmax_t atomic_uintmax_t; #define atomic_store(object, desired) \ do { \ @@ -95,20 +79,21 @@ do { \ #define atomic_load_explicit(object, order) \ atomic_load(object) +#define atomic_helper(operation, object, ...) \ + (sizeof(*object) == 4 ? \ + operation((volatile LONG *) object, __VA_ARGS__) \ + : sizeof(*object) == 8 ? \ + operation##64((volatile LONG64 *) object, __VA_ARGS__) \ + : (abort(), 0)) + #define atomic_exchange(object, desired) \ - InterlockedExchangePointer((PVOID volatile *)object, (PVOID)desired) + atomic_helper(InterlockedExchange, object, desired) #define atomic_exchange_explicit(object, desired, order) \ atomic_exchange(object, desired) -static inline int atomic_compare_exchange_strong(intptr_t *object, intptr_t *expected, - intptr_t desired) -{ - intptr_t old = *expected; - *expected = (intptr_t)InterlockedCompareExchangePointer( - (PVOID *)object, (PVOID)desired, (PVOID)old); - return *expected == old; -} +#define atomic_compare_exchange_strong(object, expected, desired) \ + atomic_helper(InterlockedCompareExchange, object, desired, *expected) #define atomic_compare_exchange_strong_explicit(object, expected, desired, success, failure) \ atomic_compare_exchange_strong(object, expected, desired) @@ -119,37 +104,20 @@ static inline int atomic_compare_exchange_strong(intptr_t *object, intptr_t *exp #define atomic_compare_exchange_weak_explicit(object, expected, desired, success, failure) \ atomic_compare_exchange_weak(object, expected, desired) -#ifdef _WIN64 -#define atomic_fetch_add(object, operand) \ - InterlockedExchangeAdd64(object, operand) - -#define atomic_fetch_sub(object, operand) \ - InterlockedExchangeAdd64(object, -(operand)) - -#define atomic_fetch_or(object, operand) \ - InterlockedOr64(object, operand) - -#define atomic_fetch_xor(object, operand) \ - InterlockedXor64(object, operand) - -#define atomic_fetch_and(object, operand) \ - InterlockedAnd64(object, operand) -#else #define atomic_fetch_add(object, operand) \ - InterlockedExchangeAdd(object, operand) + atomic_helper(InterlockedExchangeAdd, object, operand) -#define atomic_fetch_sub(object, operand) \ - InterlockedExchangeAdd(object, -(operand)) +#define atomic_fetch_sub(object, operand) \ + atomic_fetch_add(object, -(operand)) #define atomic_fetch_or(object, operand) \ - InterlockedOr(object, operand) + atomic_helper(InterlockedOr, object, operand) #define atomic_fetch_xor(object, operand) \ - InterlockedXor(object, operand) + atomic_helper(InterlockedXor, object, operand) #define atomic_fetch_and(object, operand) \ - InterlockedAnd(object, operand) -#endif /* _WIN64 */ + atomic_helper(InterlockedAnd, object, operand) #define atomic_fetch_add_explicit(object, operand, order) \ atomic_fetch_add(object, operand)