From patchwork Mon Aug 12 12:46:31 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 1785 Return-Path: Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 8487D61948 for ; Mon, 12 Aug 2019 14:46:51 +0200 (CEST) Received: from pendragon.bb.dnainternet.fi (dfj612yhrgyx302h3jwwy-3.rev.dnainternet.fi [IPv6:2001:14ba:21f5:5b00:ce28:277f:58d7:3ca4]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 2532F594 for ; Mon, 12 Aug 2019 14:46:51 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1565614011; bh=FIDTwUk9LGgahplfudWCBnnAVDIp1mC/B2/Ibk8v/Pk=; h=From:To:Subject:Date:In-Reply-To:References:From; b=UCg+Fasa3YhVGjUZEH/oVG+Qo2jU5HqmShYiEPSCp0vTvZte3vfj9eP9nVEQxkbnd rA7VysMs7xhDG6Y7cVGlvC6WmVb/iFgLp1+aU7M+1WA+bluhwR9GWq0EIyuLduIgKI yC5nYvpRp4Yu+lGqmmDYba2ibyhNRYxlyN8R+xhg= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Mon, 12 Aug 2019 15:46:31 +0300 Message-Id: <20190812124642.24287-8-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190812124642.24287-1-laurent.pinchart@ideasonboard.com> References: <20190812124642.24287-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 07/18] libcamera: object: Notify objects of thread move X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 12 Aug 2019 12:46:53 -0000 Send a synchronous message to objects just before they get moved to a new thread. This allows the object to perform any required processing. EventNotifier and Timer objects will use this mechanism to move themselves to the new thread's event disaptcher. Signed-off-by: Laurent Pinchart Reviewed-by: Niklas Söderlund --- include/libcamera/object.h | 3 +++ src/libcamera/include/message.h | 1 + src/libcamera/message.cpp | 2 ++ src/libcamera/object.cpp | 26 ++++++++++++++++++++++++++ 4 files changed, 32 insertions(+) diff --git a/include/libcamera/object.h b/include/libcamera/object.h index 869200a57d8c..14b939a9bd3d 100644 --- a/include/libcamera/object.h +++ b/include/libcamera/object.h @@ -27,6 +27,7 @@ public: virtual ~Object(); void postMessage(std::unique_ptr msg); + void sendMessage(Message *msg); template::value>::type * = nullptr> void invokeMethod(void (T::*func)(Args...), Args... args) @@ -52,6 +53,8 @@ private: void invokeMethod(BoundMethodBase *method, void *pack); + void notifyThreadMove(); + void connect(SignalBase *signal); void disconnect(SignalBase *signal); diff --git a/src/libcamera/include/message.h b/src/libcamera/include/message.h index 92717e316cc3..1cfde5669ede 100644 --- a/src/libcamera/include/message.h +++ b/src/libcamera/include/message.h @@ -23,6 +23,7 @@ public: enum Type { None = 0, InvokeMessage = 1, + ThreadMoveMessage = 2, UserMessage = 1000, }; diff --git a/src/libcamera/message.cpp b/src/libcamera/message.cpp index f6c39d40fc73..efafb655c17e 100644 --- a/src/libcamera/message.cpp +++ b/src/libcamera/message.cpp @@ -47,6 +47,8 @@ std::atomic_uint Message::nextUserType_{ Message::UserMessage }; * \brief Invalid message type * \var Message::InvokeMessage * \brief Asynchronous method invocation across threads + * \var Message::ThreadMoveMessage + * \brief Object is being moved to a different thread * \var Message::UserMessage * \brief First value available for user-defined messages */ diff --git a/src/libcamera/object.cpp b/src/libcamera/object.cpp index 7d70ce21b5d0..7c68ec01f78c 100644 --- a/src/libcamera/object.cpp +++ b/src/libcamera/object.cpp @@ -74,6 +74,20 @@ void Object::postMessage(std::unique_ptr msg) thread()->postMessage(std::move(msg), this); } +/** + * \brief Send a message directly to the object + * \param[in] msg The message + * + * This method sends the message \a msg directly to the object, delivering it + * synchronously through the message() method in the current thread. Message + * ownership is not passed to the method, and the caller shall delete the + * message after this call. + */ +void Object::sendMessage(Message *msg) +{ + message(msg); +} + /** * \brief Message handler for the object * \param[in] msg The message @@ -135,6 +149,10 @@ void Object::invokeMethod(BoundMethodBase *method, void *args) * This method moves the object from the current thread to the new \a thread. * It shall be called from the thread in which the object currently lives, * otherwise the behaviour is undefined. + * + * Before the object is moved, a Message::ThreadMoveMessage message is sent to + * it. The message() method can be reimplement in derived classes to be notified + * of the upcoming thread move and perform any required processing. */ void Object::moveToThread(Thread *thread) { @@ -143,9 +161,17 @@ void Object::moveToThread(Thread *thread) if (thread_ == thread) return; + notifyThreadMove(); + thread->moveObject(this); } +void Object::notifyThreadMove() +{ + Message msg(Message::ThreadMoveMessage); + sendMessage(&msg); +} + void Object::connect(SignalBase *signal) { signals_.push_back(signal);