[libcamera-devel,7/7] ipa: raspberrypi: metadata: Apply clang thread safety annotation
diff mbox series

Message ID 20211203195539.1574621-8-hiroh@chromium.org
State New
Headers show
Series
  • Apply clang thread safety annotations to raspberrypi
Related show

Commit Message

Hirokazu Honda Dec. 3, 2021, 7:55 p.m. UTC
This annotates member variable and functions of Metadata by clang
thread safety annotations. Metadata has lock and unlock functions
so that it can be used by std::unique_lock. I replace them by a
function of returning Mutex reference. It eases to annotate clang
thread safety annotations.

Signed-off-by: Hirokazu Honda <hiroh@chromium.org>
---
 src/ipa/raspberrypi/controller/metadata.hpp | 22 ++++++++++++---------
 src/ipa/raspberrypi/controller/rpi/agc.cpp  |  3 ++-
 src/ipa/raspberrypi/controller/rpi/ccm.cpp  |  3 ++-
 src/ipa/raspberrypi/raspberrypi.cpp         |  4 ++--
 4 files changed, 19 insertions(+), 13 deletions(-)

Patch
diff mbox series

diff --git a/src/ipa/raspberrypi/controller/metadata.hpp b/src/ipa/raspberrypi/controller/metadata.hpp
index ff2e1ae3..65dad8fb 100644
--- a/src/ipa/raspberrypi/controller/metadata.hpp
+++ b/src/ipa/raspberrypi/controller/metadata.hpp
@@ -36,6 +36,7 @@  public:
 
 	template<typename T>
 	void Set(std::string const &tag, T const &value)
+		LIBCAMERA_TSA_EXCLUDES(mutex_)
 	{
 		libcamera::MutexLocker lock(mutex_);
 		data_[tag] = value;
@@ -43,6 +44,7 @@  public:
 
 	template<typename T>
 	int Get(std::string const &tag, T &value) const
+		LIBCAMERA_TSA_EXCLUDES(mutex_)
 	{
 		libcamera::MutexLocker lock(mutex_);
 		auto it = data_.find(tag);
@@ -52,13 +54,14 @@  public:
 		return 0;
 	}
 
-	void Clear()
+	void Clear() LIBCAMERA_TSA_EXCLUDES(mutex_)
 	{
 		libcamera::MutexLocker lock(mutex_);
 		data_.clear();
 	}
 
 	Metadata &operator=(Metadata const &other)
+		LIBCAMERA_TSA_EXCLUDES(mutex_, other.mutex_)
 	{
 		libcamera::MutexLocker lock(mutex_);
 		libcamera::MutexLocker other_lock(other.mutex_);
@@ -67,6 +70,7 @@  public:
 	}
 
 	Metadata &operator=(Metadata &&other)
+		LIBCAMERA_TSA_EXCLUDES(mutex_, other.mutex_)
 	{
 		libcamera::MutexLocker lock(mutex_);
 		libcamera::MutexLocker other_lock(other.mutex_);
@@ -75,7 +79,7 @@  public:
 		return *this;
 	}
 
-	void Merge(Metadata &other)
+	void Merge(Metadata &other) LIBCAMERA_TSA_EXCLUDES(mutex_, other.mutex_)
 	{
 		libcamera::MutexLocker lock(mutex_);
 		libcamera::MutexLocker other_lock(other.mutex_);
@@ -83,7 +87,7 @@  public:
 	}
 
 	template<typename T>
-	T *GetLocked(std::string const &tag)
+	T *GetLocked(std::string const &tag) LIBCAMERA_TSA_REQUIRES(mutex_)
 	{
 		// This allows in-place access to the Metadata contents,
 		// for which you should be holding the lock.
@@ -95,20 +99,20 @@  public:
 
 	template<typename T>
 	void SetLocked(std::string const &tag, T const &value)
+		LIBCAMERA_TSA_REQUIRES(mutex_)
 	{
 		// Use this only if you're holding the lock yourself.
 		data_[tag] = value;
 	}
 
-	// Note: use of (lowercase) lock and unlock means you can create scoped
-	// locks with the standard lock classes.
-	// e.g. std::lock_guard<RPiController::Metadata> lock(metadata)
-	void lock() LIBCAMERA_TSA_ACQUIRE(mutex_) { mutex_.lock(); }
-	void unlock() LIBCAMERA_TSA_RELEASE(mutex_) { mutex_.unlock(); }
+	libcamera::Mutex &mutex() LIBCAMERA_TSA_RETURN_CAPABILITY(mutex_)
+	{
+		return mutex_;
+	}
 
 private:
 	mutable libcamera::Mutex mutex_;
-	std::map<std::string, std::any> data_;
+	std::map<std::string, std::any> data_ LIBCAMERA_TSA_GUARDED_BY(mutex_);
 };
 
 } // namespace RPiController
diff --git a/src/ipa/raspberrypi/controller/rpi/agc.cpp b/src/ipa/raspberrypi/controller/rpi/agc.cpp
index f6a9cb0a..870909af 100644
--- a/src/ipa/raspberrypi/controller/rpi/agc.cpp
+++ b/src/ipa/raspberrypi/controller/rpi/agc.cpp
@@ -485,7 +485,8 @@  void Agc::housekeepConfig()
 
 void Agc::fetchCurrentExposure(Metadata *image_metadata)
 {
-	std::unique_lock<Metadata> lock(*image_metadata);
+	MutexLocker locker(image_metadata->mutex());
+
 	DeviceStatus *device_status =
 		image_metadata->GetLocked<DeviceStatus>("device.status");
 	if (!device_status)
diff --git a/src/ipa/raspberrypi/controller/rpi/ccm.cpp b/src/ipa/raspberrypi/controller/rpi/ccm.cpp
index 821a4c7c..989b15dd 100644
--- a/src/ipa/raspberrypi/controller/rpi/ccm.cpp
+++ b/src/ipa/raspberrypi/controller/rpi/ccm.cpp
@@ -85,6 +85,7 @@  void Ccm::Initialise() {}
 
 template<typename T>
 static bool get_locked(Metadata *metadata, std::string const &tag, T &value)
+	LIBCAMERA_TSA_REQUIRES(metadata->mutex())
 {
 	T *ptr = metadata->GetLocked<T>(tag);
 	if (ptr == nullptr)
@@ -128,7 +129,7 @@  void Ccm::Prepare(Metadata *image_metadata)
 	lux.lux = 400; // in case no metadata
 	{
 		// grab mutex just once to get everything
-		std::lock_guard<Metadata> lock(*image_metadata);
+		MutexLocker locker(image_metadata->mutex());
 		awb_ok = get_locked(image_metadata, "awb.status", awb);
 		lux_ok = get_locked(image_metadata, "lux.status", lux);
 	}
diff --git a/src/ipa/raspberrypi/raspberrypi.cpp b/src/ipa/raspberrypi/raspberrypi.cpp
index fed82e22..b8d22dec 100644
--- a/src/ipa/raspberrypi/raspberrypi.cpp
+++ b/src/ipa/raspberrypi/raspberrypi.cpp
@@ -465,7 +465,7 @@  void IPARPi::signalIspPrepare(const ipa::RPi::ISPConfig &data)
 
 void IPARPi::reportMetadata()
 {
-	std::unique_lock<RPiController::Metadata> lock(rpiMetadata_);
+	MutexLocker locker(rpiMetadata_.mutex());
 
 	/*
 	 * Certain information about the current frame and how it will be
@@ -977,7 +977,7 @@  void IPARPi::prepareISP(const ipa::RPi::ISPConfig &data)
 	controller_.Prepare(&rpiMetadata_);
 
 	/* Lock the metadata buffer to avoid constant locks/unlocks. */
-	std::unique_lock<RPiController::Metadata> lock(rpiMetadata_);
+	MutexLocker locker(rpiMetadata_.mutex());
 
 	AwbStatus *awbStatus = rpiMetadata_.GetLocked<AwbStatus>("awb.status");
 	if (awbStatus)