[libcamera-devel,v5,00/12] Introduce clang thread safety annotations
mbox series

Message ID 20211201075348.3121186-1-hiroh@chromium.org
Headers show
Series
  • Introduce clang thread safety annotations
Related show

Message

Hirokazu Honda Dec. 1, 2021, 7:53 a.m. UTC
This introduces clang thread safety annotations and applies it
to libcamera Android HAL adaptation layer. It is necessary to add
new classes that wrap std::mutex, std::unique_lock and
std::condition_variable with the annotations. It is because no
C++ standard library annotates all the mutex classes today.
libstdc++ doesn't annotate any classes. libc++ annotates for
std::mutex, std::lock_guard and std::scoped_lock, but doesn't for
std::unique_lock.

Therefore it is possible to use std::mutex if libc++ is used.
Althoug I don't do so In this patch series because the existing
code using std::mutex is not annotated yet.

Document: https://clang.llvm.org/docs/ThreadSafetyAnalysis.html

Change in v5:
* 05/12: Address comments from Laurent

Hirokazu Honda (12):
  libcamera: base: Introduce ConditionVariable
  android: Consolidate mutex classes to Mutex and MutexLocker
  v4l2: Consolidate mutex classes to Mutex and MutexLocker
  libcamera: base: Add thread safety annotation macros
  libcamera: base: Add mutex classes with thread safety annotations
  libcamera: Correct include headers for Mutex classes
  android: camera_hal_manager: Add thread safety annotation
  android: camera_stream: Protect buffers initialization by mutex_
  android: camera_stream: Add thread safety annotation
  android: camera_device: Fix variables access without protection
  android: camera_device: Add thread safety annotation
  android: camera_request: Add thread safety annotation

 include/libcamera/base/meson.build          |   2 +
 include/libcamera/base/mutex.h              | 132 ++++++++++++++++++++
 include/libcamera/base/semaphore.h          |   7 +-
 include/libcamera/base/thread.h             |   4 -
 include/libcamera/base/thread_annotations.h |  80 ++++++++++++
 meson.build                                 |   1 +
 src/android/camera_device.cpp               |  10 +-
 src/android/camera_device.h                 |  17 +--
 src/android/camera_hal_manager.h            |  13 +-
 src/android/camera_request.h                |   7 +-
 src/android/camera_stream.cpp               |  15 +--
 src/android/camera_stream.h                 |  15 +--
 src/libcamera/base/meson.build              |   1 +
 src/libcamera/base/mutex.cpp                |  56 +++++++++
 src/libcamera/base/semaphore.cpp            |   1 -
 src/libcamera/base/signal.cpp               |   2 +-
 src/libcamera/base/thread.cpp               |  14 +--
 src/libcamera/camera_manager.cpp            |   4 +-
 src/v4l2/v4l2_camera.h                      |   6 +-
 src/v4l2/v4l2_camera_proxy.cpp              |   8 +-
 src/v4l2/v4l2_camera_proxy.h                |   4 +-
 21 files changed, 332 insertions(+), 67 deletions(-)
 create mode 100644 include/libcamera/base/mutex.h
 create mode 100644 include/libcamera/base/thread_annotations.h
 create mode 100644 src/libcamera/base/mutex.cpp

--
2.34.0.rc2.393.gf8c9666880-goog

Comments

Laurent Pinchart Dec. 1, 2021, 11:47 a.m. UTC | #1
On Wed, Dec 01, 2021 at 04:53:36PM +0900, Hirokazu Honda wrote:
> This introduces clang thread safety annotations and applies it
> to libcamera Android HAL adaptation layer. It is necessary to add
> new classes that wrap std::mutex, std::unique_lock and
> std::condition_variable with the annotations. It is because no
> C++ standard library annotates all the mutex classes today.
> libstdc++ doesn't annotate any classes. libc++ annotates for
> std::mutex, std::lock_guard and std::scoped_lock, but doesn't for
> std::unique_lock.
> 
> Therefore it is possible to use std::mutex if libc++ is used.
> Althoug I don't do so In this patch series because the existing
> code using std::mutex is not annotated yet.
> 
> Document: https://clang.llvm.org/docs/ThreadSafetyAnalysis.html
> 
> Change in v5:
> * 05/12: Address comments from Laurent

There were only a few very minor comments for v5, I'll address them when
applying.

> Hirokazu Honda (12):
>   libcamera: base: Introduce ConditionVariable
>   android: Consolidate mutex classes to Mutex and MutexLocker
>   v4l2: Consolidate mutex classes to Mutex and MutexLocker
>   libcamera: base: Add thread safety annotation macros
>   libcamera: base: Add mutex classes with thread safety annotations
>   libcamera: Correct include headers for Mutex classes
>   android: camera_hal_manager: Add thread safety annotation
>   android: camera_stream: Protect buffers initialization by mutex_
>   android: camera_stream: Add thread safety annotation
>   android: camera_device: Fix variables access without protection
>   android: camera_device: Add thread safety annotation
>   android: camera_request: Add thread safety annotation
> 
>  include/libcamera/base/meson.build          |   2 +
>  include/libcamera/base/mutex.h              | 132 ++++++++++++++++++++
>  include/libcamera/base/semaphore.h          |   7 +-
>  include/libcamera/base/thread.h             |   4 -
>  include/libcamera/base/thread_annotations.h |  80 ++++++++++++
>  meson.build                                 |   1 +
>  src/android/camera_device.cpp               |  10 +-
>  src/android/camera_device.h                 |  17 +--
>  src/android/camera_hal_manager.h            |  13 +-
>  src/android/camera_request.h                |   7 +-
>  src/android/camera_stream.cpp               |  15 +--
>  src/android/camera_stream.h                 |  15 +--
>  src/libcamera/base/meson.build              |   1 +
>  src/libcamera/base/mutex.cpp                |  56 +++++++++
>  src/libcamera/base/semaphore.cpp            |   1 -
>  src/libcamera/base/signal.cpp               |   2 +-
>  src/libcamera/base/thread.cpp               |  14 +--
>  src/libcamera/camera_manager.cpp            |   4 +-
>  src/v4l2/v4l2_camera.h                      |   6 +-
>  src/v4l2/v4l2_camera_proxy.cpp              |   8 +-
>  src/v4l2/v4l2_camera_proxy.h                |   4 +-
>  21 files changed, 332 insertions(+), 67 deletions(-)
>  create mode 100644 include/libcamera/base/mutex.h
>  create mode 100644 include/libcamera/base/thread_annotations.h
>  create mode 100644 src/libcamera/base/mutex.cpp
>