[{"id":16976,"web_url":"https://patchwork.libcamera.org/comment/16976/","msgid":"<CAO5uPHO5ZbiLoCuuBhVH1h=u9jpYExKjjktMr6u4mCgik5wk+Q@mail.gmail.com>","date":"2021-05-17T04:53:45","subject":"Re: [libcamera-devel] [PATCH v2 6/8] android: Guard access to the\n\tcamera state","submitter":{"id":63,"url":"https://patchwork.libcamera.org/api/people/63/","name":"Hirokazu Honda","email":"hiroh@chromium.org"},"content":"Hi Jacopo, thank you for the patch.\n\nOn Thu, May 13, 2021 at 6:22 PM Jacopo Mondi <jacopo@jmondi.org> wrote:\n\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> ---\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 8a4543f079eb..c6cd0b6e8be7 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> +       MutexLocker cameraLock(cameraMutex_);\n>         if (state_ == CameraStopped)\n>                 return;\n>\n> @@ -1915,17 +1916,21 @@ int\n> CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Reques\n>         if (!isValidRequest(camera3Request))\n>                 return -EINVAL;\n>\n> -       /* Start the camera if that's the first request we handle. */\n> -       if (state_ == CameraStopped) {\n> -               worker_.start();\n> +       {\n> +               MutexLocker cameraLock(cameraMutex_);\n>\n> -               int ret = camera_->start();\n> -               if (ret) {\n> -                       LOG(HAL, Error) << \"Failed to start camera\";\n> -                       return ret;\n> -               }\n> +               /* Start the camera if that's the first request we handle.\n> */\n> +               if (state_ == CameraStopped) {\n> +                       worker_.start();\n>\n> -               state_ = CameraRunning;\n> +                       int ret = camera_->start();\n> +                       if (ret) {\n> +                               LOG(HAL, Error) << \"Failed to start\n> camera\";\n> +                               return ret;\n> +                       }\n> +\n> +                       state_ = CameraRunning;\n> +               }\n>         }\n>\n>         /*\n> diff --git a/src/android/camera_device.h b/src/android/camera_device.h\n> index 30b364decaab..f263fdae472a 100644\n> --- a/src/android/camera_device.h\n> +++ b/src/android/camera_device.h\n> @@ -120,6 +120,7 @@ private:\n>\n>         CameraWorker worker_;\n>\n> +       libcamera::Mutex cameraMutex_; /* Protects access to the camera\n> state. */\n>\n\nI wonder if this should be used to protect camera_ and worker_.\n\n\n>         State state_;\n>\n>         std::shared_ptr<libcamera::Camera> camera_;\n> --\n> 2.31.1\n>\n> _______________________________________________\n> libcamera-devel mailing list\n> libcamera-devel@lists.libcamera.org\n> https://lists.libcamera.org/listinfo/libcamera-devel\n>","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 52092C31FC\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 17 May 2021 04:54:00 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id C4C116891E;\n\tMon, 17 May 2021 06:53:59 +0200 (CEST)","from mail-ed1-x530.google.com (mail-ed1-x530.google.com\n\t[IPv6:2a00:1450:4864:20::530])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 2E9D3602B1\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 17 May 2021 06:53:58 +0200 (CEST)","by mail-ed1-x530.google.com with SMTP id n25so5245953edr.5\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSun, 16 May 2021 21:53:58 -0700 (PDT)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=chromium.org header.i=@chromium.org\n\theader.b=\"lYwphQ04\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org;\n\ts=google; \n\th=mime-version:references:in-reply-to:from:date:message-id:subject:to\n\t:cc; bh=VZypq24q5x8LVEd/6nSv3DB9C9hSIpJJzyJyuhyByyc=;\n\tb=lYwphQ04FrznlMdrR3ndlCk3TZ9An0jTc3DoL0U3m8ReJPlpClUpBX0zvX2591DWzI\n\tsCeQZj/6669DB5X042EgwbT2TPYs9AaNvpTXY5I3jXULsG2IYwEW74/jnqG8OAvjaJpr\n\t9ElY319ZenQyEfGXq05mMzMqs/pLyNrjd6swM=","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20161025;\n\th=x-gm-message-state:mime-version:references:in-reply-to:from:date\n\t:message-id:subject:to:cc;\n\tbh=VZypq24q5x8LVEd/6nSv3DB9C9hSIpJJzyJyuhyByyc=;\n\tb=FLIAtKpIxPr72SbWFjennaloIGPZAR5yqyDgdXKKCGkWM8uOgRbTAtsZWxKKJW8yI2\n\t2EwNDUeTnOlguCflclO3uvlLhKvflTcAoF3zFAP3ds8+i1t0mulwLAI3cZdw39Io9iK9\n\tq2TWO47d1v/yQ3ncR5YFBeeEx8jfBxnTtZodf0tT07ca2yYv5Uysc92/5FoM4iLYsrxo\n\tabZ6LZ0JUZftEHJ7m/DrP7T1DubP8MwTDzSpuafOaiq004Mo3kFJ62tt/bYkyj4XQ/SF\n\tvKnNAYp/1P6xtVaB4f4vWc9Jz0l8uQjpyx1Qk4ZtNyNvLHO40FlP+H8SLuHrizLlLrWJ\n\tMrdg==","X-Gm-Message-State":"AOAM530aIEKIeAKDcBA2PwDzJaGsIMATRwSuuQv/SuGRTmKGa5WHqGZi\n\tQ24NvMwyW1AK7C3kcnQ2Ip7QSL5HG3cWT+yYl06Nfg==","X-Google-Smtp-Source":"ABdhPJw9IcaWWgM5VlgKntclx39En8Y6Q7E5jdAQYrAQwq83StbvVM24UHtVtmmAu8kQzbJUPbY146sEQa/Ar+3vu0Q=","X-Received":"by 2002:a50:afa3:: with SMTP id\n\th32mr12083595edd.202.1621227237895; \n\tSun, 16 May 2021 21:53:57 -0700 (PDT)","MIME-Version":"1.0","References":"<20210513092246.42847-1-jacopo@jmondi.org>\n\t<20210513092246.42847-7-jacopo@jmondi.org>","In-Reply-To":"<20210513092246.42847-7-jacopo@jmondi.org>","From":"Hirokazu Honda <hiroh@chromium.org>","Date":"Mon, 17 May 2021 13:53:45 +0900","Message-ID":"<CAO5uPHO5ZbiLoCuuBhVH1h=u9jpYExKjjktMr6u4mCgik5wk+Q@mail.gmail.com>","To":"Jacopo Mondi <jacopo@jmondi.org>","Content-Type":"multipart/alternative; boundary=\"0000000000009d805c05c27f61da\"","Subject":"Re: [libcamera-devel] [PATCH v2 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 <libcamera-devel@lists.libcamera.org>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]