From patchwork Fri Jul 31 10:53:41 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Umang Jain X-Patchwork-Id: 9083 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 761FBBD86F for ; Fri, 31 Jul 2020 10:53:45 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id B5E5161DD8; Fri, 31 Jul 2020 12:53:44 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=uajain.com header.i=@uajain.com header.b="N2tiQ0MU"; dkim-atps=neutral Received: from o1.f.az.sendgrid.net (o1.f.az.sendgrid.net [208.117.55.132]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id C1E4060396 for ; Fri, 31 Jul 2020 12:53:42 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=uajain.com; h=from:subject:in-reply-to:references:mime-version:to:cc: content-transfer-encoding:content-type; s=s1; bh=bMNOnkEkTHpbNEBGYnD0orvQ1VUtKvvk0NQGieyHQHA=; b=N2tiQ0MUCGJ7NWGtRrofpBsRcUB8/QceckFKu/bnSgWqGyvwPJSH28XRzNfwXHKmQxvU SplbkgTS4xpyq1vGzys+qKfSZZ7wyOWIr+KOMBFRssmugd9rxzIkiaYJYscl95zDsWpuh2 ICRn/DJYCetXoU9tIFkGt36Amm/sVEKfU= Received: by filterdrecv-p3mdw1-7ff865655c-g882x with SMTP id filterdrecv-p3mdw1-7ff865655c-g882x-20-5F23F835-1 2020-07-31 10:53:41.138588816 +0000 UTC m=+146250.616757875 Received: from mail.uajain.com (unknown) by ismtpd0004p1hnd1.sendgrid.net (SG) with ESMTP id BWbqALB4RgC8qD5ODvWMTA for ; Fri, 31 Jul 2020 10:53:40.725 +0000 (UTC) From: Umang Jain Date: Fri, 31 Jul 2020 10:53:41 +0000 (UTC) Message-Id: <20200731105335.62014-2-email@uajain.com> In-Reply-To: <20200731105335.62014-1-email@uajain.com> References: <20200731105335.62014-1-email@uajain.com> Mime-Version: 1.0 X-SG-EID: 1Q40EQ7YGir8a9gjSIAdTjhngY657NMk9ckeo4dbHZDiOpywc/L3L9rFqlwE4KPcofpXQ1KwJPJayd5tGsU1eL8pT6L5XI068+X2BjFsmbU12v7JkpGTHOEJUQjc1upjdOPJ81lysMBHJkG1DANMDVGdCJzM3BkM9m2bkrfHzYmQuH0hngP3RhjlZHRa7NXZiHEUQo0a9R+kMNa2sttZ40V6npMTrTxkX9O5/ODAFxkiJ37InOB5O7WRIz4jnoyQyAceSy+WBh1G6StX1+bhzg== To: libcamera-devel@lists.libcamera.org Subject: [libcamera-devel] [PATCH v2 1/3] libcamera: thread: Support selective message dispatch to thread 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" Extend the current dispatchMessages() to support dispatching of selective messsages according to the Message::Type passed in the function argument. dispatchMessages() can now be called explicitly to force deliver selected type's message to the thread for processing (typically when event loop is not running). Add a helper Message::Type::CatchAll message type to deliver every message posted to the thread. Signed-off-by: Umang Jain --- include/libcamera/internal/message.h | 1 + include/libcamera/internal/thread.h | 3 +- src/libcamera/event_dispatcher_poll.cpp | 2 +- src/libcamera/message.cpp | 2 ++ src/libcamera/thread.cpp | 47 ++++++++++++++++++------- 5 files changed, 40 insertions(+), 15 deletions(-) diff --git a/include/libcamera/internal/message.h b/include/libcamera/internal/message.h index 92ea64a..b8d9866 100644 --- a/include/libcamera/internal/message.h +++ b/include/libcamera/internal/message.h @@ -25,6 +25,7 @@ public: None = 0, InvokeMessage = 1, ThreadMoveMessage = 2, + CatchAll = 999, UserMessage = 1000, }; diff --git a/include/libcamera/internal/thread.h b/include/libcamera/internal/thread.h index 7b59e58..1dfeb72 100644 --- a/include/libcamera/internal/thread.h +++ b/include/libcamera/internal/thread.h @@ -14,6 +14,7 @@ #include +#include "libcamera/internal/message.h" #include "libcamera/internal/utils.h" namespace libcamera { @@ -47,7 +48,7 @@ public: EventDispatcher *eventDispatcher(); void setEventDispatcher(std::unique_ptr dispatcher); - void dispatchMessages(); + void dispatchMessages(Message::Type type); protected: int exec(); diff --git a/src/libcamera/event_dispatcher_poll.cpp b/src/libcamera/event_dispatcher_poll.cpp index 9ab85da..b9fabf8 100644 --- a/src/libcamera/event_dispatcher_poll.cpp +++ b/src/libcamera/event_dispatcher_poll.cpp @@ -146,7 +146,7 @@ void EventDispatcherPoll::processEvents() { int ret; - Thread::current()->dispatchMessages(); + Thread::current()->dispatchMessages(Message::Type::CatchAll); /* Create the pollfd array. */ std::vector pollfds; diff --git a/src/libcamera/message.cpp b/src/libcamera/message.cpp index e9b3e73..e462f90 100644 --- a/src/libcamera/message.cpp +++ b/src/libcamera/message.cpp @@ -49,6 +49,8 @@ std::atomic_uint Message::nextUserType_{ Message::UserMessage }; * \brief Asynchronous method invocation across threads * \var Message::ThreadMoveMessage * \brief Object is being moved to a different thread + * \var Message::CatchAll + * \brief Helper to match message of any type * \var Message::UserMessage * \brief First value available for user-defined messages */ diff --git a/src/libcamera/thread.cpp b/src/libcamera/thread.cpp index d1750d7..d3a5f81 100644 --- a/src/libcamera/thread.cpp +++ b/src/libcamera/thread.cpp @@ -552,26 +552,47 @@ void Thread::removeMessages(Object *receiver) } /** - * \brief Dispatch all posted messages for this thread + * \brief Dispatch posted messages for this thread as per \a type Message::Type. + * Pass Message::Type::CatchAll to dispatch every posted message posted for + * this thread. */ -void Thread::dispatchMessages() +void Thread::dispatchMessages(Message::Type type) { MutexLocker locker(data_->messages_.mutex_); - while (!data_->messages_.list_.empty()) { - std::unique_ptr msg = std::move(data_->messages_.list_.front()); - data_->messages_.list_.pop_front(); - if (!msg) - continue; + if (type == Message::Type::CatchAll) { + while (!data_->messages_.list_.empty()) { + std::unique_ptr msg = + std::move(data_->messages_.list_.front()); + data_->messages_.list_.pop_front(); + if (!msg) + continue; - Object *receiver = msg->receiver_; - ASSERT(data_ == receiver->thread()->data_); + Object *receiver = msg->receiver_; + ASSERT(data_ == receiver->thread()->data_); - receiver->pendingMessages_--; + receiver->pendingMessages_--; - locker.unlock(); - receiver->message(msg.get()); - locker.lock(); + locker.unlock(); + receiver->message(msg.get()); + locker.lock(); + } + } else { + for (std::unique_ptr &msg : data_->messages_.list_) { + if (!msg) + continue; + + if (msg->type() == type) { + std::unique_ptr message = std::move(msg); + Object *receiver = message->receiver_; + ASSERT(data_ == receiver->thread()->data_); + receiver->pendingMessages_--; + + locker.unlock(); + receiver->message(message.get()); + locker.lock(); + } + } } }