From patchwork Mon Mar 3 14:20:10 2025 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: 22905 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 3B336BD808 for ; Mon, 3 Mar 2025 14:20:18 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 2549A687EB; Mon, 3 Mar 2025 15:20:17 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="ACTlZ9Z8"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 0064268772 for ; Mon, 3 Mar 2025 15:20:14 +0100 (CET) Received: from pb-laptop.local (185.221.143.4.nat.pool.zt.hu [185.221.143.4]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 79264346 for ; Mon, 3 Mar 2025 15:18:43 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1741011523; bh=c4r/pJEhhSyInFGPBj6pSe19gl3oxdCK4R9br25tVOg=; h=From:To:Subject:Date:From; b=ACTlZ9Z8/b88q0vhkEhPqI9bJvduYKu5mD7Fl4ins99F+uIsQJxPOQmwYN05NmdaJ 48k1ftdX25z83333cQ9dDOd1vfaqACIvno81GPUjvKHPus1f2S8B5E6VLyYqF07TUx S/49ywDZQdJ4qo52XT2ry286VS8dqf6l6RSWFFew= From: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= To: libcamera-devel@lists.libcamera.org Subject: [PATCH v1] libcamera: camera_manager: Do not emit signals while holding lock Date: Mon, 3 Mar 2025 15:20:10 +0100 Message-ID: <20250303142010.714280-1-barnabas.pocze@ideasonboard.com> X-Mailer: git-send-email 2.48.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 Sender: "libcamera-devel" Both `CameraManager::Private::{add,remove}Camera()` emit the `camera{Added,Removed}` signals, respectively, while holding the lock protecting the list of cameras. This is problematic because if a callback tries to call `cameras()`, then the same (non-recursive) lock would be locked again. Furthermore, there is no real need to hold the lock while user code is running, so release the lock as soon as possible. Signed-off-by: Barnabás Pőcze Reviewed-by: Harvey Yang Reviewed-by: Laurent Pinchart --- src/libcamera/camera_manager.cpp | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/libcamera/camera_manager.cpp b/src/libcamera/camera_manager.cpp index 87e6717ec..d728ac44a 100644 --- a/src/libcamera/camera_manager.cpp +++ b/src/libcamera/camera_manager.cpp @@ -202,6 +202,7 @@ void CameraManager::Private::addCamera(std::shared_ptr camera) { ASSERT(Thread::current() == this); +{ MutexLocker locker(mutex_); for (const std::shared_ptr &c : cameras_) { @@ -213,13 +214,12 @@ void CameraManager::Private::addCamera(std::shared_ptr camera) } } - cameras_.push_back(std::move(camera)); - - unsigned int index = cameras_.size() - 1; + cameras_.push_back(camera); +} /* Report the addition to the public signal */ CameraManager *const o = LIBCAMERA_O_PTR(); - o->cameraAdded.emit(cameras_[index]); + o->cameraAdded.emit(std::move(camera)); } /** @@ -236,6 +236,7 @@ void CameraManager::Private::removeCamera(std::shared_ptr camera) { ASSERT(Thread::current() == this); +{ MutexLocker locker(mutex_); auto iter = std::find_if(cameras_.begin(), cameras_.end(), @@ -245,14 +246,15 @@ void CameraManager::Private::removeCamera(std::shared_ptr camera) if (iter == cameras_.end()) return; + cameras_.erase(iter); +} + LOG(Camera, Debug) << "Unregistering camera '" << camera->id() << "'"; - cameras_.erase(iter); - /* Report the removal to the public signal */ CameraManager *const o = LIBCAMERA_O_PTR(); - o->cameraRemoved.emit(camera); + o->cameraRemoved.emit(std::move(camera)); } /**