From patchwork Mon Nov 30 17:17:44 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: 29112 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 06F88C3300 for ; Mon, 30 Nov 2026 17:17:52 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 725E26AA45; Mon, 30 Nov 2026 18:17:51 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="e45rSOqh"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 0E11A6AA45 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 7A7654DC 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=ATDT/Calh8QVor+0UGcB4QzW4x5TAyYKY2WXdN05loo=; h=From:To:Subject:Date:From; b=e45rSOqh9zF9Dv7EFhdfugKqWiQ5AR0ia0qpyEDztCrPMOj/ulhd5xhB4DD5sOVOn CJDbxnhDwKNuTeI7bQgpE70baqV9IeZbDhtLbHxyO/NGK3ybJsiAJ1GkuPjRiiLd4J xXydrgq890ifSzXeZYu700PmaMrnsBzCnkMDzr7Q= From: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= To: libcamera-devel@lists.libcamera.org Subject: [RFC PATCH v1 1/2] libcamera: base: thread: removeMessages(): Avoid vector Date: Mon, 30 Nov 2026 18:17:44 +0100 Message-ID: <20261130171745.285537-1-barnabas.pocze@ideasonboard.com> X-Mailer: git-send-email 2.56.1 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 Since the messages of a thread are contained in an `std::list`, use an `std::list` instead of an `std::vector` to collect the messages that need to be removed. By using `splice()`, this avoids any dynamic allocation. Note that this change does not cause issues in `dispatchMessages()` because before a message is dispatched, the pointer is moved-from the list, leaving it as nullptr. This means that the a message that is being dispatched is never erased, and thus the dispatching will continue as expected. Signed-off-by: Barnabás Pőcze --- src/libcamera/base/thread.cpp | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/libcamera/base/thread.cpp b/src/libcamera/base/thread.cpp index b204355a58..d62a99f7da 100644 --- a/src/libcamera/base/thread.cpp +++ b/src/libcamera/base/thread.cpp @@ -586,24 +586,22 @@ void Thread::removeMessages(Object *receiver) if (!receiver->pendingMessages_) return; - std::vector> toDelete; - for (std::unique_ptr &msg : data_->messages_.list_) { - if (!msg) - continue; - if (msg->receiver_ != receiver) - continue; + std::list> toDelete; + for (auto it = data_->messages_.list_.begin(); it != data_->messages_.list_.end();) { + std::unique_ptr &msg = *it; /* * Move the message to the pending deletion list to delete it - * after releasing the lock. The messages list element will - * contain a null pointer, and will be removed when dispatching - * messages. + * after releasing the lock. */ - toDelete.push_back(std::move(msg)); - receiver->pendingMessages_--; + if (msg && msg->receiver_ == receiver) + toDelete.splice(toDelete.end(), data_->messages_.list_, it++); + else + ++it; } - ASSERT(!receiver->pendingMessages_); + ASSERT(receiver->pendingMessages_ == toDelete.size()); + receiver->pendingMessages_ = 0; locker.unlock(); toDelete.clear(); 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_; } /**