[libcamera-devel,11/18] libcamera: event_notifier: Bind event notifiers to threads

Message ID 20190812124642.24287-12-laurent.pinchart@ideasonboard.com
State Superseded
Headers show
Series
  • Object & Thread enhancements
Related show

Commit Message

Laurent Pinchart Aug. 12, 2019, 12:46 p.m. UTC
The EventNotifier instances are registered with the event dispatcher
instance of the CameraManager. This makes it impossible to use event
notifiers in other threads.

Fix this by inheriting from Object, which allows binding instances to a
thread, and register them with the event dispatcher for the thread they
are bound to.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 include/libcamera/event_notifier.h |  8 +++++++-
 src/libcamera/event_notifier.cpp   | 17 ++++++++++++++++-
 2 files changed, 23 insertions(+), 2 deletions(-)

Comments

Niklas Söderlund Aug. 17, 2019, 3 p.m. UTC | #1
Hi Laurent,

Thanks for your work.

On 2019-08-12 15:46:35 +0300, Laurent Pinchart wrote:
> The EventNotifier instances are registered with the event dispatcher
> instance of the CameraManager. This makes it impossible to use event
> notifiers in other threads.
> 
> Fix this by inheriting from Object, which allows binding instances to a
> thread, and register them with the event dispatcher for the thread they
> are bound to.
> 
> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>

> ---
>  include/libcamera/event_notifier.h |  8 +++++++-
>  src/libcamera/event_notifier.cpp   | 17 ++++++++++++++++-
>  2 files changed, 23 insertions(+), 2 deletions(-)
> 
> diff --git a/include/libcamera/event_notifier.h b/include/libcamera/event_notifier.h
> index 1e9b6da1340c..f80945c743dc 100644
> --- a/include/libcamera/event_notifier.h
> +++ b/include/libcamera/event_notifier.h
> @@ -7,11 +7,14 @@
>  #ifndef __LIBCAMERA_EVENT_NOTIFIER_H__
>  #define __LIBCAMERA_EVENT_NOTIFIER_H__
>  
> +#include <libcamera/object.h>
>  #include <libcamera/signal.h>
>  
>  namespace libcamera {
>  
> -class EventNotifier
> +class Message;
> +
> +class EventNotifier : public Object
>  {
>  public:
>  	enum Type {
> @@ -31,6 +34,9 @@ public:
>  
>  	Signal<EventNotifier *> activated;
>  
> +protected:
> +	void message(Message *msg) override;
> +
>  private:
>  	int fd_;
>  	Type type_;
> diff --git a/src/libcamera/event_notifier.cpp b/src/libcamera/event_notifier.cpp
> index b32c7ed2d315..515e6d1770a1 100644
> --- a/src/libcamera/event_notifier.cpp
> +++ b/src/libcamera/event_notifier.cpp
> @@ -10,6 +10,9 @@
>  #include <libcamera/camera_manager.h>
>  #include <libcamera/event_dispatcher.h>
>  
> +#include "message.h"
> +#include "thread.h"
> +
>  /**
>   * \file event_notifier.h
>   * \brief File descriptor event notifier
> @@ -103,7 +106,7 @@ void EventNotifier::setEnabled(bool enable)
>  
>  	enabled_ = enable;
>  
> -	EventDispatcher *dispatcher = CameraManager::instance()->eventDispatcher();
> +	EventDispatcher *dispatcher = thread()->eventDispatcher();
>  	if (enable)
>  		dispatcher->registerEventNotifier(this);
>  	else
> @@ -119,4 +122,16 @@ void EventNotifier::setEnabled(bool enable)
>   * parameter.
>   */
>  
> +void EventNotifier::message(Message *msg)
> +{
> +	if (msg->type() == Message::ThreadMoveMessage) {
> +		if (enabled_) {
> +			setEnabled(false);
> +			invokeMethod(this, &EventNotifier::setEnabled, true);
> +		}
> +	}
> +
> +	Object::message(msg);
> +}
> +
>  } /* namespace libcamera */
> -- 
> Regards,
> 
> Laurent Pinchart
> 
> _______________________________________________
> libcamera-devel mailing list
> libcamera-devel@lists.libcamera.org
> https://lists.libcamera.org/listinfo/libcamera-devel

Patch

diff --git a/include/libcamera/event_notifier.h b/include/libcamera/event_notifier.h
index 1e9b6da1340c..f80945c743dc 100644
--- a/include/libcamera/event_notifier.h
+++ b/include/libcamera/event_notifier.h
@@ -7,11 +7,14 @@ 
 #ifndef __LIBCAMERA_EVENT_NOTIFIER_H__
 #define __LIBCAMERA_EVENT_NOTIFIER_H__
 
+#include <libcamera/object.h>
 #include <libcamera/signal.h>
 
 namespace libcamera {
 
-class EventNotifier
+class Message;
+
+class EventNotifier : public Object
 {
 public:
 	enum Type {
@@ -31,6 +34,9 @@  public:
 
 	Signal<EventNotifier *> activated;
 
+protected:
+	void message(Message *msg) override;
+
 private:
 	int fd_;
 	Type type_;
diff --git a/src/libcamera/event_notifier.cpp b/src/libcamera/event_notifier.cpp
index b32c7ed2d315..515e6d1770a1 100644
--- a/src/libcamera/event_notifier.cpp
+++ b/src/libcamera/event_notifier.cpp
@@ -10,6 +10,9 @@ 
 #include <libcamera/camera_manager.h>
 #include <libcamera/event_dispatcher.h>
 
+#include "message.h"
+#include "thread.h"
+
 /**
  * \file event_notifier.h
  * \brief File descriptor event notifier
@@ -103,7 +106,7 @@  void EventNotifier::setEnabled(bool enable)
 
 	enabled_ = enable;
 
-	EventDispatcher *dispatcher = CameraManager::instance()->eventDispatcher();
+	EventDispatcher *dispatcher = thread()->eventDispatcher();
 	if (enable)
 		dispatcher->registerEventNotifier(this);
 	else
@@ -119,4 +122,16 @@  void EventNotifier::setEnabled(bool enable)
  * parameter.
  */
 
+void EventNotifier::message(Message *msg)
+{
+	if (msg->type() == Message::ThreadMoveMessage) {
+		if (enabled_) {
+			setEnabled(false);
+			invokeMethod(this, &EventNotifier::setEnabled, true);
+		}
+	}
+
+	Object::message(msg);
+}
+
 } /* namespace libcamera */