From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.ffmpeg.org (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTPS id EC4D54BD2A for ; Thu, 1 Jan 2026 08:11:32 +0000 (UTC) Authentication-Results: ffbox; dkim=fail (body hash mismatch (got b'KMzHyUFZJNY4sRJ0Qjy2vglsmn7HCpNRBY2COwmO3ZU=', expected b'RnC0m89l2nbsAsvl/VDdXmyvGndwM7hdWEU3/YXW9Hk=')) header.d=gmail.com header.a=rsa-sha256 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ffmpeg.org; i=@ffmpeg.org; q=dns/txt; s=mail; t=1767254988; h=to : date : message-id : in-reply-to : references : mime-version : reply-to : subject : list-id : list-archive : list-archive : list-help : list-owner : list-post : list-subscribe : list-unsubscribe : from : cc : content-type : content-transfer-encoding : from; bh=dBY4/3alLcRrvx6FATTOzYFZH6YP36gQb54WVPAF+E0=; b=bRF4tuvHWk1R+62LmaXiaK7cQR0hrjpOqx/Zp4u8td0Ptcr+wedtVB3lB8weNrf4fSbHQ xO/+74U5cbK0MkXEmcox445a/rxISGtYhRZQvEOInrX/DE2PDO5jqZR0D+eSgZCLRxQ/1Po /haA8BIEKet7fGfQpat2IrhNhT65AImePAuoi6Ok7fUK2NC2A5I3xcmJBX7MvRUtJVD54DR BQcA8eE47WzMV31+fgaXgyGFguVHYoWnL6sxG3zMDr2iUY3ZphwL2WdTXnKIt54L2f+ijmy eF7Cu5I8JFpy4g8zZbEWbZWzRM/OPS51yezaVrROxfkAG7g0popxs4xW8/Bw== Received: from [172.20.0.4] (unknown [172.20.0.4]) by ffbox0-bg.ffmpeg.org (Postfix) with ESMTP id 8DC77690D37; Thu, 1 Jan 2026 10:09:48 +0200 (EET) ARC-Seal: i=1; cv=none; a=rsa-sha256; d=ffmpeg.org; s=arc; t=1767254964; b=BAsRQa0PX/gnAKmGIi+XYk3PIF4zVNCY0T6k4wKpNn2uq1AJaIgQglgwfqGGRNcwg5snl GUSXbbV9vL4WAiR8UJxvSfN3rU/bwBUiJ/byKVX3IG96DuniILaYfgrhTO06hhnh+NetQz+ bpYt73FgYZV7AfWaMDHV85VQmI3ImDngKWDOjcBWmsNrP3D6N2/LBwKZRm2VD4kVKTujEiF +EVvBwCl6rtQE08pvn7rW+BUcG2ZtBsHOpcl5opjoJUaE6djIiTnUGSkHhEA4NBpwN/OEsd rKwob5kKcXE22Tauu9ZPpmrfPrZhAINe0Obzv6HZodW0lX49KDLrrjTxJbag== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=ffmpeg.org; s=arc; t=1767254964; h=from : sender : reply-to : subject : date : message-id : to : cc : mime-version : content-type : content-transfer-encoding : content-id : content-description : resent-date : resent-from : resent-sender : resent-to : resent-cc : resent-message-id : in-reply-to : references : list-id : list-help : list-unsubscribe : list-subscribe : list-post : list-owner : list-archive; bh=KMzHyUFZJNY4sRJ0Qjy2vglsmn7HCpNRBY2COwmO3ZU=; b=Ky/laqB+ZIN0PuOdL+VF73GfLeZr/Q9PkA1unA7fbCA16VpRcs71L4dxT8YsNZlESWoxW coWaBMxLTTREa/nLSjk9Q1HHfdubEm4nCpMxW6Tqc8J9pynPHEZ9vggHxJKGJAsWgEYYN8+ PDRxVyDcciqykfeo8zAH10vVVe2Hlz+W4jWFE8BLW2CqAC6a4zsXN1oBtMi9p9vxYj7PyA3 77oxUCfOMI8YT6ciyrMDi5r80W/9rCXlW3dzXKkLmv54Y6qHnDYjINdRnqaVzwZszEiZmZO tqE427xZnzeNhbeFM95h5bhNZTgXePO0L0XtLiJlPsiA6V6zmGWf5Pu4PQJg== ARC-Authentication-Results: i=1; ffmpeg.org; dkim=pass header.d=gmail.com; arc=none; dmarc=pass header.from=gmail.com policy.dmarc=quarantine Authentication-Results: ffmpeg.org; dkim=pass header.d=gmail.com; arc=none (Message is not ARC signed); dmarc=pass (Used From Domain Record) header.from=gmail.com policy.dmarc=quarantine Received: from mail-pf1-f182.google.com (mail-pf1-f182.google.com [209.85.210.182]) by ffbox0-bg.ffmpeg.org (Postfix) with ESMTPS id 43AB4690D0E for ; Thu, 1 Jan 2026 10:08:02 +0200 (EET) Received: by mail-pf1-f182.google.com with SMTP id d2e1a72fcca58-7f1243792f2so7595525b3a.1 for ; Thu, 01 Jan 2026 00:08:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1767254879; x=1767859679; darn=ffmpeg.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=RnC0m89l2nbsAsvl/VDdXmyvGndwM7hdWEU3/YXW9Hk=; b=Yfr5RxiZZBEhNdAQZmb/DdxjH3H/2tiuD4mttHJ/tBPGmh+OH3wN3u5+hxRXwy1jI+ eeSqh2KTIebIi2xXJqMt18Mrpds4hm1D2Pnuvt+01AKccbdzC6fjTK+qRnTbJmjMRHxt rSp3wPBkZHUhHWKuMhexlyiDUjo7/JluxLfyIAJyOUIW/v/uYOqR2ejWhyHFcJjMtpEA lT6ljdhYY/1W4gAyHqkBOEWFdK1B/F7ghRU3fSh7MZeccHMEWRJmnCTQgpZW1/0BuX5d VspAuJ4MqdfSaAtPqOmC64Cr7DSygqMm+iQJV6MSMjvDdXX0qfUw0WNkG6ntP/j+drT/ Ucuw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1767254879; x=1767859679; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=RnC0m89l2nbsAsvl/VDdXmyvGndwM7hdWEU3/YXW9Hk=; b=RLuMavCQe2Z85yhol0kxsIusf7GGwvgAflC/yzmzJXV1hNdzwWdCSEjNy63OX740pM MUkX3E25zWiCbC1OPCA0KM4ZthqKm6+Wh+mlQHTcsxK3OkUv0D+R8TK1nfJWIDe9IFcz 2byHidlJ7sLn/W80ZvRvYppKwg471FgzzyoCPrmZDWtHKez0JGN70W/xdkbV8UgSz+FM CoRN7oI+ZjLA68SFAUZOksd7jk+mLHkv+6qNvW7Te066hlM2Fnn+M1nf+EpUKGmxfOLH 02KiZ8BHXj9d+8fNziMaqCckrFJaKDnc4Fo9zEMMMx/bN+3BcISw1QCB9FmA92M6C5MG GybA== X-Gm-Message-State: AOJu0YzNinxXQbix3OenJGNW7DB7ta5Ph7LVoyRqj4r2uXEnWQ8FGPei fzcY3eRyulbHEuRU3IQAE/qW+8p+arGr/mM/I9EY0iifatUinzUfaeNaca0Hkg== X-Gm-Gg: AY/fxX5y2PIYVFrldO5pGvTzHfr9aUUTCLUmgxO2wMieZw/AdkJQTOljoLjWDN6gtWl DS6q5jEStAEHWZ6SBZu6/EAP1HUrI29TyPSsumPYC0Dgd0CI2fq/LNE7aloUIEUsSFf1aTeXP0T 4XwklhWmyuIdhi6hpRI3aMtIJa1oOAw4cU8mXABd7TE8NmkujiFbctW8LkXSz5F7UBu5wi+bJgK DJpFLqHNxb/V+ibA/ErArF+Fz1MTNhfgLR6TkHq7LKrE9j07KJaeo4seS7UkuSg3PYf2i4VzMWY hTpLj9qXTKjtMLQn5lQicFachVvDlwVN6o5kBtVsAeHFw82YzkQsrgRegQL9w8UIST609EWSwn2 nsHmeDwLwat0bHxiMLKVAHQ3fzi2CmmfG9nLPprzhQpjevIHZ+19loGgcUi3JQA1wIKpk5TF/GS 1rnHxpDknHQ92z9fRvKIMqaA7E6TExgHTPymI8am4X X-Google-Smtp-Source: AGHT+IHexfmJz58IMfDVdqM4XjnK8Z9JF9vxqg3W5zNx5EGnfPa6mBqPkvx0eCRd/aZ3sah2fnfX6w== X-Received: by 2002:a05:6a00:4e08:b0:7f6:2e6e:5289 with SMTP id d2e1a72fcca58-7ff67257b92mr25512837b3a.49.1767254878805; Thu, 01 Jan 2026 00:07:58 -0800 (PST) Received: from Raja-Rathour-ASUS-TUF-Gaming-A15.. ([103.227.71.253]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7ff7b127b00sm37251304b3a.21.2026.01.01.00.07.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Jan 2026 00:07:58 -0800 (PST) To: ffmpeg-devel@ffmpeg.org Date: Thu, 1 Jan 2026 13:37:22 +0530 Message-ID: <20260101080722.133215-4-imraja729@gmail.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20260101080722.133215-1-imraja729@gmail.com> References: <20260101080722.133215-1-imraja729@gmail.com> MIME-Version: 1.0 Message-ID-Hash: D6C5E7Y3C6XO42MA4NRUXIYM66MW3AJ3 X-Message-ID-Hash: D6C5E7Y3C6XO42MA4NRUXIYM66MW3AJ3 X-MailFrom: SRS0=om1i=7G=gmail.com=imraja729@ffmpeg.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; header-match-ffmpeg-devel.ffmpeg.org-0; header-match-ffmpeg-devel.ffmpeg.org-1; header-match-ffmpeg-devel.ffmpeg.org-2; header-match-ffmpeg-devel.ffmpeg.org-3; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list Reply-To: FFmpeg development discussions and patches Subject: [FFmpeg-devel] [PATCH v2 3/3] avfilter/dnn_backend_torch: handle lifecycle of worker thread List-Id: FFmpeg development discussions and patches Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: From: Raja Rathour via ffmpeg-devel Cc: guoyejun@gmail.com, Raja Rathour Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Archived-At: List-Archive: List-Post: Initialize async resources in dnn_load_model_th and ensure proper cleanup and thread joining in dnn_free_model_th. Signed-off-by: Raja Rathour --- libavfilter/dnn/dnn_backend_torch.cpp | 85 ++++++++++++++++++++++----- 1 file changed, 69 insertions(+), 16 deletions(-) diff --git a/libavfilter/dnn/dnn_backend_torch.cpp b/libavfilter/dnn/dnn_backend_torch.cpp index b5ff5b44d1..f384999152 100644 --- a/libavfilter/dnn/dnn_backend_torch.cpp +++ b/libavfilter/dnn/dnn_backend_torch.cpp @@ -127,27 +127,70 @@ static void dnn_free_model_th(DNNModel **model) if (!model || !*model) return; - th_model = (THModel *) (*model); - while (ff_safe_queue_size(th_model->request_queue) != 0) { - THRequestItem *item = (THRequestItem *)ff_safe_queue_pop_front(th_model->request_queue); - destroy_request_item(&item); + th_model = (THModel *)(*model); + + /* 1. Stop and join the worker thread if it exists */ + if (th_model->worker_thread) { + { + std::lock_guard lock(*th_model->mutex); + th_model->worker_stop = true; + } + th_model->cond->notify_all(); + th_model->worker_thread->join(); + delete th_model->worker_thread; + th_model->worker_thread = NULL; } - ff_safe_queue_destroy(th_model->request_queue); - while (ff_queue_size(th_model->lltask_queue) != 0) { - LastLevelTaskItem *item = (LastLevelTaskItem *)ff_queue_pop_front(th_model->lltask_queue); - av_freep(&item); + /* 2. Safely delete C++ synchronization objects */ + if (th_model->mutex) { + delete th_model->mutex; + th_model->mutex = NULL; + } + if (th_model->cond) { + delete th_model->cond; + th_model->cond = NULL; } - ff_queue_destroy(th_model->lltask_queue); - while (ff_queue_size(th_model->task_queue) != 0) { - TaskItem *item = (TaskItem *)ff_queue_pop_front(th_model->task_queue); - av_frame_free(&item->in_frame); - av_frame_free(&item->out_frame); - av_freep(&item); + /* 3. Clean up the pending queue */ + if (th_model->pending_queue) { + while (ff_safe_queue_size(th_model->pending_queue) > 0) { + THRequestItem *item = (THRequestItem *)ff_safe_queue_pop_front(th_model->pending_queue); + destroy_request_item(&item); + } + ff_safe_queue_destroy(th_model->pending_queue); + } + + /* 4. Clean up standard backend queues */ + if (th_model->request_queue) { + while (ff_safe_queue_size(th_model->request_queue) != 0) { + THRequestItem *item = (THRequestItem *)ff_safe_queue_pop_front(th_model->request_queue); + destroy_request_item(&item); + } + ff_safe_queue_destroy(th_model->request_queue); } - ff_queue_destroy(th_model->task_queue); - delete th_model->jit_model; + + if (th_model->lltask_queue) { + while (ff_queue_size(th_model->lltask_queue) != 0) { + LastLevelTaskItem *item = (LastLevelTaskItem *)ff_queue_pop_front(th_model->lltask_queue); + av_freep(&item); + } + ff_queue_destroy(th_model->lltask_queue); + } + + if (th_model->task_queue) { + while (ff_queue_size(th_model->task_queue) != 0) { + TaskItem *item = (TaskItem *)ff_queue_pop_front(th_model->task_queue); + av_frame_free(&item->in_frame); + av_frame_free(&item->out_frame); + av_freep(&item); + } + ff_queue_destroy(th_model->task_queue); + } + + /* 5. Final model cleanup */ + if (th_model->jit_model) + delete th_model->jit_model; + av_freep(&th_model); *model = NULL; } @@ -513,6 +556,16 @@ static DNNModel *dnn_load_model_th(DnnContext *ctx, DNNFunctionType func_type, A goto fail; } + th_model->pending_queue = ff_safe_queue_create(); + if (!th_model->pending_queue) { + goto fail; + } + + th_model->mutex = new std::mutex(); + th_model->cond = new std::condition_variable(); + th_model->worker_stop = false; + th_model->worker_thread = new std::thread(th_worker_thread, th_model); + model->get_input = &get_input_th; model->get_output = &get_output_th; model->filter_ctx = filter_ctx; -- 2.48.1 _______________________________________________ ffmpeg-devel mailing list -- ffmpeg-devel@ffmpeg.org To unsubscribe send an email to ffmpeg-devel-leave@ffmpeg.org