Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
* [FFmpeg-devel] [PATCH 1/2] avutil/executor: Allowing thread_count be zero
@ 2024-06-17  5:19 Zhao Zhili
  2024-06-17  7:05 ` Anton Khirnov
  0 siblings, 1 reply; 15+ messages in thread
From: Zhao Zhili @ 2024-06-17  5:19 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Zhao Zhili

From: Zhao Zhili <zhilizhao@tencent.com>

When thread_count be zero, it will be run on current thread like
!HAVE_THREADS.
---
 libavutil/executor.c | 28 ++++++++++++++++++----------
 1 file changed, 18 insertions(+), 10 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 */;
+    }
 }
-- 
2.25.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".

^ permalink raw reply	[flat|nested] 15+ messages in thread
* [FFmpeg-devel] [PATCH 1/2] avutil/executor: Allowing thread_count be zero
@ 2024-06-24  4:47 Zhao Zhili
  2024-06-25 12:36 ` Nuo Mi
  0 siblings, 1 reply; 15+ messages in thread
From: Zhao Zhili @ 2024-06-24  4:47 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Zhao Zhili

From: Zhao Zhili <zhilizhao@tencent.com>

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 <zhilizhao@tencent.com>
---
 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".

^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2024-06-27 12:57 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-06-17  5:19 [FFmpeg-devel] [PATCH 1/2] avutil/executor: Allowing thread_count be zero Zhao Zhili
2024-06-17  7:05 ` Anton Khirnov
2024-06-17  7:21   ` Paul B Mahol
2024-06-17  8:02   ` Zhao Zhili
2024-06-17  8:45     ` Hendrik Leppkes
2024-06-17  9:27       ` Zhao Zhili
2024-06-18 11:50         ` Nuo Mi
2024-06-18 12:05           ` Steven Liu
2024-06-19  8:40             ` Nuo Mi
2024-06-19 10:39               ` Steven Liu
2024-06-18 14:23           ` Zhao Zhili
2024-06-19  8:51             ` Nuo Mi
2024-06-24  4:47 Zhao Zhili
2024-06-25 12:36 ` Nuo Mi
2024-06-27 12:57   ` Nuo Mi

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git