[{"id":20876,"web_url":"https://patchwork.libcamera.org/comment/20876/","msgid":"<887b0cf3-a0fc-f515-d272-8f9288248f50@ideasonboard.com>","date":"2021-11-11T17:10:29","subject":"Re: [libcamera-devel] [RFC PATCH 3/6] android: camera_hal_manager:\n\tAdd thread safety annotation","submitter":{"id":86,"url":"https://patchwork.libcamera.org/api/people/86/","name":"Umang Jain","email":"umang.jain@ideasonboard.com"},"content":"Hi Hiro\n\nOn 10/29/21 9:44 AM, Hirokazu Honda wrote:\n> This applies clang thread safety annotation to CameraHalManager.\n> Mutex and MutexLocker there are replaced with Mutex2 and\n> MutexLocer2.\n>\n> Signed-off-by: Hirokazu Honda <hiroh@chromium.org>\n\n\nPatch looks good to me\n\nReviewed-by: Umang Jain <umang.jain@ideasonboard.com>\n\n> ---\n>   src/android/camera_hal_manager.cpp | 10 +++++-----\n>   src/android/camera_hal_manager.h   | 14 ++++++--------\n>   2 files changed, 11 insertions(+), 13 deletions(-)\n>\n> diff --git a/src/android/camera_hal_manager.cpp b/src/android/camera_hal_manager.cpp\n> index 5f7bfe26..26715a5f 100644\n> --- a/src/android/camera_hal_manager.cpp\n> +++ b/src/android/camera_hal_manager.cpp\n> @@ -75,7 +75,7 @@ int CameraHalManager::init()\n>   std::tuple<CameraDevice *, int>\n>   CameraHalManager::open(unsigned int id, const hw_module_t *hardwareModule)\n>   {\n> -\tMutexLocker locker(mutex_);\n> +\tMutexLocker2 locker(mutex_);\n>   \n>   \tif (!callbacks_) {\n>   \t\tLOG(HAL, Error) << \"Can't open camera before callbacks are set\";\n> @@ -103,7 +103,7 @@ void CameraHalManager::cameraAdded(std::shared_ptr<Camera> cam)\n>   \tbool isCameraExternal = false;\n>   \tbool isCameraNew = false;\n>   \n> -\tMutexLocker locker(mutex_);\n> +\tMutexLocker2 locker(mutex_);\n>   \n>   \t/*\n>   \t * Each camera is assigned a unique integer ID when it is seen for the\n> @@ -198,7 +198,7 @@ void CameraHalManager::cameraAdded(std::shared_ptr<Camera> cam)\n>   \n>   void CameraHalManager::cameraRemoved(std::shared_ptr<Camera> cam)\n>   {\n> -\tMutexLocker locker(mutex_);\n> +\tMutexLocker2 locker(mutex_);\n>   \n>   \tauto iter = std::find_if(cameras_.begin(), cameras_.end(),\n>   \t\t\t\t [&cam](const std::unique_ptr<CameraDevice> &camera) {\n> @@ -257,7 +257,7 @@ int CameraHalManager::getCameraInfo(unsigned int id, struct camera_info *info)\n>   \tif (!info)\n>   \t\treturn -EINVAL;\n>   \n> -\tMutexLocker locker(mutex_);\n> +\tMutexLocker2 locker(mutex_);\n>   \n>   \tCameraDevice *camera = cameraDeviceFromHalId(id);\n>   \tif (!camera) {\n> @@ -280,7 +280,7 @@ void CameraHalManager::setCallbacks(const camera_module_callbacks_t *callbacks)\n>   {\n>   \tcallbacks_ = callbacks;\n>   \n> -\tMutexLocker locker(mutex_);\n> +\tMutexLocker2 locker(mutex_);\n>   \n>   \t/*\n>   \t * Some external cameras may have been identified before the callbacks_\n> diff --git a/src/android/camera_hal_manager.h b/src/android/camera_hal_manager.h\n> index 3f6d302a..9b8c3a1f 100644\n> --- a/src/android/camera_hal_manager.h\n> +++ b/src/android/camera_hal_manager.h\n> @@ -8,7 +8,6 @@\n>   #define __ANDROID_CAMERA_MANAGER_H__\n>   \n>   #include <map>\n> -#include <mutex>\n>   #include <stddef.h>\n>   #include <tuple>\n>   #include <vector>\n> @@ -18,6 +17,8 @@\n>   #include <system/camera_metadata.h>\n>   \n>   #include <libcamera/base/class.h>\n> +#include <libcamera/base/mutex.h>\n> +#include <libcamera/base/thread_annotations.h>\n>   \n>   #include <libcamera/camera_manager.h>\n>   \n> @@ -44,9 +45,6 @@ public:\n>   private:\n>   \tLIBCAMERA_DISABLE_COPY_AND_MOVE(CameraHalManager)\n>   \n> -\tusing Mutex = std::mutex;\n> -\tusing MutexLocker = std::unique_lock<std::mutex>;\n> -\n>   \tstatic constexpr unsigned int firstExternalCameraId_ = 1000;\n>   \n>   \tCameraHalManager();\n> @@ -56,15 +54,15 @@ private:\n>   \tvoid cameraAdded(std::shared_ptr<libcamera::Camera> cam);\n>   \tvoid cameraRemoved(std::shared_ptr<libcamera::Camera> cam);\n>   \n> -\tCameraDevice *cameraDeviceFromHalId(unsigned int id);\n> +\tCameraDevice *cameraDeviceFromHalId(unsigned int id) REQUIRES(mutex_);\n>   \n>   \tstd::unique_ptr<libcamera::CameraManager> cameraManager_;\n>   \tCameraHalConfig halConfig_;\n>   \n>   \tconst camera_module_callbacks_t *callbacks_;\n> -\tstd::vector<std::unique_ptr<CameraDevice>> cameras_;\n> -\tstd::map<std::string, unsigned int> cameraIdsMap_;\n> -\tMutex mutex_;\n> +\tstd::vector<std::unique_ptr<CameraDevice>> cameras_ GUARDED_BY(mutex_);\n> +\tstd::map<std::string, unsigned int> cameraIdsMap_ GUARDED_BY(mutex_);\n> +\tlibcamera::Mutex2 mutex_;\n>   \n>   \tunsigned int numInternalCameras_;\n>   \tunsigned int nextExternalCameraId_;","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 C68A5BF415\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 11 Nov 2021 17:10:38 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 2AA0E60345;\n\tThu, 11 Nov 2021 18:10:38 +0100 (CET)","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 B8401600B5\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 11 Nov 2021 18:10:36 +0100 (CET)","from [192.168.1.106] (unknown [103.251.226.254])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 8007E3E7;\n\tThu, 11 Nov 2021 18:10:35 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"rGUT+PMD\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1636650636;\n\tbh=k2GGVDolQ0aDqzvBoEwmNJ7Sb1BBza01ZDodUAowIs0=;\n\th=Subject:To:References:From:Date:In-Reply-To:From;\n\tb=rGUT+PMDgagniveZSJ33iMxeQoMjwyRWsIAFKodlOuBF+3I6Ck434CFH6Qak/Jjdz\n\tFpoPcbeMx8YNprJDZQyuL1aT5FNo3eACJC0LMVu18Ccmfi/Kf1SzftMCuy8BqWXLRX\n\tASs6IZcov5/07sSkS/D9aEPADHErOMhKykkLiyIA=","To":"Hirokazu Honda <hiroh@chromium.org>, libcamera-devel@lists.libcamera.org","References":"<20211029041424.1430886-1-hiroh@chromium.org>\n\t<20211029041424.1430886-4-hiroh@chromium.org>","From":"Umang Jain <umang.jain@ideasonboard.com>","Message-ID":"<887b0cf3-a0fc-f515-d272-8f9288248f50@ideasonboard.com>","Date":"Thu, 11 Nov 2021 22:40:29 +0530","User-Agent":"Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101\n\tThunderbird/78.10.2","MIME-Version":"1.0","In-Reply-To":"<20211029041424.1430886-4-hiroh@chromium.org>","Content-Type":"text/plain; charset=utf-8; format=flowed","Content-Transfer-Encoding":"7bit","Content-Language":"en-US","Subject":"Re: [libcamera-devel] [RFC PATCH 3/6] android: camera_hal_manager:\n\tAdd thread safety annotation","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>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}},{"id":20884,"web_url":"https://patchwork.libcamera.org/comment/20884/","msgid":"<YY2fFbU6zgoLFThZ@pendragon.ideasonboard.com>","date":"2021-11-11T22:54:13","subject":"Re: [libcamera-devel] [RFC PATCH 3/6] android: camera_hal_manager:\n\tAdd thread safety annotation","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Hiro,\n\nThank you for the patch.\n\nOn Fri, Oct 29, 2021 at 01:14:21PM +0900, Hirokazu Honda wrote:\n> This applies clang thread safety annotation to CameraHalManager.\n> Mutex and MutexLocker there are replaced with Mutex2 and\n> MutexLocer2.\n\ns/Locer/Locker/\n\n> Signed-off-by: Hirokazu Honda <hiroh@chromium.org>\n\nThis looks fine, except for the *2 suffix that I'd like to avoid. This\nis discussed in 2/6, so for this patch,\n\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\n> ---\n>  src/android/camera_hal_manager.cpp | 10 +++++-----\n>  src/android/camera_hal_manager.h   | 14 ++++++--------\n>  2 files changed, 11 insertions(+), 13 deletions(-)\n> \n> diff --git a/src/android/camera_hal_manager.cpp b/src/android/camera_hal_manager.cpp\n> index 5f7bfe26..26715a5f 100644\n> --- a/src/android/camera_hal_manager.cpp\n> +++ b/src/android/camera_hal_manager.cpp\n> @@ -75,7 +75,7 @@ int CameraHalManager::init()\n>  std::tuple<CameraDevice *, int>\n>  CameraHalManager::open(unsigned int id, const hw_module_t *hardwareModule)\n>  {\n> -\tMutexLocker locker(mutex_);\n> +\tMutexLocker2 locker(mutex_);\n>  \n>  \tif (!callbacks_) {\n>  \t\tLOG(HAL, Error) << \"Can't open camera before callbacks are set\";\n> @@ -103,7 +103,7 @@ void CameraHalManager::cameraAdded(std::shared_ptr<Camera> cam)\n>  \tbool isCameraExternal = false;\n>  \tbool isCameraNew = false;\n>  \n> -\tMutexLocker locker(mutex_);\n> +\tMutexLocker2 locker(mutex_);\n>  \n>  \t/*\n>  \t * Each camera is assigned a unique integer ID when it is seen for the\n> @@ -198,7 +198,7 @@ void CameraHalManager::cameraAdded(std::shared_ptr<Camera> cam)\n>  \n>  void CameraHalManager::cameraRemoved(std::shared_ptr<Camera> cam)\n>  {\n> -\tMutexLocker locker(mutex_);\n> +\tMutexLocker2 locker(mutex_);\n>  \n>  \tauto iter = std::find_if(cameras_.begin(), cameras_.end(),\n>  \t\t\t\t [&cam](const std::unique_ptr<CameraDevice> &camera) {\n> @@ -257,7 +257,7 @@ int CameraHalManager::getCameraInfo(unsigned int id, struct camera_info *info)\n>  \tif (!info)\n>  \t\treturn -EINVAL;\n>  \n> -\tMutexLocker locker(mutex_);\n> +\tMutexLocker2 locker(mutex_);\n>  \n>  \tCameraDevice *camera = cameraDeviceFromHalId(id);\n>  \tif (!camera) {\n> @@ -280,7 +280,7 @@ void CameraHalManager::setCallbacks(const camera_module_callbacks_t *callbacks)\n>  {\n>  \tcallbacks_ = callbacks;\n>  \n> -\tMutexLocker locker(mutex_);\n> +\tMutexLocker2 locker(mutex_);\n>  \n>  \t/*\n>  \t * Some external cameras may have been identified before the callbacks_\n> diff --git a/src/android/camera_hal_manager.h b/src/android/camera_hal_manager.h\n> index 3f6d302a..9b8c3a1f 100644\n> --- a/src/android/camera_hal_manager.h\n> +++ b/src/android/camera_hal_manager.h\n> @@ -8,7 +8,6 @@\n>  #define __ANDROID_CAMERA_MANAGER_H__\n>  \n>  #include <map>\n> -#include <mutex>\n>  #include <stddef.h>\n>  #include <tuple>\n>  #include <vector>\n> @@ -18,6 +17,8 @@\n>  #include <system/camera_metadata.h>\n>  \n>  #include <libcamera/base/class.h>\n> +#include <libcamera/base/mutex.h>\n> +#include <libcamera/base/thread_annotations.h>\n>  \n>  #include <libcamera/camera_manager.h>\n>  \n> @@ -44,9 +45,6 @@ public:\n>  private:\n>  \tLIBCAMERA_DISABLE_COPY_AND_MOVE(CameraHalManager)\n>  \n> -\tusing Mutex = std::mutex;\n> -\tusing MutexLocker = std::unique_lock<std::mutex>;\n> -\n>  \tstatic constexpr unsigned int firstExternalCameraId_ = 1000;\n>  \n>  \tCameraHalManager();\n> @@ -56,15 +54,15 @@ private:\n>  \tvoid cameraAdded(std::shared_ptr<libcamera::Camera> cam);\n>  \tvoid cameraRemoved(std::shared_ptr<libcamera::Camera> cam);\n>  \n> -\tCameraDevice *cameraDeviceFromHalId(unsigned int id);\n> +\tCameraDevice *cameraDeviceFromHalId(unsigned int id) REQUIRES(mutex_);\n>  \n>  \tstd::unique_ptr<libcamera::CameraManager> cameraManager_;\n>  \tCameraHalConfig halConfig_;\n>  \n>  \tconst camera_module_callbacks_t *callbacks_;\n> -\tstd::vector<std::unique_ptr<CameraDevice>> cameras_;\n> -\tstd::map<std::string, unsigned int> cameraIdsMap_;\n> -\tMutex mutex_;\n> +\tstd::vector<std::unique_ptr<CameraDevice>> cameras_ GUARDED_BY(mutex_);\n> +\tstd::map<std::string, unsigned int> cameraIdsMap_ GUARDED_BY(mutex_);\n> +\tlibcamera::Mutex2 mutex_;\n>  \n>  \tunsigned int numInternalCameras_;\n>  \tunsigned int nextExternalCameraId_;","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 EEBD5BF415\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 11 Nov 2021 22:54:36 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id A7ED460345;\n\tThu, 11 Nov 2021 23:54:36 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 66F8E600B5\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 11 Nov 2021 23:54:35 +0100 (CET)","from pendragon.ideasonboard.com\n\t(117.145-247-81.adsl-dyn.isp.belgacom.be [81.247.145.117])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 0F369556;\n\tThu, 11 Nov 2021 23:54:35 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"bFsfJujv\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1636671275;\n\tbh=maGUBC6P0h6/L4lMHazouE6Ll0t7qbV/Bs6/uhEnJjo=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=bFsfJujv3S9USTGW4zwrPApy3cSUlN9JiGaL7UIau8nC6CqJMVb7OqH7/yrJa87cj\n\tfFTU68A3PuFk2UP3LEOGXKjgqcrSSBrTv9w0IUKnzZXisxAWGnjGzp5dcldxew5k71\n\tJPh+BtN6Tq624VEoJ8EsQxPRUT2htfYhNNhSym6Y=","Date":"Fri, 12 Nov 2021 00:54:13 +0200","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Hirokazu Honda <hiroh@chromium.org>","Message-ID":"<YY2fFbU6zgoLFThZ@pendragon.ideasonboard.com>","References":"<20211029041424.1430886-1-hiroh@chromium.org>\n\t<20211029041424.1430886-4-hiroh@chromium.org>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20211029041424.1430886-4-hiroh@chromium.org>","Subject":"Re: [libcamera-devel] [RFC PATCH 3/6] android: camera_hal_manager:\n\tAdd thread safety annotation","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>"}}]