From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTP id 0DDEF42296 for ; Thu, 30 Jun 2022 09:48:00 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 8378168B677; Thu, 30 Jun 2022 12:47:58 +0300 (EEST) Received: from EUR05-DB8-obe.outbound.protection.outlook.com (mail-db8eur05olkn2079.outbound.protection.outlook.com [40.92.89.79]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id A38F168B312 for ; Thu, 30 Jun 2022 12:47:52 +0300 (EEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=T9clkIcduZ5Uc+kzU/1d1BrUtF+mE4RVNYUSyZoiIHe8eJ5NpPtTM+dPtr5GSDg1EOvxjuXUuFVXIrxOqcAuEsGLlJiuRLFp2Ociy1IquqkYIFrciTObAmnx3pD9p672zBfa3OXnvczn9AFR+j3Pr1r4wEfxnsrYuvugrIYP2U74PWnutwtCPUhVFRdq5pl26l1RLbNINj3Lqhyxom+5alsFSuUQLCzD4yZOijZ1L0HzYcLUfQEkz1jM6+e7swyhiDzHUh9PIpzJ9LB2MGxPMVNLOhp2ilNumQHOigRPY7tbTnCXnMZj7u8x+k1izWRFMvR66FdTIJnBPca2yuD5Hw== 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=KrVGeqq7houqg4wtlHxDsx+LVnidfMmb5jsgRiCtrlk=; b=gBwEm4Ib5lB6OP2AwuQa8kvOw0d4UmkosEa1Z2sXZQ7QuYPT7GRUQRpIN9KJGT6KML62+sp5zQPTNNiUZiusH3PVQ+NPuuWcroLXsRJ2AmcW/kyAsl2nWECwYUmKveb4Ef9WVJcHK8lpIDqe9d4QEEc1D9Q/lr3IUznp2FZy3PfhonPO9DlHu3NRCfma5R6TehD0/8L23DEXNhPmo3VytRokiGQv9n1ltegCEj3vzSY+JcfrIZD9NjLWtZAaJ5ZR497IoN1OcsqRUb+W6eXWIOjhLMVuv6/WLl5tS/3RJky7lh4EqNrexmN7oDIiy4GDX8KpdBLsgG/Mrn6G6DndiA== 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=KrVGeqq7houqg4wtlHxDsx+LVnidfMmb5jsgRiCtrlk=; b=LmNaQT+16P4noWpcJGWo5FX+KqmTkvEIDS/0NGn9A8yS4ltF3oZyRxAXTcFlGBtLgfIs2cdAJ92tn0PlWvNqIpHWN2GL/jZYaqOD+qrM2L3heu1W4G9YjPz74sHEi6azKLcxTYP9lpaHpK8v8jHvUeM7dR4PceTTqnYhMktPi66QVwQL5eQ4E5s6ZZTuNFuuUgMwYiWPefWlBJ/4UUQwwiFTz2QsQ80OnG4/gFUpUS4cZEtg7+V7AtQe7Jn3Ob4O969cv6WVXen3Uu++hSD6zgtyz+j775EbaRUdp5L9STb9J1FbWxLsNcNhxXlG0iidK3C+i5gJrGk/Wcqw7hSbjQ== Received: from DB6PR0101MB2214.eurprd01.prod.exchangelabs.com (2603:10a6:4:42::27) by DB6PR01MB3878.eurprd01.prod.exchangelabs.com (2603:10a6:6:49::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5373.18; Thu, 30 Jun 2022 09:47:50 +0000 Received: from DB6PR0101MB2214.eurprd01.prod.exchangelabs.com ([fe80::60b9:9f29:40cc:f01c]) by DB6PR0101MB2214.eurprd01.prod.exchangelabs.com ([fe80::60b9:9f29:40cc:f01c%10]) with mapi id 15.20.5373.018; Thu, 30 Jun 2022 09:47:50 +0000 From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Thu, 30 Jun 2022 11:47:43 +0200 Message-ID: X-Mailer: git-send-email 2.34.1 X-TMN: [LucXDh8pIdNddS/HSoEb7WpMvttvZpfA] X-ClientProxiedBy: FR0P281CA0040.DEUP281.PROD.OUTLOOK.COM (2603:10a6:d10:48::11) To DB6PR0101MB2214.eurprd01.prod.exchangelabs.com (2603:10a6:4:42::27) X-Microsoft-Original-Message-ID: <20220630094743.2512824-1-andreas.rheinhardt@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: f383a271-ea83-4547-52ff-08da5a7d983e X-MS-Exchange-SLBlob-MailProps: gjx25WM8ZNXqMBxJfDcJWDoAj06zwkJHIdMum+ZoKmdc247DaIarJ+9MVehL8osP6Fx8aFlFPeYgTRm/ls+otyxGE/3Axaxd6WA8PbxAAOpzPaqAVzTMIdxMt6dvUE7fHPbYLWBmO1KDcU5aOffnA5YEGHMREHdSJjnzE9zUFokKdPh2qS++N8iI6xoky9h9Dd49gKO0OnP3QboH6GfXu1bON2CRJ5XfG7qTsZ8d4NAfsuyIojziRILpWjOysAT0eZJtR2TdilVjQKr8bqIdSfHVAQTOmpPrxs1mo1r7Jv5G0ZHnA0e9RSUL206PwCXK6My3WsroYkonKjSll/UQ91xoNcaDqbc2lILaSWe32n1e/+cI7bJCdcYLzxPHvNaIxCHEIJCkfImi0U+Tj3ZqMvrh7fM/B7xhfAycZ4lPKVszifx7wGZnJRTTc0eFkeqNSoJVQ+o3p9/fr+hwzq0uTk4ZwGCfqMtsl/4Gqt3juQKNFXdrB0zP9xsQWkAo7cSen6VX6/i19rxSC+8f9kCAcYIhIzyaIZ24FGngNyN/86LFzKRfU38pllyO8V0T5ClaGLlGnwJV2Kk/g0A0b9e8hzBN5KLRBEWpN2C5gDWvesrEapVDgP5OsxNjPLg1+E7P+LM+kPUwmc9jVEmSQWftTiBBKV3tbO8U76zWRbEEvM90meZPAdON4n43WlW2WEcXw5kmJGS+VmN0E7rTBYkP0g90xIx8O2Sen1ned/7a4hI= X-MS-TrafficTypeDiagnostic: DB6PR01MB3878:EE_ X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: MVkSgBK8fg56uhsynKVZSo63cmt9QM9JKnyJxo3aB63p2yw0CivEL2ms7Ux2lsb6NEzgLxML0o1X6H2W1JfcjI9OFPoUPFz25vte3MltyHJLbgSxWAKIWji25oJzUyWWAxqi+afQaNsN/2ZQ5//LhRPtxxo67FZSLQEtNxNnj71WnWjH75nwOkWzRIGDHcoE1DT3c3JYImYL23cQ6PnHUrtaqNWYM4TWTKdzLis2e4YWjM+qju5L/3XE9hKtZ8NCo9gtLZreHWDl4nFenSGsulT8Agya470XHrXy5AjYj87+OdrMivmERIcgIcPZ2n4DDKlCVZadgcL0nbeQq5x69KmTF67P3CYMRNinRxDIwNaL/ItID8IWkHlTgOb0XStoq+OR5ddUfArsZ2ChHNftkZruarlmOSuFqu+cp0EW6/9JTvrL7UQOd7/O51yrPy+7Zq/zhLPgCfv/OkNAdvUerHJciTT+I1cqbeWwZGVsCaTZRbYiajlBCLYMliYV6I2CGnn3UPXOWqU6aVdzZX+CXBXSirTvEyYEGWR2jMy8dhML6eLmRbxWhHjgFzAffJkooduZyPsioPTpcsXpejF15SCD75h7K0Sh4iHRGChbIaMiNzcSBMxj4/NonrAy4cB4FcxaKuhTvCvcm4I+HDkrqA== X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?tG9ZOcm3bsVcobyyNg857+hMzXAbTEGRrHu4R7v8DNYqtzpCmoVOjbfcCdz6?= =?us-ascii?Q?yLdjRK58cl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DTKVS2JrdST?= =?us-ascii?Q?QoNBRvQzdqPXCbw4WsT8DwBCRWh+tLsZVuRV6wevtS4y2WxnoYZGwJGfrB1b?= =?us-ascii?Q?OBKEm8Svsuzk+86OU9t9h8+7m0HRJ9LY9EY7W7dTwjtFBt3wOC9g+GGY2+Y0?= =?us-ascii?Q?fNfo2xjpWTkwAiixUOSZX0jsNMQwAwwj107gVebVF2sxaMAjxU1Djq1G5wSy?= =?us-ascii?Q?rO2+WSNJa19P/gosh4GgZAWAii60f8nKn7b0MtyonvgF0gP7Zl0X//nehEny?= =?us-ascii?Q?1QeZNYTKb+SYXwnxXq+SEllWNA/12X8g1xdR4+89crWz8jAqPH4xI1wvq1it?= =?us-ascii?Q?6YCyterOhd0vyPBQ8ZuXa5+50up90O1EvFPm7CnxElnb6ubkTBRQ1ySxJK0Q?= =?us-ascii?Q?59k=3D?= X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: f383a271-ea83-4547-52ff-08da5a7d983e X-MS-Exchange-CrossTenant-AuthSource: DB6PR0101MB2214.eurprd01.prod.exchangelabs.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Jun 2022 09:47:50.0948 (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: DB6PR01MB3878 Subject: [FFmpeg-devel] [PATCH] avcodec/pthread_slice: Don't reinitialise initialised mutex 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 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: It results in undefined behaviour. Instead initialize the mutexes and condition variables once during init (and check these initializations). Also combine the corresponding mutex and condition variable into one structure so that one can allocate their array jointly. Signed-off-by: Andreas Rheinhardt --- libavcodec/hevcdec.c | 7 +++- libavcodec/pthread_slice.c | 83 ++++++++++++++++++++++++-------------- libavcodec/thread.h | 1 + 3 files changed, 58 insertions(+), 33 deletions(-) diff --git a/libavcodec/hevcdec.c b/libavcodec/hevcdec.c index 7037048d53..f222f20706 100644 --- a/libavcodec/hevcdec.c +++ b/libavcodec/hevcdec.c @@ -3803,9 +3803,12 @@ static av_cold int hevc_decode_init(AVCodecContext *avctx) HEVCContext *s = avctx->priv_data; int ret; - if(avctx->active_thread_type & FF_THREAD_SLICE) + if (avctx->active_thread_type & FF_THREAD_SLICE) { s->threads_number = avctx->thread_count; - else + ret = ff_slice_thread_init_progress(avctx); + if (ret < 0) + return ret; + } else s->threads_number = 1; if((avctx->active_thread_type & FF_THREAD_FRAME) && avctx->thread_count > 1) diff --git a/libavcodec/pthread_slice.c b/libavcodec/pthread_slice.c index 0ad1965a22..e34116116d 100644 --- a/libavcodec/pthread_slice.c +++ b/libavcodec/pthread_slice.c @@ -41,6 +41,11 @@ typedef int (action_func)(AVCodecContext *c, void *arg); typedef int (action_func2)(AVCodecContext *c, void *arg, int jobnr, int threadnr); typedef int (main_func)(AVCodecContext *c); +typedef struct Progress { + pthread_cond_t cond; + pthread_mutex_t mutex; +} Progress; + typedef struct SliceThreadContext { AVSliceThread *thread; action_func *func; @@ -53,8 +58,7 @@ typedef struct SliceThreadContext { int *entries; int entries_count; int thread_count; - pthread_cond_t *progress_cond; - pthread_mutex_t *progress_mutex; + Progress *progress; } SliceThreadContext; static void main_function(void *priv) { @@ -83,13 +87,13 @@ void ff_slice_thread_free(AVCodecContext *avctx) avpriv_slicethread_free(&c->thread); for (i = 0; i < c->thread_count; i++) { - pthread_mutex_destroy(&c->progress_mutex[i]); - pthread_cond_destroy(&c->progress_cond[i]); + Progress *const progress = &c->progress[i]; + pthread_mutex_destroy(&progress->mutex); + pthread_cond_destroy(&progress->cond); } av_freep(&c->entries); - av_freep(&c->progress_mutex); - av_freep(&c->progress_cond); + av_freep(&c->progress); av_freep(&avctx->internal->thread_ctx); } @@ -172,65 +176,82 @@ int ff_slice_thread_init(AVCodecContext *avctx) return 0; } +int av_cold ff_slice_thread_init_progress(AVCodecContext *avctx) +{ + SliceThreadContext *const p = avctx->internal->thread_ctx; + int err, i = 0, thread_count = avctx->thread_count; + + p->progress = av_calloc(thread_count, sizeof(*p->progress)); + if (!p->progress) { + err = AVERROR(ENOMEM); + goto fail; + } + + for (; i < thread_count; i++) { + Progress *const progress = &p->progress[i]; + err = pthread_mutex_init(&progress->mutex, NULL); + if (err) { + err = AVERROR(err); + goto fail; + } + err = pthread_cond_init (&progress->cond, NULL); + if (err) { + err = AVERROR(err); + pthread_mutex_destroy(&progress->mutex); + goto fail; + } + } + err = 0; +fail: + p->thread_count = i; + return err; +} + void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n) { SliceThreadContext *p = avctx->internal->thread_ctx; + Progress *const progress = &p->progress[thread]; int *entries = p->entries; - pthread_mutex_lock(&p->progress_mutex[thread]); + pthread_mutex_lock(&progress->mutex); entries[field] +=n; - pthread_cond_signal(&p->progress_cond[thread]); - pthread_mutex_unlock(&p->progress_mutex[thread]); + pthread_cond_signal(&progress->cond); + pthread_mutex_unlock(&progress->mutex); } void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift) { SliceThreadContext *p = avctx->internal->thread_ctx; + Progress *progress; int *entries = p->entries; if (!entries || !field) return; thread = thread ? thread - 1 : p->thread_count - 1; + progress = &p->progress[thread]; - pthread_mutex_lock(&p->progress_mutex[thread]); + pthread_mutex_lock(&progress->mutex); while ((entries[field - 1] - entries[field]) < shift){ - pthread_cond_wait(&p->progress_cond[thread], &p->progress_mutex[thread]); + pthread_cond_wait(&progress->cond, &progress->mutex); } - pthread_mutex_unlock(&p->progress_mutex[thread]); + pthread_mutex_unlock(&progress->mutex); } int ff_alloc_entries(AVCodecContext *avctx, int count) { - int i; - if (avctx->active_thread_type & FF_THREAD_SLICE) { SliceThreadContext *p = avctx->internal->thread_ctx; if (p->entries) { - av_assert0(p->thread_count == avctx->thread_count); av_freep(&p->entries); } - p->thread_count = avctx->thread_count; p->entries = av_calloc(count, sizeof(*p->entries)); - - if (!p->progress_mutex) { - p->progress_mutex = av_malloc_array(p->thread_count, sizeof(pthread_mutex_t)); - p->progress_cond = av_malloc_array(p->thread_count, sizeof(pthread_cond_t)); - } - - if (!p->entries || !p->progress_mutex || !p->progress_cond) { - av_freep(&p->entries); - av_freep(&p->progress_mutex); - av_freep(&p->progress_cond); + if (!p->entries) { + p->entries_count = 0; return AVERROR(ENOMEM); } p->entries_count = count; - - for (i = 0; i < p->thread_count; i++) { - pthread_mutex_init(&p->progress_mutex[i], NULL); - pthread_cond_init(&p->progress_cond[i], NULL); - } } return 0; diff --git a/libavcodec/thread.h b/libavcodec/thread.h index d36dc83bd7..92cbd927f1 100644 --- a/libavcodec/thread.h +++ b/libavcodec/thread.h @@ -106,6 +106,7 @@ int ff_slice_thread_execute_with_mainfunc(AVCodecContext *avctx, void ff_thread_free(AVCodecContext *s); int ff_alloc_entries(AVCodecContext *avctx, int count); void ff_reset_entries(AVCodecContext *avctx); +int ff_slice_thread_init_progress(AVCodecContext *avctx); void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n); void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift); -- 2.34.1 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".