Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: Raja Rathour via ffmpeg-devel <ffmpeg-devel@ffmpeg.org>
To: ffmpeg-devel@ffmpeg.org
Cc: guoyejun@gmail.com, Raja Rathour <imraja729@gmail.com>
Subject: [FFmpeg-devel] [PATCH v2 1/3] avfilter/dnn_backend_torch: add async infrastructure
Date: Thu,  1 Jan 2026 13:37:20 +0530
Message-ID: <20260101080722.133215-2-imraja729@gmail.com> (raw)
In-Reply-To: <20260101080722.133215-1-imraja729@gmail.com>

Add worker thread, mutex, and condition variable to THModel.
---
 libavfilter/dnn/dnn_backend_torch.cpp | 31 +++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)

diff --git a/libavfilter/dnn/dnn_backend_torch.cpp b/libavfilter/dnn/dnn_backend_torch.cpp
index 2e4326d9d4..af92cd13da 100644
--- a/libavfilter/dnn/dnn_backend_torch.cpp
+++ b/libavfilter/dnn/dnn_backend_torch.cpp
@@ -25,6 +25,10 @@
 
 #include <torch/torch.h>
 #include <torch/script.h>
+#include <thread>
+#include <mutex>
+#include <condition_variable>
+#include <atomic>
 
 extern "C" {
 #include "dnn_io_proc.h"
@@ -42,6 +46,11 @@ typedef struct THModel {
     SafeQueue *request_queue;
     Queue *task_queue;
     Queue *lltask_queue;
+    SafeQueue *pending_queue;       ///< requests waiting for inference
+    std::thread *worker_thread;     ///< background worker thread
+    std::mutex *mutex;              ///< mutex for the condition variable
+    std::condition_variable *cond;  ///< condition variable for worker wakeup
+    std::atomic<bool> worker_stop;  ///< signal for thread exit
 } THModel;
 
 typedef struct THInferRequest {
@@ -318,6 +327,28 @@ err:
     }
 }
 
+static void th_worker_thread(THModel *th_model) {
+    while (true) {
+        THRequestItem *request = NULL;
+        {
+            std::unique_lock<std::mutex> lock(*th_model->mutex);
+            th_model->cond->wait(lock, [&]{
+                return th_model->worker_stop || ff_safe_queue_size(th_model->pending_queue) > 0;
+            });
+
+            if (th_model->worker_stop && ff_safe_queue_size(th_model->pending_queue) == 0)
+                break;
+
+            request = (THRequestItem *)ff_safe_queue_pop_front(th_model->pending_queue);
+        }
+        
+        if (request) {
+            th_start_inference(request);
+            infer_completion_callback(request);
+        }
+    }
+}
+
 static int execute_model_th(THRequestItem *request, Queue *lltask_queue)
 {
     THModel *th_model = NULL;
-- 
2.48.1

_______________________________________________
ffmpeg-devel mailing list -- ffmpeg-devel@ffmpeg.org
To unsubscribe send an email to ffmpeg-devel-leave@ffmpeg.org

  reply	other threads:[~2026-01-01  8:10 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-12-25 10:33 [FFmpeg-devel] [PATCH 1/3] avfilter/dnn_backend_torch: implement async execution Raja Rathour via ffmpeg-devel
2025-12-31  6:41 ` [FFmpeg-devel] " Guo, Yejun via ffmpeg-devel
2026-01-01  8:07   ` [FFmpeg-devel] [PATCH v2 0/3] avfilter/dnn: add async support for LibTorch backend Raja Rathour via ffmpeg-devel
2026-01-01  8:07     ` Raja Rathour via ffmpeg-devel [this message]
2026-01-01  8:07     ` [FFmpeg-devel] [PATCH v2 2/3] avfilter/dnn_backend_torch: implement async execution logic Raja Rathour via ffmpeg-devel
2026-01-01  8:07     ` [FFmpeg-devel] [PATCH v2 3/3] avfilter/dnn_backend_torch: handle lifecycle of worker thread Raja Rathour via ffmpeg-devel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260101080722.133215-2-imraja729@gmail.com \
    --to=ffmpeg-devel@ffmpeg.org \
    --cc=guoyejun@gmail.com \
    --cc=imraja729@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

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