From patchwork Mon Oct 4 14:44:47 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 30898 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:6506:0:0:0:0:0 with SMTP id z6csp4390916iob; Mon, 4 Oct 2021 07:45:19 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxCgznHfKEof3AYeuqSyQlS2PxXn6k+AwzqGohk4b/59uCp+pIJ0Woe3TTlqugiYVmtUtNZ X-Received: by 2002:a50:eb9a:: with SMTP id y26mr8653758edr.186.1633358719790; Mon, 04 Oct 2021 07:45:19 -0700 (PDT) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id l8si17255162edb.67.2021.10.04.07.45.19; Mon, 04 Oct 2021 07:45:19 -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=@outlook.com header.s=selector1 header.b=Oy++lJHv; arc=fail (body hash mismatch); 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=QUARANTINE dis=NONE) header.from=outlook.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id EE47868A940; Mon, 4 Oct 2021 17:45:09 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from EUR02-AM5-obe.outbound.protection.outlook.com (mail-oln040092067080.outbound.protection.outlook.com [40.92.67.80]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id A9C1B68A8D9 for ; Mon, 4 Oct 2021 17:45:02 +0300 (EEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=FGuhUFW65omC8+qjD4/TfOcW27BajnvgS3rHdZ+kRIg7A1aF9AHykGyV1lYKBURhorTRNhIncvdskcfXlgLz21EQzD2yFANv43be24I/eD7s0BOp0xD56pN50/GV/6FoXBSeWF9QV+kjFzXXcqp6LD5IC3DPqZo3x0N219CVeRsvuigT05+caqbGZZQeJaEo6zac+5mOZjXzjccLHoNu3zFUmgSj8qfg7GzXtb+AF+vYz1YdCUaGA/WlqJoxd2t0V5Uu03nU1IoEbxBnKXmPMKZs9jgCNfvIGM2Vhc8TLsg/LYNu/w8H/vUv/ngextUeL8KSjICc9RK/PB1lx76Rkg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=+wKSw3PO+tsDRcz/41Y/PVsDmn7BsJb5XZqmTfnqi9Y=; b=Amsz642ugqR1sEms5jOLKJU5QDV/AUhQMO/YHnpATz8wzRiujiEzgc/pE9t+Df7kq8JCdvozKXbLRs4sF6yiZodHGEB6D397zmuz8JpSmWlJP4O9hebL5wpkgDIqYo+db4TRymNCzUssLzqe8+FpEx0hpHXSOtmqpUXD0Y/BdTj3eENFXpvkehJqkGFtJ7wXw/e9z+U0y1ltbdiqupabDHwr6N7X5hY58UBGoO4BLuWRSYirLjiElgBJ20fbtB6EoncOKhf9ccHcRsVfP+/FUaiysbxtjLW2o/Kb2gVZGp1zrUFhW5mUuyEF+Eq0jQjL1OX621YaaWn5TanLUDmwew== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=+wKSw3PO+tsDRcz/41Y/PVsDmn7BsJb5XZqmTfnqi9Y=; b=Oy++lJHvoyI2z4tavPEnuU5Tb4RVRuc8dk1+ji5vJJoMnqlyKHnhF1/4p691f9dkI2EckBOHM5snObccjDUx3wEasJV0zwoVH6z71xcYmyTrMygloikI/21+NXMfEXU6uqbjrtOzfb4wsjH9ecM1xN5RZ6xE356xG3Y+XrxaMgbFbPjCbQmrns0gIpzmGREMzGbAsVKmu8kxOAC2AmxLMMRPHsr1stx/6uY9mTwNmHreY34GbNiE24lUgUlcABqrU9eV/AXKs6CPI5QZizjNirch0DGNjolW9YCzS17ruQIG1MShdFeBRRZV9dYrqjBXA3ZXv4l+dPJqHjTvdzC2zA== Received: from AM7PR03MB6660.eurprd03.prod.outlook.com (2603:10a6:20b:1c1::22) by AM6PR03MB5379.eurprd03.prod.outlook.com (2603:10a6:20b:c1::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4566.19; Mon, 4 Oct 2021 14:44:58 +0000 Received: from AM7PR03MB6660.eurprd03.prod.outlook.com ([fe80::700f:d70b:3bb8:4d51]) by AM7PR03MB6660.eurprd03.prod.outlook.com ([fe80::700f:d70b:3bb8:4d51%7]) with mapi id 15.20.4566.022; Mon, 4 Oct 2021 14:44:58 +0000 From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Mon, 4 Oct 2021 16:44:47 +0200 Message-ID: X-Mailer: git-send-email 2.30.2 In-Reply-To: References: X-TMN: [vBUOX1IFIEgFbkvh1Omko0TZfy8hJpDl] X-ClientProxiedBy: AM6P191CA0044.EURP191.PROD.OUTLOOK.COM (2603:10a6:209:7f::21) To AM7PR03MB6660.eurprd03.prod.outlook.com (2603:10a6:20b:1c1::22) X-Microsoft-Original-Message-ID: <20211004144447.3984261-2-andreas.rheinhardt@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sblaptop.fritz.box (188.192.142.38) by AM6P191CA0044.EURP191.PROD.OUTLOOK.COM (2603:10a6:209:7f::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4566.15 via Frontend Transport; Mon, 4 Oct 2021 14:44:58 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 909a3d8f-a090-4928-6040-08d9874589b0 X-MS-Exchange-SLBlob-MailProps: +LiGfBxqLEvCnas31JGWNUANaS7a1JYubGX+jHl3kVI0FBquqVmJfMpHkkRWGC0UoB7MRb0nrbvzLi5Eq/nXMHeSprt4himlGD0e5eP+vFhsNxNovVH3ZZz0cV0sPfqkp/UODqmlaUCR/cTnUlQ1nmYGrwiBE532kXsrneM5pqyQs4QJGXaXmDjhV/kv2xNDiuDuQ4C2e20XCLH68auPUtNbGmWmvzPE+pRLcXzJKX/LPrnmjB7Zuajz/FzMjqoP7BC4rMK04ZEKedzexcWTpc+m3RNTX6dcRmenoCKGisDpeGFzHtnvdC4x27gKpi+gPeGpFnr/8gL6Lgb4QtggdC17LzwLCMV2q70fOWBnrTcQm7NkDEICJsb6JI+7qj5NoOVn9053b3J31FOe5qho1R17nnYMiZTNnixAaawFpPXVJuOyl3Auq/yIompc1TINnQdz+TUDKE00lKSRwMbOYDhkXF5QlVvd0UedYgODyvpvo50ZgIUjDY3iSmuZfsGyAQAEIAKAlodVWTvTfL4OX4Wc380n9AXbIBftlE8XRWVuGtvvtQO0LKogBX9UZR/vDTeIQ92eJahya1ypTJ7T8luF+30nZROQaKSOxDiel4CF1ponvclqB0lV6bojWCx1zRBcU+vjiLIxuoihHkoydivQO/IvY4TYkG34mIxBC7lBxcgCEmMONHuKMbp9MaW0xO2Hsuv73/s29sxhcH2usByPGrDrbWmt1C1GYlxsTrbzaEEaQuav9cT5aZX6/IERoxhkIBejfinCAVqIwK2rxKV7LOwPnWg0 X-MS-TrafficTypeDiagnostic: AM6PR03MB5379: X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: DWuTGM3MJ7QTJXB78dQWIl7X53GvpSZMQirCucYA0vfhM9H9MbEiiCA0BaFg9Z715XPiQtKb1h63FhBMy7p93e2wMSPk6jI72rkv5LSxfF8rvpkydf3FpvIB3WOqOBvyPKE94eCkWln/Qh0KQgeg6BWwF0FEjTnwWJnx6KcM7+FEYQUYaw3eFDDYwKNKPecIebsLScrxGZwDVNnP0ggP71a9laB8HJIkSsucAdQF8hN99VH/aKz2+4aKOIruvRDy3OKa4I18BuBM5y3a7F1/hSPafSSgR1yPZtlPLOz8CKP/o4L3T7AjsMvwcvP42eTWsmkC1pNDF2jBS3ina2pMD4FqJ9e/FTMqWNDg6ofR7nII4yd6eIfZV9Ryyq2VfQip7fZZyL61f87YQWNQlSCysG2YDNCICh0ItwbRrj0/G91iPXevgbCsPfDd87U16t/L X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: P2A8Jj7teIyRK05brErcRvcNp3uQOzQUPY/vEdBF1oO2WP9X9fDx++gosjtYvPGqFIRL/xvlzPGS7hbL4vi76TnJ5hqUguCrYImbeVqHwu4mKJVfepm8ODcZZTpjTucEouTf2RpW6pZINcWpxpvlWg== X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 909a3d8f-a090-4928-6040-08d9874589b0 X-MS-Exchange-CrossTenant-AuthSource: AM7PR03MB6660.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 Oct 2021 14:44:58.4446 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM6PR03MB5379 Subject: [FFmpeg-devel] [PATCH 3/3] avutil/buffer: Don't use atomics for AVBufferPool.refcount 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: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: 3oDb7Egz0rNQ The AVBufferPool is already guarded by a mutex, so one can just move use said mutex to also protect modifying the refcount. Doing so yields a minor decrease in complexity. Signed-off-by: Andreas Rheinhardt --- Currently freeing the entries is serialized; the API documentation does not force us to do so ("is thread-safe as long as either the default alloc callback is used, or the user-supplied one is thread-safe"). I could easily implement unserializing it if desired, yet it would be incompatible with this patch. libavutil/buffer.c | 17 ++++++++++------- libavutil/buffer_internal.h | 2 +- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/libavutil/buffer.c b/libavutil/buffer.c index e4562a79b1..2335a99e9f 100644 --- a/libavutil/buffer.c +++ b/libavutil/buffer.c @@ -272,7 +272,7 @@ AVBufferPool *av_buffer_pool_init2(size_t size, void *opaque, pool->alloc = av_buffer_alloc; // fallback pool->pool_free = pool_free; - atomic_init(&pool->refcount, 1); + pool->refcount = 1; return pool; } @@ -288,7 +288,7 @@ AVBufferPool *av_buffer_pool_init(size_t size, AVBufferRef* (*alloc)(size_t size pool->size = size; pool->alloc = alloc ? alloc : av_buffer_alloc; - atomic_init(&pool->refcount, 1); + pool->refcount = 1; return pool; } @@ -322,6 +322,7 @@ static void buffer_pool_free(AVBufferPool *pool) void av_buffer_pool_uninit(AVBufferPool **ppool) { AVBufferPool *pool; + unsigned refcount; if (!ppool || !*ppool) return; @@ -330,9 +331,10 @@ void av_buffer_pool_uninit(AVBufferPool **ppool) ff_mutex_lock(&pool->mutex); buffer_pool_flush(pool); + refcount = --pool->refcount; ff_mutex_unlock(&pool->mutex); - if (atomic_fetch_sub_explicit(&pool->refcount, 1, memory_order_acq_rel) == 1) + if (!refcount) buffer_pool_free(pool); } @@ -340,13 +342,15 @@ static void pool_release_buffer(void *opaque, uint8_t *data) { BufferPoolEntry *buf = opaque; AVBufferPool *pool = buf->pool; + unsigned refcount; ff_mutex_lock(&pool->mutex); buf->next = pool->pool; pool->pool = buf; + refcount = --pool->refcount; ff_mutex_unlock(&pool->mutex); - if (atomic_fetch_sub_explicit(&pool->refcount, 1, memory_order_acq_rel) == 1) + if (!refcount) buffer_pool_free(pool); } @@ -400,10 +404,9 @@ AVBufferRef *av_buffer_pool_get(AVBufferPool *pool) } else { ret = pool_alloc_buffer(pool); } - ff_mutex_unlock(&pool->mutex); - if (ret) - atomic_fetch_add_explicit(&pool->refcount, 1, memory_order_relaxed); + pool->refcount++; + ff_mutex_unlock(&pool->mutex); return ret; } diff --git a/libavutil/buffer_internal.h b/libavutil/buffer_internal.h index bdff1b5b32..a726e5e865 100644 --- a/libavutil/buffer_internal.h +++ b/libavutil/buffer_internal.h @@ -99,7 +99,7 @@ struct AVBufferPool { * buffers have been released, then it's safe to free the pool and all * the buffers in it. */ - atomic_uint refcount; + unsigned refcount; size_t size; void *opaque;