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 C7BB94997A for ; Mon, 24 Jun 2024 04:48:21 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 0482668CDA6; Mon, 24 Jun 2024 07:48:09 +0300 (EEST) Received: from out203-205-221-245.mail.qq.com (out203-205-221-245.mail.qq.com [203.205.221.245]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 38B5C68CDA6 for ; Mon, 24 Jun 2024 07:47:58 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foxmail.com; s=s201512; t=1719204468; bh=w1QnylxIClvi+QIZ60+hp8NCiasZ8g5X4F+UycWs828=; h=From:To:Cc:Subject:Date; b=osF49eunUykdD8i3557aP0ajE1ysDyE09w17yVq5pwdsoBBaeHiywxprQmmhYpV0E P2aPT+WU+8Ra2XAYjdbUWVLIVG76xxxRTZ3DLHl5aMJLyiuJdRldl6CnyZGzfzfhGl 5mb8fmlA1YCgK/WSIBJbQmzUSV+NA9rXRe+dutRc= Received: from ZHILIZHAO-MB1.tencent.com ([113.108.77.64]) by newxmesmtplogicsvrszc13-0.qq.com (NewEsmtp) with SMTP id BEE9DC3E; Mon, 24 Jun 2024 12:47:46 +0800 X-QQ-mid: xmsmtpt1719204466thpy0v4fh Message-ID: X-QQ-XMAILINFO: MQjKimNqHmey5ZompCSlhSbpKV+QLyBmwsl7X9Cmm5kj57vsbrV6fH4RvTH+HZ 3I+Re9miGJHSK37k4E7HWOAPh5Do969DyRWVALNAWGnLIpJMZScc3G3PWG7kE5KQwTenkp/beUub Rz/N97Aeqkm+C0du0AifGFxUd6uhv9oo4jVEqpsFF4ZEeoVjSCy9Eh3s/5LNAnDczepLUiPbOwz2 g70eXCQHS7N8DypiW1Cm/TmEF9huBWls+0ImA0IqZF/6iq9syxvobHIB9Ubgs0B9r2CbUb92P+Ui XD8rZ/GhLIboGqaeLqVyHFURdLuispuTqQ79v7XIeWyJW3nt6NvzP4UVSuZXjCgVM8fWZXnG40/n kSXfFR3mxgyg7pWj/+74kvcT/ZA1RzkvTmdRxbnK3Rk6ZMcDNimKMpcrmjok5pf3PiAHPGQgnuOu xu92dz+NQzjeaC83+kqH2c2jmsmoBHeJK/N74issWSrqJB7tvbkKyD2FO0z/DgoiJhfoipNCCqvp 4m1aNhTig2SdmLv/YW//TBkqEaCyImcM5sOPST1Y9LyJpngIFmPZU9aHrd6JwPkWL6ZoPYb+EhRk Nqrxug/t0OkTqFgsngn6tm2PAfTYTlSET//tCuxqu75Fn9XxYIxyXESScq9zNpS/sBVpJ7SDJjlk V7C0vNMFuBr+qDi8csg2ptGAHaGcxNVu8H81wx6pDZoku0WF1ABvYSZCLxm3YPxebrA2eYLkO+b8 yRBstbvcZDWEWQvNKepUEJPvO4olZJrm2/t1adaZ+YHxsTcYJWhDNnrcd9DKeRRG3aGhvYNQHlmg WBGRKmPOHGIo4P07DyxmlnjDpMjARBwrJh8WvjO/VvF2XvNqRISxSDWXK1zqXzbw2wDkfqLJi/hR gC1Qiwd0QUPd5uy4LusoBBRN109SeTWavviAnvLFwH+NYqJ8/44mvG8SuFnNv/qEDEaI8F7Qng/Y 5LuRoMoQ9QqJ4hOGQxzU4cL0XafAxVWbvDGNJm/x76xBdAX1J2Eg== X-QQ-XMRINFO: NS+P29fieYNw95Bth2bWPxk= From: Zhao Zhili To: ffmpeg-devel@ffmpeg.org Date: Mon, 24 Jun 2024 12:47:43 +0800 X-OQ-MSGID: <20240624044744.52915-1-quinkblack@foxmail.com> X-Mailer: git-send-email 2.42.0 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/2] avutil/executor: Allowing thread_count be zero 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: Zhao Zhili 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: From: Zhao Zhili Before the patch, disable threads support at configure/build time was the only method to force zero thread in executor. However, it's common practice for libavcodec to run on caller's thread when user specify thread number to one. And for WASM environment, whether threads are supported needs to be detected at runtime. So executor should support zero thread at runtime. A single thread executor can be useful, e.g., to handle network protocol. So we can't take thread_count one as zero thread, which disabled a valid usercase. Other libraries take -threads 0 to mean auto. Executor as a low level utils doesn't do cpu detect. So take thread_count zero as zero thread, literally. Signed-off-by: Zhao Zhili --- libavutil/executor.c | 28 ++++++++++++++++++---------- libavutil/executor.h | 2 +- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/libavutil/executor.c b/libavutil/executor.c index 26691fe157..fb20104b58 100644 --- a/libavutil/executor.c +++ b/libavutil/executor.c @@ -82,9 +82,11 @@ static int run_one_task(AVExecutor *e, void *lc) /* nothing */; if (*prev) { AVTask *t = remove_task(prev, *prev); - ff_mutex_unlock(&e->lock); + if (e->thread_count > 0) + ff_mutex_unlock(&e->lock); cb->run(t, lc, cb->user_data); - ff_mutex_lock(&e->lock); + if (e->thread_count > 0) + ff_mutex_lock(&e->lock); return 1; } return 0; @@ -146,14 +148,17 @@ AVExecutor* av_executor_alloc(const AVTaskCallbacks *cb, int thread_count) return NULL; e->cb = *cb; - e->local_contexts = av_calloc(thread_count, e->cb.local_context_size); + e->local_contexts = av_calloc(FFMAX(thread_count, 1), e->cb.local_context_size); if (!e->local_contexts) goto free_executor; - e->threads = av_calloc(thread_count, sizeof(*e->threads)); + e->threads = av_calloc(FFMAX(thread_count, 1), sizeof(*e->threads)); if (!e->threads) goto free_executor; + if (!thread_count) + return e; + has_lock = !ff_mutex_init(&e->lock, NULL); has_cond = !ff_cond_init(&e->cond, NULL); @@ -175,9 +180,12 @@ free_executor: void av_executor_free(AVExecutor **executor) { + int thread_count; + if (!executor || !*executor) return; - executor_free(*executor, 1, 1); + thread_count = (*executor)->thread_count; + executor_free(*executor, thread_count, thread_count); *executor = NULL; } @@ -195,9 +203,9 @@ void av_executor_execute(AVExecutor *e, AVTask *t) ff_cond_signal(&e->cond); ff_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 + if (!e->thread_count || !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 */; + } } diff --git a/libavutil/executor.h b/libavutil/executor.h index c602bcb613..0eb21c10c8 100644 --- a/libavutil/executor.h +++ b/libavutil/executor.h @@ -46,7 +46,7 @@ typedef struct AVTaskCallbacks { /** * Alloc executor * @param callbacks callback structure for executor - * @param thread_count worker thread number + * @param thread_count worker thread number, 0 for run on caller's thread directly * @return return the executor */ AVExecutor* av_executor_alloc(const AVTaskCallbacks *callbacks, int thread_count); -- 2.42.0 _______________________________________________ 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".