[{"id":17139,"web_url":"https://patchwork.libcamera.org/comment/17139/","msgid":"<YKqaTXm/j1zaX2fa@pendragon.ideasonboard.com>","date":"2021-05-23T18:09:17","subject":"Re: [libcamera-devel] [PATCH v3 6/8] android: Guard access to the\n\tcamera state","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Jacopo,\n\nThank you for the patch.\n\nOn Fri, May 21, 2021 at 05:42:25PM +0200, Jacopo Mondi wrote:\n> Guard access to the camera state and the start/stop sequences\n> with a mutex.\n> \n> Currently only stop() and the first call to processCaptureRequest()\n> start and stop the camera, and they're not meant to race with each\n> other. With the introduction of flush() the camera can be stopped\n> concurrently to a processCaptureRequest() call, hence access to the\n> camera state will need to be protected.\n> \n> Prepare for that by guarding the existing paths with a mutex.\n> \n> Signed-off-by: Jacopo Mondi <jacopo@jmondi.org>\n> Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\n> Reviewed-by: Hirokazu Honda <hiroh@chromium.org>\n> ---\n>  src/android/camera_device.cpp | 23 ++++++++++++++---------\n>  src/android/camera_device.h   |  1 +\n>  2 files changed, 15 insertions(+), 9 deletions(-)\n> \n> diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp\n> index fceae96a8b7c..6645c019410e 100644\n> --- a/src/android/camera_device.cpp\n> +++ b/src/android/camera_device.cpp\n> @@ -759,6 +759,7 @@ void CameraDevice::close()\n>  \n>  void CameraDevice::stop()\n>  {\n> +\tMutexLocker cameraLock(cameraMutex_);\n>  \tif (state_ == CameraStopped)\n>  \t\treturn;\n>  \n> @@ -1844,17 +1845,21 @@ int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Reques\n>  \tif (!isValidRequest(camera3Request))\n>  \t\treturn -EINVAL;\n>  \n> -\t/* Start the camera if that's the first request we handle. */\n> -\tif (state_ == CameraStopped) {\n> -\t\tworker_.start();\n> +\t{\n> +\t\tMutexLocker cameraLock(cameraMutex_);\n>  \n> -\t\tint ret = camera_->start();\n> -\t\tif (ret) {\n> -\t\t\tLOG(HAL, Error) << \"Failed to start camera\";\n> -\t\t\treturn ret;\n> -\t\t}\n> +\t\t/* Start the camera if that's the first request we handle. */\n> +\t\tif (state_ == CameraStopped) {\n> +\t\t\tworker_.start();\n>  \n> -\t\tstate_ = CameraRunning;\n> +\t\t\tint ret = camera_->start();\n> +\t\t\tif (ret) {\n> +\t\t\t\tLOG(HAL, Error) << \"Failed to start camera\";\n> +\t\t\t\treturn ret;\n> +\t\t\t}\n> +\n> +\t\t\tstate_ = CameraRunning;\n> +\t\t}\n>  \t}\n>  \n>  \t/*\n> diff --git a/src/android/camera_device.h b/src/android/camera_device.h\n> index 995b423c6deb..6fe5454c6535 100644\n> --- a/src/android/camera_device.h\n> +++ b/src/android/camera_device.h\n> @@ -119,6 +119,7 @@ private:\n>  \n>  \tCameraWorker worker_;\n>  \n> +\tlibcamera::Mutex cameraMutex_; /* Protects access to the camera state. */\n\nTo make it more explicit that it protects the state, I'd prefer naming\nthe variable stateMutex_.\n\nThe patch looks good, but I suppose I'll see how the mutex is really\nused in the next patches. Assuming there's no issue there,\n\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\n>  \tState state_;\n>  \n>  \tstd::shared_ptr<libcamera::Camera> camera_;","headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 8A7DFC3200\n\tfor <parsemail@patchwork.libcamera.org>;\n\tSun, 23 May 2021 18:09:23 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 1187D602B1;\n\tSun, 23 May 2021 20:09:23 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 9B2A7601A9\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSun, 23 May 2021 20:09:21 +0200 (CEST)","from pendragon.ideasonboard.com (62-78-145-57.bb.dnainternet.fi\n\t[62.78.145.57])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id EB7D82A8;\n\tSun, 23 May 2021 20:09:20 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"dJaKq17G\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1621793361;\n\tbh=faEihPnQfbKZ3eaSV7mLTIpvYFvX3Z2pi9t/z3/Tev4=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=dJaKq17G+rppudVUPyghjT8FB9lNK1KbsIDoppyOmPyhmMkvbXXfpV8CTR5SACHvV\n\t1Nk3NW31Op+YoCIJ6XqfAOfPAH40pntGWdMZIgua+m9mOgGe1Mr4Fb2y+9eZE/Ed9v\n\tsQaJuS3fQl4qqt8YwfAnESATP6zFLvd02Qb6pFTk=","Date":"Sun, 23 May 2021 21:09:17 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Jacopo Mondi <jacopo@jmondi.org>","Message-ID":"<YKqaTXm/j1zaX2fa@pendragon.ideasonboard.com>","References":"<20210521154227.60186-1-jacopo@jmondi.org>\n\t<20210521154227.60186-7-jacopo@jmondi.org>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","Content-Transfer-Encoding":"8bit","In-Reply-To":"<20210521154227.60186-7-jacopo@jmondi.org>","Subject":"Re: [libcamera-devel] [PATCH v3 6/8] android: Guard access to the\n\tcamera state","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Cc":"libcamera-devel@lists.libcamera.org","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]