From patchwork Thu Sep 2 15:41:16 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 29968 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6602:2a4a:0:0:0:0 with SMTP id k10csp399869iov; Thu, 2 Sep 2021 08:41:57 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyyVqxubZwx0Sx3Er0kA53qtpWnlU2avYdDiFy2i0kmkZMfof0ljLh2h4w/wSN89ArjutWV X-Received: by 2002:a05:6402:2032:: with SMTP id ay18mr4161204edb.364.1630597316846; Thu, 02 Sep 2021 08:41:56 -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 d16si2217731ejm.114.2021.09.02.08.41.56; Thu, 02 Sep 2021 08:41:56 -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=ZD1ZTZ9u; 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 AC65968A5E1; Thu, 2 Sep 2021 18:41:52 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from EUR03-AM5-obe.outbound.protection.outlook.com (mail-oln040092070098.outbound.protection.outlook.com [40.92.70.98]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id B63226809C3 for ; Thu, 2 Sep 2021 18:41:45 +0300 (EEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=JjLmk/kFNtY7LYqKI4q7/PD4RPYzny2PutyhxKGtcCXx9SpqMjeC7xy8gvdPuQY9Xvmk6l/DfWttmFMEbrUvvm7+ESG6/V9ifdPN3jp/rQdrjyJKM3bqYG8e2P2ot7QWbM6MU1fp5AZrBFI7juyt4m6UhbetFaifFY2CgkGRf4PZbIoNZ1NKWMS+3B8/J/7KQy02iDzrL9NCPIYb5DWORwsGVaYkVb6E2KAKJw374wtConrL6xjBjD1Bd+g1d1cB/Pmv8dgLgWgvWI1J3qV8hMByj5wJDvD0KUyHDTk/rcskPl5H4rkufaka1NBPezMndI/MwhvZoS+bOc6brL82uQ== 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-SenderADCheck; bh=uHQKls61rCNgWBBXhEW/u+7jpGyfPTMMu+i7wwj7WxU=; b=Ec46HtIAsQCw7LsOf1do92qrzAnEhn/TPZFX/xXkQkXj6Isdmnbw7vZcPxnTdorZPZSKAGQvfb5ovEvzkWrt+P5lXdYUeJGccUcXzM66diaYUKR27HBGeSYwJmoHfVttkrqcoyPt/eih8Y2NXIyxWIh2CPJSEOpvpu2/8XInKhx/qgSOODKQ7wPFlnMdWkQG7ldeIqB/lzK0PSq3BUy7lGTwPH1hw5KRKAneAuahOo8VS3237eK6Y6uQWIRiwEOKwrSEVR4zr14QJImGClH/T3/eWAmyVK3VizviSVmVeerSKSQNmkYG5DJRwILyJqbTiY1F2XRuhkAnSb4pvubLiw== 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=uHQKls61rCNgWBBXhEW/u+7jpGyfPTMMu+i7wwj7WxU=; b=ZD1ZTZ9uokfCZ28QGHhSaIbHhi1iAAFUr8KPQ0BIAn0Zh1EtCWiKk8H3w8uvzVoOJRv2JspLO/Z6M3eGLqbtNQbPxFu7kNFS+UzdB0yYOwHkmTmLKGfgGXN0nnJYbZm3muNu+96LPTdZ8z6AquCl/VUEv/5N3oGz/BDqvGtu30uDNqTQ2u0DyWWjTSCDfee2LbQyr0aNFaUMj27WKpARaMCoxLfvBvZeyYlvSAqRPJlwz+FISXX/Z4yYOWiLB0IrSQT3UmyiYV8l+gT4z4dAGJm7pQpVNOsRpzjoTaBqMYNenOLY8OO0WyuQ7BGXh5iuH1wrvYN9sVnQQ3xWgfa1zg== Received: from AM7PR03MB6660.eurprd03.prod.outlook.com (2603:10a6:20b:1c1::22) by AM7PR03MB6532.eurprd03.prod.outlook.com (2603:10a6:20b:1c3::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4457.17; Thu, 2 Sep 2021 15:41:38 +0000 Received: from AM7PR03MB6660.eurprd03.prod.outlook.com ([fe80::f8b5:1ef2:89b2:f8c5]) by AM7PR03MB6660.eurprd03.prod.outlook.com ([fe80::f8b5:1ef2:89b2:f8c5%7]) with mapi id 15.20.4457.024; Thu, 2 Sep 2021 15:41:38 +0000 From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Thu, 2 Sep 2021 17:41:16 +0200 Message-ID: X-Mailer: git-send-email 2.30.2 In-Reply-To: References: X-TMN: [CTCTzC1TPmetOJlL0jyIxh+6+SEOxv9P] X-ClientProxiedBy: PR1P264CA0031.FRAP264.PROD.OUTLOOK.COM (2603:10a6:102:19f::18) To AM7PR03MB6660.eurprd03.prod.outlook.com (2603:10a6:20b:1c1::22) X-Microsoft-Original-Message-ID: <20210902154127.2571616-1-andreas.rheinhardt@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sblaptop.fritz.box (188.192.142.38) by PR1P264CA0031.FRAP264.PROD.OUTLOOK.COM (2603:10a6:102:19f::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4478.20 via Frontend Transport; Thu, 2 Sep 2021 15:41:38 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: b80c1aed-6be4-42e8-1d42-08d96e28270f X-MS-Exchange-SLBlob-MailProps: gjx25WM8ZNW7ZO6S4gJQgEvgNWj+v++jloQoiKFMTaZ4JaJas2mL0Y4qptzJ9RomgZUcI+bC/9/Nz0ohorhnZ+BTu+gq9rixI2K1NBfMlVwdPVXQ8Qxiq2XkUngef7JpDVZnfp9EFZhqDBuD8NTWFjqCe1qqDHiKRgybXOwYCHx1bziynVftVXXRIYz8f0t7ljQIvJIkRc+qJXyN3iBIvRD+247Meff51enhWPHTT437G3800H3VWk3VJy7HHVYGM8Gj0b0ZTtwVOQ3XhZQfg/e5B5AcClyVzPqjN1Sou2tHqQv5U5fHdDhSj7ShVo2YHyUJw8J4APfvYAtkCOFSPNB7npfsiAGXE7DrezEOAp43OftyH2mvsG3GHAA0nd08MTOlEzbDPyx2431/MAcJpRt+SwyFniTI5Osf0Ypibyx80gDEvlRULWUOlrcesgeM4tv9pgbdubjpRtxumYm4/rRwLDKSSyRoLPibVR7l8TETiSLnSGF2d9DRuOV9a9YURCdrIwOjw9WhwyU4+9vnDcBb6vAtl+Z4T5QMNd5c8PVwGg7vRVkSzkWp8/L1OF/6BlwDgc8B0XokKLhAZ2/HW75Corm5Z/1ffCDYeYgpauDPNKGEbU0SGjlo/U8BuxobC/TvQLoYQe1RBz2ii0rpcDxbNVrXfM2tvgB1v21TO2+anK9XgWKZEHFd2gDs3NkFDAphIqjYPAHo9633lbOwf9QNS3SLcaV8j9CcWJiYHr0= X-MS-TrafficTypeDiagnostic: AM7PR03MB6532: X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: +mXhsUB2BZrokx5CiyKes8c5ilqWxLF6SN6ZgIDD6FlI2kLByLV87Qze5Ic5SL20J8qKXqxsVPosI9VapSelHC27JXJvsm2uBcJcusgSpyvuBbkAlMshGDh/A3y9/KUyi7IR+PFzxBZ+7QJNsjd8707vU1f3FrSo7OYHpNIM0P3LbdC57HYgiNYEjNjgIviIybQuS8j1KyEWih17WZHD0hTEGmMsKp0+xrPhf4MlwA/fbFY3MfbLMjb2Ify1IRwKBx7rdd3yg7b1K7zX9/o35+0c6kJ9AEumU/eY5jGvhRe7F7aqD7WmZekPNOT/y/8XnacfPw8mMpV9zGBl5K28WR9LpQcuI1ZJ0etbR0l07NhDeltomKxCnMVuBx3m7o2v8zlzc08NLeCoAd0Dy54A4Xp8RgxfCYX/ySYUnAsbf9mmZ8DpseEuBlTYYQASxqc8 X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: l4J1hibdiQADN7ETstMbWUMaKBh09kVA8huTrBdG3N+c/RCCG731F76gwYmBVIX7i8008Ufrhw2UuIh5fENx7XWYZEgpWleNYVm/39ZZHSDAhxG1Zua+5W3ANhmtVX9XCib7VJfV46ZKpd9cmYpAvw== X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: b80c1aed-6be4-42e8-1d42-08d96e28270f X-MS-Exchange-CrossTenant-AuthSource: AM7PR03MB6660.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Sep 2021 15:41:38.5034 (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: AM7PR03MB6532 Subject: [FFmpeg-devel] [PATCH 02/13] avcodec/pthread_frame: Move (init|free)_pthread() to pthread.c 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: 8gXL9PeO26+5 We have more mutexes/condition variables whose initialization is unchecked. Also use a proper namespace for these functions. Signed-off-by: Andreas Rheinhardt --- libavcodec/pthread.c | 38 ++++++++++++++++++++++++ libavcodec/pthread_frame.c | 55 +++++------------------------------ libavcodec/pthread_internal.h | 32 ++++++++++++++++++++ 3 files changed, 77 insertions(+), 48 deletions(-) diff --git a/libavcodec/pthread.c b/libavcodec/pthread.c index 14b7cca4fe..e7bad19f8b 100644 --- a/libavcodec/pthread.c +++ b/libavcodec/pthread.c @@ -29,6 +29,8 @@ * @see doc/multithreading.txt */ +#include "libavutil/thread.h" + #include "avcodec.h" #include "internal.h" #include "pthread_internal.h" @@ -86,3 +88,39 @@ void ff_thread_free(AVCodecContext *avctx) else ff_slice_thread_free(avctx); } + +av_cold void ff_pthread_free(void *obj, const unsigned offsets[]) +{ + unsigned cnt = *(unsigned*)((char*)obj + offsets[0]); + const unsigned *cur_offset = offsets; + + *(unsigned*)((char*)obj + offsets[0]) = 0; + + for (; *(++cur_offset) != THREAD_SENTINEL && cnt; cnt--) + pthread_mutex_destroy((pthread_mutex_t*)((char*)obj + *cur_offset)); + for (; *(++cur_offset) != THREAD_SENTINEL && cnt; cnt--) + pthread_cond_destroy ((pthread_cond_t *)((char*)obj + *cur_offset)); +} + +av_cold int ff_pthread_init(void *obj, const unsigned offsets[]) +{ + const unsigned *cur_offset = offsets; + unsigned cnt = 0; + int err; + +#define PTHREAD_INIT_LOOP(type) \ + for (; *(++cur_offset) != THREAD_SENTINEL; cnt++) { \ + pthread_ ## type ## _t *dst = (void*)((char*)obj + *cur_offset); \ + err = pthread_ ## type ## _init(dst, NULL); \ + if (err) { \ + err = AVERROR(err); \ + goto fail; \ + } \ + } + PTHREAD_INIT_LOOP(mutex) + PTHREAD_INIT_LOOP(cond) + +fail: + *(unsigned*)((char*)obj + offsets[0]) = cnt; + return err; +} diff --git a/libavcodec/pthread_frame.c b/libavcodec/pthread_frame.c index 8c0966f026..9c5d66c0d4 100644 --- a/libavcodec/pthread_frame.c +++ b/libavcodec/pthread_frame.c @@ -680,59 +680,18 @@ static void park_frame_worker_threads(FrameThreadContext *fctx, int thread_count async_lock(fctx); } -#define SENTINEL 0 // This forbids putting a mutex/condition variable at the front. -#define OFFSET_ARRAY(...) __VA_ARGS__, SENTINEL -#define DEFINE_OFFSET_ARRAY(type, name, mutexes, conds) \ -static const unsigned name ## _offsets[] = { offsetof(type, pthread_init_cnt),\ - OFFSET_ARRAY mutexes, \ - OFFSET_ARRAY conds } - #define OFF(member) offsetof(FrameThreadContext, member) -DEFINE_OFFSET_ARRAY(FrameThreadContext, thread_ctx, +DEFINE_OFFSET_ARRAY(FrameThreadContext, thread_ctx, pthread_init_cnt, (OFF(buffer_mutex), OFF(hwaccel_mutex), OFF(async_mutex)), (OFF(async_cond))); #undef OFF #define OFF(member) offsetof(PerThreadContext, member) -DEFINE_OFFSET_ARRAY(PerThreadContext, per_thread, +DEFINE_OFFSET_ARRAY(PerThreadContext, per_thread, pthread_init_cnt, (OFF(progress_mutex), OFF(mutex)), (OFF(input_cond), OFF(progress_cond), OFF(output_cond))); #undef OFF -static av_cold void free_pthread(void *obj, const unsigned offsets[]) -{ - unsigned cnt = *(unsigned*)((char*)obj + offsets[0]); - const unsigned *cur_offset = offsets; - - for (; *(++cur_offset) != SENTINEL && cnt; cnt--) - pthread_mutex_destroy((pthread_mutex_t*)((char*)obj + *cur_offset)); - for (; *(++cur_offset) != SENTINEL && cnt; cnt--) - pthread_cond_destroy ((pthread_cond_t *)((char*)obj + *cur_offset)); -} - -static av_cold int init_pthread(void *obj, const unsigned offsets[]) -{ - const unsigned *cur_offset = offsets; - unsigned cnt = 0; - int err; - -#define PTHREAD_INIT_LOOP(type) \ - for (; *(++cur_offset) != SENTINEL; cnt++) { \ - pthread_ ## type ## _t *dst = (void*)((char*)obj + *cur_offset); \ - err = pthread_ ## type ## _init(dst, NULL); \ - if (err) { \ - err = AVERROR(err); \ - goto fail; \ - } \ - } - PTHREAD_INIT_LOOP(mutex) - PTHREAD_INIT_LOOP(cond) - -fail: - *(unsigned*)((char*)obj + offsets[0]) = cnt; - return err; -} - void ff_frame_thread_free(AVCodecContext *avctx, int thread_count) { FrameThreadContext *fctx = avctx->internal->thread_ctx; @@ -792,14 +751,14 @@ void ff_frame_thread_free(AVCodecContext *avctx, int thread_count) av_frame_free(&p->frame); - free_pthread(p, per_thread_offsets); + ff_pthread_free(p, per_thread_offsets); av_packet_free(&p->avpkt); av_freep(&p->avctx); } av_freep(&fctx->threads); - free_pthread(fctx, thread_ctx_offsets); + ff_pthread_free(fctx, thread_ctx_offsets); av_freep(&avctx->internal->thread_ctx); } @@ -845,7 +804,7 @@ static av_cold int init_thread(PerThreadContext *p, int *threads_to_free, } } - err = init_pthread(p, per_thread_offsets); + err = ff_pthread_init(p, per_thread_offsets); if (err < 0) return err; @@ -906,9 +865,9 @@ int ff_frame_thread_init(AVCodecContext *avctx) if (!fctx) return AVERROR(ENOMEM); - err = init_pthread(fctx, thread_ctx_offsets); + err = ff_pthread_init(fctx, thread_ctx_offsets); if (err < 0) { - free_pthread(fctx, thread_ctx_offsets); + ff_pthread_free(fctx, thread_ctx_offsets); av_freep(&avctx->internal->thread_ctx); return err; } diff --git a/libavcodec/pthread_internal.h b/libavcodec/pthread_internal.h index d2115cbbaf..d0b6a7a673 100644 --- a/libavcodec/pthread_internal.h +++ b/libavcodec/pthread_internal.h @@ -31,4 +31,36 @@ void ff_slice_thread_free(AVCodecContext *avctx); int ff_frame_thread_init(AVCodecContext *avctx); void ff_frame_thread_free(AVCodecContext *avctx, int thread_count); +#define THREAD_SENTINEL 0 // This forbids putting a mutex/condition variable at the front. +/** + * Initialize/destroy a list of mutexes/conditions contained in a structure. + * The positions of these mutexes/conditions in the structure are given by + * their offsets. Because it is undefined behaviour to destroy + * an uninitialized mutex/condition, ff_pthread_init() stores the number + * of successfully initialized mutexes and conditions in the object itself + * and ff_pthread_free() uses this number to destroy exactly the mutexes and + * condition variables that have been successfully initialized. + * + * @param obj The object containing the mutexes/conditions. + * @param[in] offsets An array of offsets. Its first member gives the offset + * of the variable that contains the count of successfully + * initialized mutexes/condition variables; said variable + * must be an unsigned int. Two arrays of offsets, each + * delimited by a THREAD_SENTINEL follow. The first + * contains the offsets of all the mutexes, the second + * contains the offsets of all the condition variables. + */ +int ff_pthread_init(void *obj, const unsigned offsets[]); +void ff_pthread_free(void *obj, const unsigned offsets[]); + +/** + * Macros to help creating the above lists. mutexes and conds need + * to be parentheses-enclosed lists of offsets in the containing structure. + */ +#define OFFSET_ARRAY(...) __VA_ARGS__, THREAD_SENTINEL +#define DEFINE_OFFSET_ARRAY(type, name, cnt_variable, mutexes, conds) \ +static const unsigned name ## _offsets[] = { offsetof(type, cnt_variable), \ + OFFSET_ARRAY mutexes, \ + OFFSET_ARRAY conds } + #endif // AVCODEC_PTHREAD_INTERNAL_H