[libcamera-devel,v4,2/2] libcamera: base: object: Prevent the same signal being connected more than once
diff mbox series

Message ID 20220201123803.281397-3-kieran.bingham@ideasonboard.com
State Accepted
Headers show
Series
  • libcamera: pipeline_handler: Register requests
Related show

Commit Message

Kieran Bingham Feb. 1, 2022, 12:38 p.m. UTC
Objects are not expected to be connected to the same signal more than
once. Doing so likely indicates a bug in the code, and can be
highlighted in debug builds with an assert that performs a lookup on the
signals_ list.

While it is possible to allow the implementation to let objects connect
to a specific signal multiple times, there are no expected use cases for
this in libcamera and this behaviour is restricted to favour defensive
programming by raising an error when this occurs.

Remove the support in the test framework which uses multiple Signal
connections on the same object, and update the test to use a second
Signal.

Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>
---
 src/libcamera/base/object.cpp | 12 ++++++++++++
 src/libcamera/base/signal.cpp |  7 ++++---
 test/signal.cpp               | 17 +++++++++++------
 3 files changed, 27 insertions(+), 9 deletions(-)

Comments

Laurent Pinchart Feb. 3, 2022, 3:04 a.m. UTC | #1
Hi Kieran,

Thank you for the patch.

On Tue, Feb 01, 2022 at 12:38:03PM +0000, Kieran Bingham wrote:
> Objects are not expected to be connected to the same signal more than
> once. Doing so likely indicates a bug in the code, and can be
> highlighted in debug builds with an assert that performs a lookup on the
> signals_ list.
> 
> While it is possible to allow the implementation to let objects connect
> to a specific signal multiple times, there are no expected use cases for
> this in libcamera and this behaviour is restricted to favour defensive
> programming by raising an error when this occurs.
> 
> Remove the support in the test framework which uses multiple Signal
> connections on the same object, and update the test to use a second
> Signal.
> 
> Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
> Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>
> ---
>  src/libcamera/base/object.cpp | 12 ++++++++++++
>  src/libcamera/base/signal.cpp |  7 ++++---
>  test/signal.cpp               | 17 +++++++++++------
>  3 files changed, 27 insertions(+), 9 deletions(-)
> 
> diff --git a/src/libcamera/base/object.cpp b/src/libcamera/base/object.cpp
> index 3f28768e48f8..13e8ee99caac 100644
> --- a/src/libcamera/base/object.cpp
> +++ b/src/libcamera/base/object.cpp
> @@ -47,6 +47,12 @@ LOG_DEFINE_CATEGORY(Object)
>   * object's thread, regardless of whether the signal is emitted in the same or
>   * in another thread.
>   *
> + * Objects can be connected to multiple signals, buy they can only be connected

s/buy/but/

> + * to each signal once. While it is possible to allow the implementation to let
> + * objects connect to a specific signal multiple times, there are no expected
> + * use cases for this in libcamera and this behaviour is restricted to favour
> + * defensive programming by raising an error when this occurs.

"raising an error" may sound like an error will be returned and could be
handled, but the process will actually abort. Could we clarify this ?
One option could be

 * Objects can be connected to multiple signals, buy they can only be connected
 * to each signal once. Attempting to create multiple concurrent connections
 * between the same signal and the same Object (to either the same or differents
 * slots of the object) will cause an assertion failure. While it would be
 * possible to allow the implementation to let objects connect to the same
 * signal multiple times, there are no expected use cases for this in libcamera
 * and this behaviour is restricted to favour defensive programming.

> + *
>   * \sa Message, Signal, Thread
>   */
>  
> @@ -284,6 +290,12 @@ void Object::notifyThreadMove()
>  
>  void Object::connect(SignalBase *signal)
>  {
> +	/*
> +	 * Connecting the same signal to an object multiple times is not
> +	 * supported.
> +	 */
> +	ASSERT(std::find(signals_.begin(), signals_.end(), signal) == signals_.end());
> +
>  	signals_.push_back(signal);
>  }
>  
> diff --git a/src/libcamera/base/signal.cpp b/src/libcamera/base/signal.cpp
> index 9df45d079a42..a46386a05abf 100644
> --- a/src/libcamera/base/signal.cpp
> +++ b/src/libcamera/base/signal.cpp
> @@ -93,9 +93,7 @@ SignalBase::SlotList SignalBase::slots()
>   * Connecting a signal to a slot results in the slot being called with the
>   * arguments passed to the emit() function when the signal is emitted. Multiple
>   * slots can be connected to the same signal, and multiple signals can connected
> - * to the same slot. Duplicate connections between a signal and a slot are
> - * allowed and result in the slot being called multiple times for the same
> - * signal emission.
> + * to the same slot.
>   *
>   * When a slot belongs to an instance of the Object class, the slot is called
>   * in the context of the thread that the object is bound to. If the signal is
> @@ -105,6 +103,9 @@ SignalBase::SlotList SignalBase::slots()
>   * loop, after the Signal::emit() function returns, with a copy of the signal's
>   * arguments. The emitter shall thus ensure that any pointer or reference
>   * passed through the signal will remain valid after the signal is emitted.
> + *
> + * Duplicate connections between a signal and a slot are not expected and use of
> + * the Object class to manage signals will enforce this restriction.
>   */
>  
>  /**
> diff --git a/test/signal.cpp b/test/signal.cpp
> index fcf2def18df4..48f905348ca3 100644
> --- a/test/signal.cpp
> +++ b/test/signal.cpp
> @@ -212,17 +212,19 @@ protected:
>  		/* ----------------- Signal -> Object tests ----------------- */
>  
>  		/*
> -		 * Test automatic disconnection on object deletion. Connect the
> -		 * slot twice to ensure all instances are disconnected.
> +		 * Test automatic disconnection on object deletion. Connect two
> +		 * signals to ensure all instances are disconnected.
>  		 */
>  		signalVoid_.disconnect();
> +		signalVoid2_.disconnect();

This could be skipped as the signal isn't connected at this point, but
it doesn't hurt, and could be useful to catch issues with disconnecting
a non-connected signal.

>  
>  		SlotObject *slotObject = new SlotObject();
>  		signalVoid_.connect(slotObject, &SlotObject::slot);
> -		signalVoid_.connect(slotObject, &SlotObject::slot);
> +		signalVoid2_.connect(slotObject, &SlotObject::slot);
>  		delete slotObject;
>  		valueStatic_ = 0;
>  		signalVoid_.emit();
> +		signalVoid2_.emit();
>  		if (valueStatic_ != 0) {
>  			cout << "Signal disconnection on object deletion test failed" << endl;
>  			return TestFail;
> @@ -298,17 +300,19 @@ protected:
>  		/* --------- Signal -> Object (multiple inheritance) -------- */
>  
>  		/*
> -		 * Test automatic disconnection on object deletion. Connect the
> -		 * slot twice to ensure all instances are disconnected.
> +		 * Test automatic disconnection on object deletion. connect two

s/connect two/Connect two/

Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

> +		 * signals to ensure all instances are disconnected.
>  		 */
>  		signalVoid_.disconnect();
> +		signalVoid2_.disconnect();
>  
>  		SlotMulti *slotMulti = new SlotMulti();
>  		signalVoid_.connect(slotMulti, &SlotMulti::slot);
> -		signalVoid_.connect(slotMulti, &SlotMulti::slot);
> +		signalVoid2_.connect(slotMulti, &SlotMulti::slot);
>  		delete slotMulti;
>  		valueStatic_ = 0;
>  		signalVoid_.emit();
> +		signalVoid2_.emit();
>  		if (valueStatic_ != 0) {
>  			cout << "Signal disconnection on object deletion test failed" << endl;
>  			return TestFail;
> @@ -345,6 +349,7 @@ protected:
>  
>  private:
>  	Signal<> signalVoid_;
> +	Signal<> signalVoid2_;
>  	Signal<int> signalInt_;
>  	Signal<int, const std::string &> signalMultiArgs_;
>

Patch
diff mbox series

diff --git a/src/libcamera/base/object.cpp b/src/libcamera/base/object.cpp
index 3f28768e48f8..13e8ee99caac 100644
--- a/src/libcamera/base/object.cpp
+++ b/src/libcamera/base/object.cpp
@@ -47,6 +47,12 @@  LOG_DEFINE_CATEGORY(Object)
  * object's thread, regardless of whether the signal is emitted in the same or
  * in another thread.
  *
+ * Objects can be connected to multiple signals, buy they can only be connected
+ * to each signal once. While it is possible to allow the implementation to let
+ * objects connect to a specific signal multiple times, there are no expected
+ * use cases for this in libcamera and this behaviour is restricted to favour
+ * defensive programming by raising an error when this occurs.
+ *
  * \sa Message, Signal, Thread
  */
 
@@ -284,6 +290,12 @@  void Object::notifyThreadMove()
 
 void Object::connect(SignalBase *signal)
 {
+	/*
+	 * Connecting the same signal to an object multiple times is not
+	 * supported.
+	 */
+	ASSERT(std::find(signals_.begin(), signals_.end(), signal) == signals_.end());
+
 	signals_.push_back(signal);
 }
 
diff --git a/src/libcamera/base/signal.cpp b/src/libcamera/base/signal.cpp
index 9df45d079a42..a46386a05abf 100644
--- a/src/libcamera/base/signal.cpp
+++ b/src/libcamera/base/signal.cpp
@@ -93,9 +93,7 @@  SignalBase::SlotList SignalBase::slots()
  * Connecting a signal to a slot results in the slot being called with the
  * arguments passed to the emit() function when the signal is emitted. Multiple
  * slots can be connected to the same signal, and multiple signals can connected
- * to the same slot. Duplicate connections between a signal and a slot are
- * allowed and result in the slot being called multiple times for the same
- * signal emission.
+ * to the same slot.
  *
  * When a slot belongs to an instance of the Object class, the slot is called
  * in the context of the thread that the object is bound to. If the signal is
@@ -105,6 +103,9 @@  SignalBase::SlotList SignalBase::slots()
  * loop, after the Signal::emit() function returns, with a copy of the signal's
  * arguments. The emitter shall thus ensure that any pointer or reference
  * passed through the signal will remain valid after the signal is emitted.
+ *
+ * Duplicate connections between a signal and a slot are not expected and use of
+ * the Object class to manage signals will enforce this restriction.
  */
 
 /**
diff --git a/test/signal.cpp b/test/signal.cpp
index fcf2def18df4..48f905348ca3 100644
--- a/test/signal.cpp
+++ b/test/signal.cpp
@@ -212,17 +212,19 @@  protected:
 		/* ----------------- Signal -> Object tests ----------------- */
 
 		/*
-		 * Test automatic disconnection on object deletion. Connect the
-		 * slot twice to ensure all instances are disconnected.
+		 * Test automatic disconnection on object deletion. Connect two
+		 * signals to ensure all instances are disconnected.
 		 */
 		signalVoid_.disconnect();
+		signalVoid2_.disconnect();
 
 		SlotObject *slotObject = new SlotObject();
 		signalVoid_.connect(slotObject, &SlotObject::slot);
-		signalVoid_.connect(slotObject, &SlotObject::slot);
+		signalVoid2_.connect(slotObject, &SlotObject::slot);
 		delete slotObject;
 		valueStatic_ = 0;
 		signalVoid_.emit();
+		signalVoid2_.emit();
 		if (valueStatic_ != 0) {
 			cout << "Signal disconnection on object deletion test failed" << endl;
 			return TestFail;
@@ -298,17 +300,19 @@  protected:
 		/* --------- Signal -> Object (multiple inheritance) -------- */
 
 		/*
-		 * Test automatic disconnection on object deletion. Connect the
-		 * slot twice to ensure all instances are disconnected.
+		 * Test automatic disconnection on object deletion. connect two
+		 * signals to ensure all instances are disconnected.
 		 */
 		signalVoid_.disconnect();
+		signalVoid2_.disconnect();
 
 		SlotMulti *slotMulti = new SlotMulti();
 		signalVoid_.connect(slotMulti, &SlotMulti::slot);
-		signalVoid_.connect(slotMulti, &SlotMulti::slot);
+		signalVoid2_.connect(slotMulti, &SlotMulti::slot);
 		delete slotMulti;
 		valueStatic_ = 0;
 		signalVoid_.emit();
+		signalVoid2_.emit();
 		if (valueStatic_ != 0) {
 			cout << "Signal disconnection on object deletion test failed" << endl;
 			return TestFail;
@@ -345,6 +349,7 @@  protected:
 
 private:
 	Signal<> signalVoid_;
+	Signal<> signalVoid2_;
 	Signal<int> signalInt_;
 	Signal<int, const std::string &> signalMultiArgs_;