From patchwork Thu May 13 09:22:44 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 12268 X-Patchwork-Delegate: jacopo@jmondi.org 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 81C47C31EF for ; Thu, 13 May 2021 09:22:15 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 4426A6892E; Thu, 13 May 2021 11:22:15 +0200 (CEST) Received: from relay1-d.mail.gandi.net (relay1-d.mail.gandi.net [217.70.183.193]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 547C26891A for ; Thu, 13 May 2021 11:22:11 +0200 (CEST) Received: from uno.LocalDomain (93-61-96-190.ip145.fastwebnet.it [93.61.96.190]) (Authenticated sender: jacopo@jmondi.org) by relay1-d.mail.gandi.net (Postfix) with ESMTPSA id B0B92240008; Thu, 13 May 2021 09:22:10 +0000 (UTC) From: Jacopo Mondi To: libcamera-devel@lists.libcamera.org Date: Thu, 13 May 2021 11:22:44 +0200 Message-Id: <20210513092246.42847-7-jacopo@jmondi.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210513092246.42847-1-jacopo@jmondi.org> References: <20210513092246.42847-1-jacopo@jmondi.org> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 6/8] android: Guard access to the camera state 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" Guard access to the camera state and the start/stop sequences with a mutex. Currently only stop() and the first call to processCaptureRequest() start and stop the camera, and they're not meant to race with each other. With the introduction of flush() the camera can be stopped concurrently to a processCaptureRequest() call, hence access to the camera state will need to be protected. Prepare for that by guarding the existing paths with a mutex. Signed-off-by: Jacopo Mondi Reviewed-by: Niklas Söderlund --- src/android/camera_device.cpp | 23 ++++++++++++++--------- src/android/camera_device.h | 1 + 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp index 8a4543f079eb..c6cd0b6e8be7 100644 --- a/src/android/camera_device.cpp +++ b/src/android/camera_device.cpp @@ -759,6 +759,7 @@ void CameraDevice::close() void CameraDevice::stop() { + MutexLocker cameraLock(cameraMutex_); if (state_ == CameraStopped) return; @@ -1915,17 +1916,21 @@ int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Reques if (!isValidRequest(camera3Request)) return -EINVAL; - /* Start the camera if that's the first request we handle. */ - if (state_ == CameraStopped) { - worker_.start(); + { + MutexLocker cameraLock(cameraMutex_); - int ret = camera_->start(); - if (ret) { - LOG(HAL, Error) << "Failed to start camera"; - return ret; - } + /* Start the camera if that's the first request we handle. */ + if (state_ == CameraStopped) { + worker_.start(); - state_ = CameraRunning; + int ret = camera_->start(); + if (ret) { + LOG(HAL, Error) << "Failed to start camera"; + return ret; + } + + state_ = CameraRunning; + } } /* diff --git a/src/android/camera_device.h b/src/android/camera_device.h index 30b364decaab..f263fdae472a 100644 --- a/src/android/camera_device.h +++ b/src/android/camera_device.h @@ -120,6 +120,7 @@ private: CameraWorker worker_; + libcamera::Mutex cameraMutex_; /* Protects access to the camera state. */ State state_; std::shared_ptr camera_;