[libcamera-devel,v2,07/18] libcamera: object: Notify objects of thread move

Message ID 20190817152104.10834-8-laurent.pinchart@ideasonboard.com
State Accepted
Headers show
Series
  • Object & Thread enhancements
Related show

Commit Message

Laurent Pinchart Aug. 17, 2019, 3:20 p.m. UTC
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 <laurent.pinchart@ideasonboard.com>
Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>
Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
---
Changes since v1:

- Drop Object::sendMessage()
---
 include/libcamera/object.h      |  2 ++
 src/libcamera/include/message.h |  1 +
 src/libcamera/message.cpp       |  2 ++
 src/libcamera/object.cpp        | 12 ++++++++++++
 4 files changed, 17 insertions(+)

Patch

diff --git a/include/libcamera/object.h b/include/libcamera/object.h
index 869200a57d8c..e128c75e7db8 100644
--- a/include/libcamera/object.h
+++ b/include/libcamera/object.h
@@ -52,6 +52,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..bbb28f261405 100644
--- a/src/libcamera/object.cpp
+++ b/src/libcamera/object.cpp
@@ -135,6 +135,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 +147,17 @@  void Object::moveToThread(Thread *thread)
 	if (thread_ == thread)
 		return;
 
+	notifyThreadMove();
+
 	thread->moveObject(this);
 }
 
+void Object::notifyThreadMove()
+{
+	Message msg(Message::ThreadMoveMessage);
+	message(&msg);
+}
+
 void Object::connect(SignalBase *signal)
 {
 	signals_.push_back(signal);