From patchwork Mon Jun 24 04:47:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhao Zhili X-Patchwork-Id: 50109 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a59:ae71:0:b0:482:c625:d099 with SMTP id w17csp1785445vqz; Sun, 23 Jun 2024 21:48:25 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCVv0ZdgeRxyDktdfHFwqcqNgLhTWD/DjSaHR0kUOrzwa9soQigXW9rgEZ33UbFuSGkcr9QYOIZwdRrSU4Rvqv6cVbP9FX42srAkUw== X-Google-Smtp-Source: AGHT+IFXMcfQz209BgOo6FydUJIOwPGrlwjfpw9bCXibPs90PtaNP0rlaPYn2uGG57a6p0E6gZjb X-Received: by 2002:a17:907:cbc9:b0:a6f:b193:758e with SMTP id a640c23a62f3a-a70385e67dfmr354583166b.29.1719204504906; Sun, 23 Jun 2024 21:48:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1719204504; cv=none; d=google.com; s=arc-20160816; b=JVAQWuXO96eG5LcoOsE3ORXWlhAJEHHbLt68bQnv4NMPieGk9LFPPWVr1tT++YRWvX sKhLKr0I9Ps8AJeaaQ82qbnVeLdQEBa5TcxlfTLEABibyHQ3NNrBmuaHlAZwrDe2SHRe WP2yu36yptU2Ywz/NjmBQcJ0UDgA/qac2C4+peJhCKwGpbg8yf8BoaDjdh/pe3cdbYlc e81ra4/8P8l9aPuxKJDCnQ3Lj5jVKh7v1FG1C0QYiIIq5jhzrYqEBE5KPyUBAzzSIVTO hzdVrOgt4JS6F0Ll2INZ/uO6iGq0F2Syf6ch2wO4mruJvFs8nbgQvKnr+Vt6a6LXkT0T f80w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:date:to:from:message-id :dkim-signature:delivered-to; bh=uyltCGZIuW6SZ42hAymp6lOvfdn1M7aA8Nc/Bvtm7NI=; fh=HnHYuZ9XgUo86ZRXTLWWmQxhslYEI9B9taZ5X1DLFfc=; b=QHtwxEEtuUTLJnbQXu9+9ATSy+zWzHdRETT6wMiGz0byuKMrXHXWrSM+SgXtXNpHAk DZ2Nu9iWy5JBd3t+3ECI1I5pYxOUnAmalQ7UG2NjnXBxMLqgr6xstC+sN0BLjKYLvec/ rNHI5i3hw+XFMqRRDX1AYakjUaP2Ibr2/ojs+JdOKQkwcGSGqSt6+C0tDYt7NGLj4Z3a KhU5jZepDygVThWr2q/+TlesuQD7IknKA9Cfd+aTIU5NPI6QajSZv5zFyHQenLB72nuv BPUq4JMnjfBKMyqwnZlzxAKOtD2sWhl4n3N0tMRErk6Uo1yeERSKZlucPUq93eBujN4p qwpA==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@foxmail.com header.s=s201512 header.b=osF49eun; 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=NONE dis=NONE) header.from=foxmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id a640c23a62f3a-a724830180dsi136733366b.454.2024.06.23.21.48.24; Sun, 23 Jun 2024 21:48:24 -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=@foxmail.com header.s=s201512 header.b=osF49eun; 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=NONE dis=NONE) header.from=foxmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 5CDF968D51E; Mon, 24 Jun 2024 07:48:09 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: +2G9GeJ+TIFX 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);