From patchwork Tue Aug 15 14:50:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nuo Mi X-Patchwork-Id: 43228 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:4c9d:b0:130:ccc6:6c4b with SMTP id fq29csp623032pzb; Tue, 15 Aug 2023 07:50:42 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHma3DpxPUlAvzLV0gnNzqs5iKHynLhlxD2k3pKeQh2dsmNKPczaqtN6XJkUfwOXoBd0Lag X-Received: by 2002:a17:907:75ee:b0:99d:9adc:d69c with SMTP id jz14-20020a17090775ee00b0099d9adcd69cmr6088026ejc.14.1692111042584; Tue, 15 Aug 2023 07:50:42 -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 p7-20020a17090628c700b00986560ae9a7si8543463ejd.307.2023.08.15.07.50.42; Tue, 15 Aug 2023 07:50:42 -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=dEIyVx9u; 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=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id BF94468C5E9; Tue, 15 Aug 2023 17:50:39 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from APC01-PSA-obe.outbound.protection.outlook.com (mail-psaapc01olkn2038.outbound.protection.outlook.com [40.92.52.38]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 72ADB68C6C5 for ; Tue, 15 Aug 2023 17:50:32 +0300 (EEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=BcPVI1PhAxKCT9WTY7Awh3JzyQlszE1Get47PtvJ43M8xOscUXKJGv1kJtuP8+rdil6UXgYevUHygB9H4G8Aoo45c4m6iU6Mzno74LG3a9Wg1bsDgqYC2VZojkL3TBJu751dPyxB9/3uxiCJHEaJ8qVyzdr2puwCv01H/6mfc1/UNcm4vq7CMut1bCGlrP9Qc9vvJ17VGS7KKFfc2gg/UmnzMYhVnkBwqnpL/PXFSwg5UvzgVD0BzExrppITMUz0Psrbwyz5ljESHmRmY+LX8+N2UWYgTot9GHrePFBJ+5PqpmlWfpT484uyLtmI2ZnDI2w6HE1sV6/JRW2X35/Jng== 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=Zpnl/HsM6PpAqLZ1fxIyTknGNrbEmI6vsqR94GXsn3Q=; b=JBIXsUGmj4ZoLRiFSNM7QOaLq0q+lQSR5ZH/3wrbu0qZZwcQ2XXcqTLM/TUao0zkI0+BlQ502eN0Hc3srk1O6dhk5NfIBnukmAw724i8hoWQbAt5n4RjJ4csNlLVOI3qxXstIsrX749y5TQyoOBO+4Ec4TZ54Jc6YOSzPdlFKxjPfpSXvG21LWt0hli1kSh36Ybj/7Km3fQeHhbD+v4FEm4hnWsBSxt07YMoQLG7vHIEI6Lu3Htp/V49u+rSs+jFO2j/2hxrbOln82lkjKZFNls5rycPjJg4m9VsuTByKnDZvtrcmq5n1SYBXNHtYqSz2mhd1BoNIHdsd1VWDQBH5w== 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=Zpnl/HsM6PpAqLZ1fxIyTknGNrbEmI6vsqR94GXsn3Q=; b=dEIyVx9uqPdJ14rgynOllhK0u3UaGsL+IoHmRG56uADsExfJRCl2ttexZ8ETcjh73ceiXHMCBmnKb6hYxXKOFqDPjtJzBFBHDI00ix8EqDcr6fsRl8nN0iUCgHz/4yrwY/GRfb2GwoPB7XuVAcFsn1VRo+nYYo2RNbQ3JHL6mIhe2WiCStrgWekn6+diwm22lRmirUD9dtLiii6itpdVi/MBeClSOYpnNWvw7eXBu1TvJSyZwb5NeRFED4+WjV9Iqd91LppM+WWBzGCLU4GmjuhMuRuAoJiGYgIRnJOh2iNN1bkrkzx+KXZhWKR8k+ugksPYR+BR9bF91qslsq+RPg== Received: from TYSPR06MB6433.apcprd06.prod.outlook.com (2603:1096:400:47a::6) by SEZPR06MB5764.apcprd06.prod.outlook.com (2603:1096:101:aa::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6678.26; Tue, 15 Aug 2023 14:50:27 +0000 Received: from TYSPR06MB6433.apcprd06.prod.outlook.com ([fe80::f1a4:bbff:7592:ee31]) by TYSPR06MB6433.apcprd06.prod.outlook.com ([fe80::f1a4:bbff:7592:ee31%5]) with mapi id 15.20.6678.022; Tue, 15 Aug 2023 14:50:27 +0000 From: Nuo Mi To: ffmpeg-devel@ffmpeg.org Date: Tue, 15 Aug 2023 22:50:13 +0800 Message-ID: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: X-TMN: [oxNtvVCVHA5MSMkuEF2sFmPWlO18Ednp] X-ClientProxiedBy: SG2PR02CA0005.apcprd02.prod.outlook.com (2603:1096:3:17::17) To TYSPR06MB6433.apcprd06.prod.outlook.com (2603:1096:400:47a::6) X-Microsoft-Original-Message-ID: <20230815145013.2346-1-nuomi2021@gmail.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 2 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: TYSPR06MB6433:EE_|SEZPR06MB5764:EE_ X-MS-Office365-Filtering-Correlation-Id: c617c3b5-840c-41d8-d6ca-08db9d9ef6ce X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Hrgrp+lGgEWF5W4uG6Fn0DXJmPkCWuIESXqJqRDnG2Bn34/glzeUNwHbD7P/FtIPvTzwkZPuMIURvi9kfnZz7DC6rAq6K732Ld6O/wJoy0XTFlsVUPvdbPo3m7lJBSUhf2kMMfcdVjeldSmRlDphzHisPsB0w/v+R68qqe/j5LaIppbsTcqr5J7W/6EqfEbxYwGP0qeM7xDVEFwsAPuiXGK/U9rAP6Bu7XVqM9AwTgshP2mec7Ymg9KT3xjU+70xPLUP76geAsMzwWt2WqZQmw9V6sfU7oOgMH8hfNZPijHdqF1MhuNxw4w0DH+STs0IA5K9rQsJHU1E46QFwMwWX2w0IZeAMI00VNhI5O/xtu+MQxbnOfapyaeCdi50SGUARDTY6NGYrBUEfr+VvGkHWePbkc8oPdPxAdUyfm/gYGVFuiPQsCyQwCYhakc9TtKFfae4KirWP5DnpZIiH7aInIO9cRrtR9CLKBQjvB9jtZgCeZYgIiePUXvHSgSOTZBvWjPDDRPobBuyA/92IHx5c0PHJPcDA6wkKzzvRioniJ5HMm8NcPsMiwBwe8AnLig4i6eM93/PH+joS9X8RKIf6YeXfJDlTCuzsZP2K8vYO4plJ5RKcsD5k40iqdOPuFtn5H+WFMNRF8B9wCRV2913MA== X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: MD9QIbZOeI78D6+MfLdYH6Hp3c2BYY/wFZ3hr2VPSlD/2+MxuSAY8t8f5XCwNpjhUIyl65KvM6F8qsDfaP6WewRVO+GDuAu/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3I67rHrnnGDyqS3uqcTs0MWMUb4kND9N/x+ECpixFDt77zyfdbKIovOPWi/niqmYxPGRqk9YbPdHTYxyh4y1SW7OON6fSZufy7C9fCCNLb74 X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: c617c3b5-840c-41d8-d6ca-08db9d9ef6ce X-MS-Exchange-CrossTenant-AuthSource: TYSPR06MB6433.apcprd06.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Aug 2023 14:50:27.6206 (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: SEZPR06MB5764 Subject: [FFmpeg-devel] [PATCH v4] vvcdec: add thread executor 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: Nuo Mi Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: nzDtBTLL/U9h The executor design pattern was inroduced by java it also adapted by python Compared to handcrafted thread pool management, it greatly simplifies the thread code. --- libavutil/Makefile | 2 + libavutil/executor.c | 201 +++++++++++++++++++++++++++++++++++++++++++ libavutil/executor.h | 67 +++++++++++++++ 3 files changed, 270 insertions(+) create mode 100644 libavutil/executor.c create mode 100644 libavutil/executor.h diff --git a/libavutil/Makefile b/libavutil/Makefile index 7828c94dc5..4711f8cde8 100644 --- a/libavutil/Makefile +++ b/libavutil/Makefile @@ -31,6 +31,7 @@ HEADERS = adler32.h \ encryption_info.h \ error.h \ eval.h \ + executor.h \ fifo.h \ file.h \ frame.h \ @@ -127,6 +128,7 @@ OBJS = adler32.o \ encryption_info.o \ error.o \ eval.o \ + executor.o \ fifo.o \ file.o \ file_open.o \ diff --git a/libavutil/executor.c b/libavutil/executor.c new file mode 100644 index 0000000000..6b47ba0bf2 --- /dev/null +++ b/libavutil/executor.c @@ -0,0 +1,201 @@ +/* + * Copyright (C) 2023 Nuo Mi + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#include "internal.h" +#include "mem.h" +#include "thread.h" + +#include "executor.h" + +#if !HAVE_THREADS +#define pthread_create(t, a, s, ar) 0 +#define pthread_join(t, r) do {} while(0) + +#define pthread_cond_init(c, a) 0 +#define pthread_cond_broadcast(c) do {} while(0) +#define pthread_cond_signal(c) do {} while(0) +#define pthread_cond_wait(c, m) do {} while(0) +#define pthread_cond_destroy(c) do {} while(0) + +#define pthread_mutex_init(m, a) 0 +#define pthread_mutex_lock(l) do {} while(0) +#define pthread_mutex_unlock(l) do {} while(0) +#define pthread_mutex_destroy(l) do {} while(0) +#endif + +typedef struct ThreadInfo { + AVExecutor *e; + pthread_t thread; +} ThreadInfo; + +struct AVExecutor { + AVTaskCallbacks cb; + int thread_count; + + ThreadInfo *threads; + uint8_t *local_contexts; + + pthread_mutex_t lock; + pthread_cond_t cond; + int die; + + AVTask *tasks; +}; + +static AVTask* remove_task(AVTask **prev, AVTask *t) +{ + *prev = t->next; + t->next = NULL; + return t; +} + +static void add_task(AVTask **prev, AVTask *t) +{ + t->next = *prev; + *prev = t; +} + +static int run_one_task(AVExecutor *e, void *lc) +{ + AVTaskCallbacks *cb = &e->cb; + AVTask **prev; + + for (prev = &e->tasks; *prev && !cb->ready(*prev, cb->user_data); prev = &(*prev)->next) + /* nothing */; + if (*prev) { + AVTask *t = remove_task(prev, *prev); + pthread_mutex_unlock(&e->lock); + cb->run(t, lc, cb->user_data); + pthread_mutex_lock(&e->lock); + return 1; + } + return 0; +} + +#if HAVE_THREADS +static void *executor_worker_task(void *data) +{ + ThreadInfo *ti = (ThreadInfo*)data; + AVExecutor *e = ti->e; + void *lc = e->local_contexts + (ti - e->threads) * e->cb.local_context_size; + + pthread_mutex_lock(&e->lock); + while (1) { + if (e->die) break; + + if (!run_one_task(e, lc)) { + //no task in one loop + pthread_cond_wait(&e->cond, &e->lock); + } + } + pthread_mutex_unlock(&e->lock); + return NULL; +} +#endif + +static void executor_free(AVExecutor *e, const int has_lock, const int has_cond) +{ + if (e->thread_count) { + //signal die + pthread_mutex_lock(&e->lock); + e->die = 1; + pthread_cond_broadcast(&e->cond); + pthread_mutex_unlock(&e->lock); + + for (int i = 0; i < e->thread_count; i++) + pthread_join(e->threads[i].thread, NULL); + } + if (has_cond) + pthread_cond_destroy(&e->cond); + if (has_lock) + pthread_mutex_destroy(&e->lock); + + av_free(e->threads); + av_free(e->local_contexts); + + av_free(e); +} + +AVExecutor* av_executor_alloc(const AVTaskCallbacks *cb, int thread_count) +{ + AVExecutor *e; + int has_lock = 0, has_cond = 0; + if (!cb || !cb->user_data || !cb->ready || !cb->run || !cb->priority_higher) + return NULL; + + e = av_calloc(1, sizeof(*e)); + if (!e) + return NULL; + e->cb = *cb; + + e->local_contexts = av_calloc(thread_count, e->cb.local_context_size); + if (!e->local_contexts) + goto free_executor; + + e->threads = av_calloc(thread_count, sizeof(*e->threads)); + if (!e->threads) + goto free_executor; + + has_lock = !pthread_mutex_init(&e->lock, NULL); + has_cond = !pthread_cond_init(&e->cond, NULL); + + if (!has_lock || !has_cond) + goto free_executor; + + for (/* nothing */; e->thread_count < thread_count; e->thread_count++) { + ThreadInfo *ti = e->threads + e->thread_count; + ti->e = e; + if (pthread_create(&ti->thread, NULL, executor_worker_task, ti)) + goto free_executor; + } + return e; + +free_executor: + executor_free(e, has_lock, has_cond); + return NULL; +} + +void av_executor_free(AVExecutor **executor) +{ + if (!executor || !*executor) + return; + executor_free(*executor, 1, 1); + *executor = NULL; +} + +void av_executor_execute(AVExecutor *e, AVTask *t) +{ + AVTaskCallbacks *cb = &e->cb; + AVTask **prev; + + pthread_mutex_lock(&e->lock); + if (t) { + for (prev = &e->tasks; *prev && cb->priority_higher(*prev, t); prev = &(*prev)->next) + /* nothing */; + add_task(prev, t); + } + pthread_cond_signal(&e->cond); + pthread_mutex_unlock(&e->lock); + +#if !HAVE_THREADS + // We are running in a single-threaded environment, so we must handle all tasks ourselves + while (run_one_task(e, e->local_contexts)) + /* nothing */; +#endif +} diff --git a/libavutil/executor.h b/libavutil/executor.h new file mode 100644 index 0000000000..c3fe44bd0e --- /dev/null +++ b/libavutil/executor.h @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2023 Nuo Mi + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef AVUTIL_EXECUTOR_H +#define AVUTIL_EXECUTOR_H + +typedef struct AVExecutor AVExecutor; +typedef struct AVTask AVTask; + +struct AVTask { + AVTask *next; +}; + +typedef struct AVTaskCallbacks { + void *user_data; + + int local_context_size; + + // return 1 if a's priority > b's priority + int (*priority_higher)(const AVTask *a, const AVTask *b); + + // task is ready for run + int (*ready)(const AVTask *t, void *user_data); + + // run the task + int (*run)(AVTask *t, void *local_context, void *user_data); +} AVTaskCallbacks; + +/** + * Alloc executor + * @param callbacks callback strucutre for executor + * @param thread_count worker thread number + * @return return the executor + */ +AVExecutor* av_executor_alloc(const AVTaskCallbacks *callbacks, int thread_count); + +/** + * Free executor + * @param e pointer to executor + */ +void av_executor_free(AVExecutor **e); + +/** + * Add task to executor + * @param e pointer to executor + * @param t pointer to task. If NULL, it will wakeup one work thread + */ +void av_executor_execute(AVExecutor *e, AVTask *t); + +#endif //AVUTIL_EXECUTOR_H