From patchwork Mon Feb 24 12:08:54 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Milan Zamazal X-Patchwork-Id: 22845 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id D6A81C32C2 for ; Mon, 24 Feb 2025 12:09:24 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 53ACC686F9; Mon, 24 Feb 2025 13:09:24 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="K6v5HW2C"; dkim-atps=neutral Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id B0027686F4 for ; Mon, 24 Feb 2025 13:09:22 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1740398961; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=vommuU7mKkaVRfL0lYXIdRZQCLVVNLDOpQA66iiz7rE=; b=K6v5HW2CmRJrgG47jyrId1z3+RK6p6PT2PJBV1gMbnr9cqR4cRyQYPHcwE9kdrjYNvG1mk Xq77YPSFUrR9xN0WgTXWS8CIVRF3rKMUsxsJx6puQs8/kLq34yWqFNyrNyjhjokf4i/jxZ LNlBcKVXHzbzyjVGGavMx9zB+WF1E7k= Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-336-RqpnhuoyOBuEIe21BMOOLA-1; Mon, 24 Feb 2025 07:09:18 -0500 X-MC-Unique: RqpnhuoyOBuEIe21BMOOLA-1 X-Mimecast-MFC-AGG-ID: RqpnhuoyOBuEIe21BMOOLA_1740398957 Received: from mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.40]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 6AECB1800875; Mon, 24 Feb 2025 12:09:17 +0000 (UTC) Received: from mzamazal-thinkpadp1gen7.tpbc.com (unknown [10.45.225.60]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 816B819560AB; Mon, 24 Feb 2025 12:09:15 +0000 (UTC) From: Milan Zamazal To: libcamera-devel@lists.libcamera.org Cc: Milan Zamazal , Stanislaw Gruszka , Laurent Pinchart , Kieran Bingham Subject: [PATCH 5/5] libcamera: software_isp: Modify dispatching messages on stop Date: Mon, 24 Feb 2025 13:08:54 +0100 Message-ID: <20250224120854.19747-6-mzamazal@redhat.com> In-Reply-To: <20250224120854.19747-1-mzamazal@redhat.com> References: <20250224120854.19747-1-mzamazal@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.40 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: 86FtDa__4BGjze9YI5SFsh9ajcRePAiR-iexkDtN-ec_1740398957 X-Mimecast-Originator: redhat.com content-type: text/plain; charset="US-ASCII"; x-default=true X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" There may be pending messages in SoftwareIsp message queue when SoftwareIsp stops. The call to IPAProxySoft::stop() will dispatch them before SoftwareIsp::stop() finishes. But this is dependent on IPAProxySoft::stop() implementation and we can do better to ensure they are dispatched before SoftwareIsp::stop() finishes. Let's introduce new `receiver' argument to Thread::dispatchMessages(), limiting dispatching the messages to the given receiver. Now we can flush the messages destined for the SoftwareIsp instance explicitly. And the IPA proxy can flush just the messages destined for itself. Other messages of the given thread remain queued and will be handled elsewhere as appropriate. Signed-off-by: Milan Zamazal --- include/libcamera/base/thread.h | 3 ++- src/libcamera/base/thread.cpp | 14 +++++++++----- src/libcamera/software_isp/software_isp.cpp | 3 +++ .../libcamera_templates/proxy_functions.tmpl | 2 +- 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/include/libcamera/base/thread.h b/include/libcamera/base/thread.h index 3cbf6398..b9284c2c 100644 --- a/include/libcamera/base/thread.h +++ b/include/libcamera/base/thread.h @@ -48,7 +48,8 @@ public: EventDispatcher *eventDispatcher(); - void dispatchMessages(Message::Type type = Message::Type::None); + void dispatchMessages(Message::Type type = Message::Type::None, + Object *receiver = nullptr); protected: int exec(); diff --git a/src/libcamera/base/thread.cpp b/src/libcamera/base/thread.cpp index 02128f23..2fde2959 100644 --- a/src/libcamera/base/thread.cpp +++ b/src/libcamera/base/thread.cpp @@ -603,6 +603,8 @@ void Thread::removeMessages(Object *receiver) /** * \brief Dispatch posted messages for this thread * \param[in] type The message type + * \param[in] receiver If not null, dispatch only messages for the given + * receiver * * This function immediately dispatches all the messages previously posted for * this thread with postMessage() that match the message \a type. If the \a type @@ -616,7 +618,7 @@ void Thread::removeMessages(Object *receiver) * same thread from an object's message handler. It guarantees delivery of * messages in the order they have been posted in all cases. */ -void Thread::dispatchMessages(Message::Type type) +void Thread::dispatchMessages(Message::Type type, Object *receiver) { ASSERT(data_ == ThreadData::current()); @@ -640,12 +642,14 @@ void Thread::dispatchMessages(Message::Type type) */ std::unique_ptr message = std::move(msg); - Object *receiver = message->receiver_; - ASSERT(data_ == receiver->thread()->data_); - receiver->pendingMessages_--; + Object *messageReceiver = message->receiver_; + if (receiver && receiver != messageReceiver) + continue; + ASSERT(data_ == messageReceiver->thread()->data_); + messageReceiver->pendingMessages_--; locker.unlock(); - receiver->message(message.get()); + messageReceiver->message(message.get()); message.reset(); locker.lock(); } diff --git a/src/libcamera/software_isp/software_isp.cpp b/src/libcamera/software_isp/software_isp.cpp index 3a605ab2..8f5ee774 100644 --- a/src/libcamera/software_isp/software_isp.cpp +++ b/src/libcamera/software_isp/software_isp.cpp @@ -13,6 +13,8 @@ #include #include +#include + #include #include #include @@ -339,6 +341,7 @@ void SoftwareIsp::stop() ispWorkerThread_.wait(); running_ = false; + Thread::current()->dispatchMessages(Message::Type::InvokeMessage, this); ipa_->stop(); for (auto buffer : queuedOutputBuffers_) { diff --git a/utils/codegen/ipc/generators/libcamera_templates/proxy_functions.tmpl b/utils/codegen/ipc/generators/libcamera_templates/proxy_functions.tmpl index b5797b14..25476990 100644 --- a/utils/codegen/ipc/generators/libcamera_templates/proxy_functions.tmpl +++ b/utils/codegen/ipc/generators/libcamera_templates/proxy_functions.tmpl @@ -34,7 +34,7 @@ thread_.exit(); thread_.wait(); - Thread::current()->dispatchMessages(Message::Type::InvokeMessage); + Thread::current()->dispatchMessages(Message::Type::InvokeMessage, this); state_ = ProxyStopped; {%- endmacro -%}