[{"id":21493,"web_url":"https://patchwork.libcamera.org/comment/21493/","msgid":"<Yack692dolplYzTO@pendragon.ideasonboard.com>","date":"2021-12-01T07:31:55","subject":"Re: [libcamera-devel] [PATCH v4 05/12] libcamera: base: Add mutex\n\tclasses with thread safety annotations","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 Wed, Dec 01, 2021 at 04:07:21PM +0900, Hirokazu Honda wrote:\n> This replaces Mutex and MutexLocker with our own defined classes.\n> The classes are annotated by clang thread safety annotations.\n> So we can add annotation to code where the classes are used.\n> \n> v4l2 code needs to be annotated, which violates Mutex capability.\n> \n> Signed-off-by: Hirokazu Honda <hiroh@chromium.org>\n> ---\n>  include/libcamera/base/meson.build |   1 +\n>  include/libcamera/base/mutex.h     | 132 +++++++++++++++++++++++++++++\n>  include/libcamera/base/thread.h    |   7 +-\n>  src/libcamera/base/meson.build     |   1 +\n>  src/libcamera/base/mutex.cpp       |  65 ++++++++++++++\n>  src/libcamera/base/thread.cpp      |  15 ----\n>  src/v4l2/v4l2_camera_proxy.h       |   4 +-\n>  7 files changed, 202 insertions(+), 23 deletions(-)\n>  create mode 100644 include/libcamera/base/mutex.h\n>  create mode 100644 src/libcamera/base/mutex.cpp\n> \n> diff --git a/include/libcamera/base/meson.build b/include/libcamera/base/meson.build\n> index 1a71ce5a..37c4435a 100644\n> --- a/include/libcamera/base/meson.build\n> +++ b/include/libcamera/base/meson.build\n> @@ -13,6 +13,7 @@ libcamera_base_headers = files([\n>      'flags.h',\n>      'log.h',\n>      'message.h',\n> +    'mutex.h',\n>      'object.h',\n>      'private.h',\n>      'semaphore.h',\n> diff --git a/include/libcamera/base/mutex.h b/include/libcamera/base/mutex.h\n> new file mode 100644\n> index 00000000..8b70508e\n> --- /dev/null\n> +++ b/include/libcamera/base/mutex.h\n> @@ -0,0 +1,132 @@\n> +/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> +/*\n> + * Copyright (C) 2021, Google Inc.\n> + *\n> + * mutex.h - Mutex classes with clang thread safety annotation\n> + */\n> +\n> +#pragma once\n> +\n> +#include <condition_variable>\n> +#include <mutex>\n> +\n> +#include <libcamera/base/thread_annotations.h>\n> +\n> +namespace libcamera {\n> +\n> +/* \\todo using Mutex = std::mutex if lib++ is used. */\n> +\n> +#ifndef __DOXYGEN__\n> +\n> +class LIBCAMERA_TSA_CAPABILITY(\"mutex\") Mutex final\n> +{\n> +public:\n> +\tconstexpr Mutex()\n> +\t{\n> +\t}\n> +\n> +\tvoid lock() LIBCAMERA_TSA_ACQUIRE()\n> +\t{\n> +\t\tmutex_.lock();\n> +\t}\n> +\n> +\tvoid unlock() LIBCAMERA_TSA_RELEASE()\n> +\t{\n> +\t\tmutex_.unlock();\n> +\t}\n> +\n> +private:\n> +\tfriend class MutexLocker;\n> +\n> +\tstd::mutex mutex_;\n> +};\n> +\n> +class LIBCAMERA_TSA_SCOPED_CAPABILITY MutexLocker final\n> +{\n> +public:\n> +\texplicit MutexLocker(Mutex &mutex) LIBCAMERA_TSA_ACQUIRE(mutex)\n> +\t\t: lock_(mutex.mutex_)\n> +\t{\n> +\t}\n> +\n> +\tMutexLocker(Mutex &mutex, std::defer_lock_t t) noexcept LIBCAMERA_TSA_EXCLUDES(mutex)\n> +\t\t: lock_(mutex.mutex_, t)\n> +\t{\n> +\t}\n> +\n> +\t~MutexLocker() LIBCAMERA_TSA_RELEASE()\n> +\t{\n> +\t}\n> +\n> +\tvoid lock() LIBCAMERA_TSA_ACQUIRE()\n> +\t{\n> +\t\tlock_.lock();\n> +\t}\n> +\n> +\tbool try_lock() LIBCAMERA_TSA_TRY_ACQUIRE(true)\n> +\t{\n> +\t\treturn lock_.try_lock();\n> +\t}\n> +\n> +\tvoid unlock() LIBCAMERA_TSA_RELEASE()\n> +\t{\n> +\t\tlock_.unlock();\n> +\t}\n> +\n> +private:\n> +\tfriend class ConditionVariable;\n> +\n> +\tstd::unique_lock<std::mutex> lock_;\n> +};\n> +\n> +class ConditionVariable final\n> +{\n> +public:\n> +\tConditionVariable()\n> +\t{\n> +\t}\n> +\n> +\tvoid notify_one() noexcept\n> +\t{\n> +\t\tcv_.notify_one();\n> +\t}\n> +\n> +\tvoid notify_all() noexcept\n> +\t{\n> +\t\tcv_.notify_all();\n> +\t}\n> +\n> +\ttemplate<class Predicate>\n> +\tvoid wait(MutexLocker &locker, Predicate stopWaiting)\n> +\t{\n> +\t\tcv_.wait(locker.lock_, stopWaiting);\n> +\t}\n> +\n> +\ttemplate<class Rep, class Period, class Predicate>\n> +\tbool wait_for(MutexLocker &locker,\n> +\t\t      const std::chrono::duration<Rep, Period> &relTime,\n> +\t\t      Predicate stopWaiting)\n> +\t{\n> +\t\treturn cv_.wait_for(locker.lock_, relTime, stopWaiting);\n> +\t}\n> +\n> +private:\n> +\tstd::condition_variable cv_;\n> +};\n> +\n> +#else /* __DOXYGEN__ */\n> +\n> +class Mutex final\n> +{\n> +};\n> +\n> +class MutexLocker final\n> +{\n> +};\n> +\n> +class ConditionVariable final\n> +{\n> +};\n> +\n> +#endif /* __DOXYGEN__ */\n> +} /* namespace libcamera */\n> diff --git a/include/libcamera/base/thread.h b/include/libcamera/base/thread.h\n> index 1ebf8363..44678c34 100644\n> --- a/include/libcamera/base/thread.h\n> +++ b/include/libcamera/base/thread.h\n> @@ -7,15 +7,14 @@\n>  \n>  #pragma once\n>  \n> -#include <condition_variable>\n>  #include <memory>\n> -#include <mutex>\n>  #include <sys/types.h>\n>  #include <thread>\n>  \n>  #include <libcamera/base/private.h>\n>  \n>  #include <libcamera/base/message.h>\n> +#include <libcamera/base/mutex.h>\n>  #include <libcamera/base/signal.h>\n>  #include <libcamera/base/utils.h>\n>  \n> @@ -27,10 +26,6 @@ class Object;\n>  class ThreadData;\n>  class ThreadMain;\n>  \n> -using ConditionVariable = std::condition_variable;\n> -using Mutex = std::mutex;\n> -using MutexLocker = std::unique_lock<std::mutex>;\n> -\n>  class Thread\n>  {\n>  public:\n> diff --git a/src/libcamera/base/meson.build b/src/libcamera/base/meson.build\n> index 05fed7ac..b93b8505 100644\n> --- a/src/libcamera/base/meson.build\n> +++ b/src/libcamera/base/meson.build\n> @@ -11,6 +11,7 @@ libcamera_base_sources = files([\n>      'flags.cpp',\n>      'log.cpp',\n>      'message.cpp',\n> +    'mutex.cpp',\n>      'object.cpp',\n>      'semaphore.cpp',\n>      'signal.cpp',\n> diff --git a/src/libcamera/base/mutex.cpp b/src/libcamera/base/mutex.cpp\n> new file mode 100644\n> index 00000000..313bcb20\n> --- /dev/null\n> +++ b/src/libcamera/base/mutex.cpp\n> @@ -0,0 +1,65 @@\n> +/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> +/*\n> + * Copyright (C) 2021, Google Inc.\n> + *\n> + * mutex.cpp - Mutex classes with clang thread safety annotation\n> + */\n> +\n> +#include <libcamera/base/mutex.h>\n> +\n> +/**\n> + * \\file base/mutex.h\n> + * \\brief Mutex classes with clang thread safety annotation\n> + */\n> +\n> +namespace libcamera {\n> +\n> +/**\n> + * \\class Mutex\n> + * \\brief std::mutex wrapper with clang thread safety annotation\n> + *\n> + * The Mutex class wraps a std::mutex instance to add clang thread safety\n> + * annotation support. The class exposes the same interface as std::mutex and\n> + * can be used as a transparent replacement. It integrates with the\n> + * MutexLocker and ConditionVariable classes.\n> + *\n> + * See https://en.cppreference.com/w/cpp/thread/mutex for the complete API\n> + * documentation.\n> + */\n> +\n> +/**\n> + * \\class MutexLocker\n> + * \\brief std::unique_lock wrapper with clang thread safety annotation\n> + *\n> + * The MutexLocker class wraps a std::unique_lock instance to add clang thread\n> + * safety annotation support. The class exposes the same interface as\n> + * std::unique_lock and can be used as a transparent replacement. It integrates\n> + *  with the Mutex and ConditionVariable classes.\n\nExtra space before \"with\".\n\n> + *\n> + * See https://en.cppreference.com/w/cpp/thread/mutex for the complete API\n\nThis should point to the unique_lock documentation.\n\n> + * documentation.\n> + */\n> +\n> +/**\n> + * \\class MutexLocker\n> + * \\brief std::unique_lock wrapper with clang thread safety annotation\n> + *\n> + * The MutexLocker class wraps a std::unique_lock instance to add clang thread\n> + * safety annotation support. The class exposes the same interface as\n> + * std::unique_lock and can be used as a transparent replacement. It integrates\n> + *  with the Mutex and ConditionVariable classes.\n> + *\n> + * See https://en.cppreference.com/w/cpp/thread/mutex for the complete API\n> + * documentation.\n> + */\n\nDuplicate documentation for MutexLocker ?\n\n> +\n> +/**\n> + * \\class ConditionVariable\n> + * \\brief std::condition_variable wrapper integrating with MutexLocker\n> + *\n> + * The ConditionVariable class wraps a std::condition_variable instance to\n> + * integrate MutexLocker. The class exposes the same interface as\n\ns/integrate MutexLocker/integrate with the MutexLocker class/\n\n> + * std::condition_variable and can be used as a transparent replacement.\n\nA pointer to the condition_variable documentation would be useful here\ntoo, like for mutex and unique_lock.\n\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\n> + */\n> +\n> +} /* namespace libcamera */\n> diff --git a/src/libcamera/base/thread.cpp b/src/libcamera/base/thread.cpp\n> index b893135f..b2043b7e 100644\n> --- a/src/libcamera/base/thread.cpp\n> +++ b/src/libcamera/base/thread.cpp\n> @@ -204,21 +204,6 @@ ThreadData *ThreadData::current()\n>  \treturn data;\n>  }\n>  \n> -/**\n> - * \\typedef ConditionVariable\n> - * \\brief An alias for std::condition_variable\n> - */\n> -\n> -/**\n> - * \\typedef Mutex\n> - * \\brief An alias for std::mutex\n> - */\n> -\n> -/**\n> - * \\typedef MutexLocker\n> - * \\brief An alias for std::unique_lock<std::mutex>\n> - */\n> -\n>  /**\n>   * \\class Thread\n>   * \\brief A thread of execution\n> diff --git a/src/v4l2/v4l2_camera_proxy.h b/src/v4l2/v4l2_camera_proxy.h\n> index 040954dd..fa0a49e0 100644\n> --- a/src/v4l2/v4l2_camera_proxy.h\n> +++ b/src/v4l2/v4l2_camera_proxy.h\n> @@ -14,7 +14,7 @@\n>  #include <sys/types.h>\n>  #include <vector>\n>  \n> -#include <libcamera/base/thread.h>\n> +#include <libcamera/base/mutex.h>\n>  \n>  #include <libcamera/camera.h>\n>  \n> @@ -59,7 +59,7 @@ private:\n>  \tint vidioc_querybuf(V4L2CameraFile *file, struct v4l2_buffer *arg);\n>  \tint vidioc_qbuf(V4L2CameraFile *file, struct v4l2_buffer *arg);\n>  \tint vidioc_dqbuf(V4L2CameraFile *file, struct v4l2_buffer *arg,\n> -\t\t\t libcamera::Mutex *lock);\n> +\t\t\t libcamera::Mutex *lock) LIBCAMERA_TSA_REQUIRES(*lock);\n>  \tint vidioc_streamon(V4L2CameraFile *file, int *arg);\n>  \tint vidioc_streamoff(V4L2CameraFile *file, int *arg);\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 747F8BDB13\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed,  1 Dec 2021 07:32:23 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id BB2F960718;\n\tWed,  1 Dec 2021 08:32:22 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 4650B60592\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed,  1 Dec 2021 08:32:21 +0100 (CET)","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 ACE83A15;\n\tWed,  1 Dec 2021 08:32:20 +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=\"ZhjADs8C\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1638343940;\n\tbh=sZTq1fAhZAIn+N/m29z8VH7Kc6cFFSp2yJZPR2B6lJo=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=ZhjADs8C1FEBOTooktcNjdYrfqF8kRdG7GmDvc8rcDgft7Uj9QlUbDKUPQdRAGV/x\n\tI3pHTUnvNLKjM5yRLT7iEEf11vODZSrYXv0wIPQp/4zseYCl57s4r/Ffo5yt02PyuS\n\tOYlCs/1WC5ksoMSFwc2Wod5w+x5n4YKZuoWpAQc8=","Date":"Wed, 1 Dec 2021 09:31:55 +0200","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Hirokazu Honda <hiroh@chromium.org>","Message-ID":"<Yack692dolplYzTO@pendragon.ideasonboard.com>","References":"<20211201070728.3114247-1-hiroh@chromium.org>\n\t<20211201070728.3114247-6-hiroh@chromium.org>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20211201070728.3114247-6-hiroh@chromium.org>","Subject":"Re: [libcamera-devel] [PATCH v4 05/12] libcamera: base: Add mutex\n\tclasses with thread safety annotations","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>"}}]