From patchwork Mon Nov 30 17:17:45 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= X-Patchwork-Id: 29113 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 1B1CFC3300 for ; Mon, 30 Nov 2026 17:17:55 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 254B76AA72; Mon, 30 Nov 2026 18:17:52 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="OW3nmh2e"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 28FF26AA60 for ; Mon, 30 Nov 2026 18:17:50 +0100 (CET) Received: from pb-laptop.local (185.182.215.119.nat.pool.zt.hu [185.182.215.119]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id A719EC36 for ; Mon, 30 Nov 2026 18:15:12 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1796058912; bh=wHiEnu3Oj8rbbMMy9SYJeGb9Id8L9sVkf1DLsHhokw4=; h=From:To:Subject:Date:In-Reply-To:References:From; b=OW3nmh2ebun2eQc58Z6zFNJbxmWjeYKcRT6/T6qMX4YNnksD31XyHQnXg+9FgL15c dkiKLnH9J3JccA+/M/iPxob4CBZ5eH2ksmzyNjyHaxYOZc1VwuPQTrXrjiMzbyH/05 LNTvYk0+if63p0T20HOUZ60HVxgTv3fwrVOnyQio= From: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= To: libcamera-devel@lists.libcamera.org Subject: [RFC PATCH v1 2/2] libcamera: base: thread: Remove message list entry immediately Date: Mon, 30 Nov 2026 18:17:45 +0100 Message-ID: <20261130171745.285537-2-barnabas.pocze@ideasonboard.com> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261130171745.285537-1-barnabas.pocze@ideasonboard.com> References: <20261130171745.285537-1-barnabas.pocze@ideasonboard.com> MIME-Version: 1.0 X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org 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 --- src/libcamera/base/thread.cpp | 33 +++++++++++---------------------- 1 file changed, 11 insertions(+), 22 deletions(-) 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> &messages = data_->messages_.list_; - for (std::unique_ptr &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 &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 = 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_; } /**