[libcamera-devel,05/12] test: event-thread: Destroy Object from correct thread context
diff mbox series

Message ID 20240121035948.4226-6-laurent.pinchart@ideasonboard.com
State Accepted
Headers show
Series
  • libcamera: Hardening against thread race conditions
Related show

Commit Message

Laurent Pinchart Jan. 21, 2024, 3:59 a.m. UTC
The EventHandler used in the test is destroyed from the main thread,
which is invalid for a thread-bound object bound to a different thread.
Fix it by destroying it with deleteLater().

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 test/event-thread.cpp | 37 +++++++++++++++++++++++++++----------
 1 file changed, 27 insertions(+), 10 deletions(-)

Comments

Milan Zamazal Jan. 22, 2024, 7:49 p.m. UTC | #1
Laurent Pinchart <laurent.pinchart@ideasonboard.com> writes:

> The EventHandler used in the test is destroyed from the main thread,
> which is invalid for a thread-bound object bound to a different thread.
> Fix it by destroying it with deleteLater().
>
> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

Reviewed-by: Milan Zamazal <mzamazal@redhat.com>

> ---
>  test/event-thread.cpp | 37 +++++++++++++++++++++++++++----------
>  1 file changed, 27 insertions(+), 10 deletions(-)
>
> diff --git a/test/event-thread.cpp b/test/event-thread.cpp
> index 88a8c07ef9f0..d6e5d27af185 100644
> --- a/test/event-thread.cpp
> +++ b/test/event-thread.cpp
> @@ -85,10 +85,17 @@ private:
>  class EventThreadTest : public Test
>  {
>  protected:
> +	int init()
> +	{
> +		thread_.start();
> +
> +		handler_ = new EventHandler();
> +
> +		return TestPass;
> +	}
> +
>  	int run()
>  	{
> -		Thread thread;
> -		thread.start();
>  
>  		/*
>  		 * Fire the event notifier and then move the notifier to a
> @@ -98,23 +105,33 @@ protected:
>  		 * different thread will correctly process already pending
>  		 * events in the new thread.
>  		 */
> -		EventHandler handler;
> -		handler.notify();
> -		handler.moveToThread(&thread);
> +		handler_->notify();
> +		handler_->moveToThread(&thread_);
>  
>  		this_thread::sleep_for(chrono::milliseconds(100));
>  
> -		/* Must stop thread before destroying the handler. */
> -		thread.exit(0);
> -		thread.wait();
> -
> -		if (!handler.notified()) {
> +		if (!handler_->notified()) {
>  			cout << "Thread event handling test failed" << endl;
>  			return TestFail;
>  		}
>  
>  		return TestPass;
>  	}
> +
> +	void cleanup()
> +	{
> +		/*
> +		 * Object class instances must be destroyed from the thread
> +		 * they live in.
> +		 */
> +		handler_->deleteLater();
> +		thread_.exit(0);
> +		thread_.wait();
> +	}
> +
> +private:
> +	EventHandler *handler_;
> +	Thread thread_;
>  };
>  
>  TEST_REGISTER(EventThreadTest)

Patch
diff mbox series

diff --git a/test/event-thread.cpp b/test/event-thread.cpp
index 88a8c07ef9f0..d6e5d27af185 100644
--- a/test/event-thread.cpp
+++ b/test/event-thread.cpp
@@ -85,10 +85,17 @@  private:
 class EventThreadTest : public Test
 {
 protected:
+	int init()
+	{
+		thread_.start();
+
+		handler_ = new EventHandler();
+
+		return TestPass;
+	}
+
 	int run()
 	{
-		Thread thread;
-		thread.start();
 
 		/*
 		 * Fire the event notifier and then move the notifier to a
@@ -98,23 +105,33 @@  protected:
 		 * different thread will correctly process already pending
 		 * events in the new thread.
 		 */
-		EventHandler handler;
-		handler.notify();
-		handler.moveToThread(&thread);
+		handler_->notify();
+		handler_->moveToThread(&thread_);
 
 		this_thread::sleep_for(chrono::milliseconds(100));
 
-		/* Must stop thread before destroying the handler. */
-		thread.exit(0);
-		thread.wait();
-
-		if (!handler.notified()) {
+		if (!handler_->notified()) {
 			cout << "Thread event handling test failed" << endl;
 			return TestFail;
 		}
 
 		return TestPass;
 	}
+
+	void cleanup()
+	{
+		/*
+		 * Object class instances must be destroyed from the thread
+		 * they live in.
+		 */
+		handler_->deleteLater();
+		thread_.exit(0);
+		thread_.wait();
+	}
+
+private:
+	EventHandler *handler_;
+	Thread thread_;
 };
 
 TEST_REGISTER(EventThreadTest)