[RFC,v1,2/2] libcamera: base: thread: Remove message list entry immediately
diff mbox series

Message ID 20261130171745.285537-2-barnabas.pocze@ideasonboard.com
State New
Headers show
Series
  • [RFC,v1,1/2] libcamera: base: thread: removeMessages(): Avoid vector
Related show

Commit Message

Barnabás Pőcze Nov. 30, 2026, 5:17 p.m. UTC
This commit effectively reverts da9b6bb196e0 ("base: thread: Fix recursive calls to dispatchMessages()").
And addresses the underlying issue in another way. Consider the following:

  (1) a message is moved-from before dispatching
  (2) a moved-from message is ignored during recursive dispatch or deletion
  (3) erasure from a list only invalidates the erased element(s)

These three together provide a way to avoid delaying the message deletion
until after the top-most dispatch.

If the iterator is left alone until after the message has been processed,
then due to the above it is guaranteed that the iterator will be a valid
iterator pointing into the thread's message list.

Therefore it is simply sufficient to do the deletion after the message
has been processed for correct operation. Note that prior to the above commit,
the deletion was carried out before the message processing, which meant that
e.g. it could be deleted during message processing.

Signed-off-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>
---
 src/libcamera/base/thread.cpp | 33 +++++++++++----------------------
 1 file changed, 11 insertions(+), 22 deletions(-)

Patch
diff mbox series

diff --git a/src/libcamera/base/thread.cpp b/src/libcamera/base/thread.cpp
index d62a99f7da..8cc1cea2a7 100644
--- a/src/libcamera/base/thread.cpp
+++ b/src/libcamera/base/thread.cpp
@@ -635,20 +635,19 @@  void Thread::dispatchMessages(Message::Type type, Object *receiver)
 
 	std::list<std::unique_ptr<Message>> &messages = data_->messages_.list_;
 
-	for (std::unique_ptr<Message> &msg : messages) {
-		if (!msg)
-			continue;
-
-		if (type != Message::Type::None && msg->type() != type)
-			continue;
+	for (auto it = messages.begin(); it != messages.end();) {
+		std::unique_ptr<Message> &msg = *it;
 
-		if (receiver && receiver != msg->receiver_)
+		if (!msg ||
+		    (type != Message::Type::None && msg->type() != type) ||
+		    (receiver && receiver != msg->receiver_)) {
+			++it;
 			continue;
+		}
 
 		/*
 		 * Move the message, setting the entry in the list to null. It
-		 * will cause recursive calls to ignore the entry, and the erase
-		 * loop at the end of the function to delete it from the list.
+		 * will cause recursive calls to ignore the entry.
 		 */
 		std::unique_ptr<Message> message = std::move(msg);
 
@@ -660,21 +659,11 @@  void Thread::dispatchMessages(Message::Type type, Object *receiver)
 		messageReceiver->message(message.get());
 		message.reset();
 		locker.lock();
-	}
 
-	/*
-	 * If the recursion level is 0, erase all null messages in the list. We
-	 * can't do so during recursion, as it would invalidate the iterator of
-	 * the outer calls.
-	 */
-	if (!--data_->messages_.recursion_) {
-		for (auto iter = messages.begin(); iter != messages.end();) {
-			if (!*iter)
-				iter = messages.erase(iter);
-			else
-				++iter;
-		}
+		it = messages.erase(it);
 	}
+
+	--data_->messages_.recursion_;
 }
 
 /**