[libcamera-devel,1/7] ipa: raspberrypi: Use libcamera Mutex class
diff mbox series

Message ID 20211203195539.1574621-2-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 replaces std::mutex, std::unique_lock and
std::condition_variable in src/ipa/raspberrypi with libcamera
Mutex, MutexLocker and ConditionVariable, respectively.

Signed-off-by: Hirokazu Honda <hiroh@chromium.org>
---
 src/ipa/raspberrypi/controller/metadata.hpp   | 28 +++++++++++--------
 src/ipa/raspberrypi/controller/rpi/alsc.cpp   | 12 ++++----
 src/ipa/raspberrypi/controller/rpi/alsc.hpp   | 10 +++----
 src/ipa/raspberrypi/controller/rpi/awb.cpp    | 10 +++----
 src/ipa/raspberrypi/controller/rpi/awb.hpp    | 10 +++----
 .../raspberrypi/controller/rpi/contrast.cpp   |  4 +--
 .../raspberrypi/controller/rpi/contrast.hpp   |  4 +--
 src/ipa/raspberrypi/controller/rpi/lux.cpp    |  4 +--
 src/ipa/raspberrypi/controller/rpi/lux.hpp    |  5 ++--
 9 files changed, 45 insertions(+), 42 deletions(-)

Patch
diff mbox series

diff --git a/src/ipa/raspberrypi/controller/metadata.hpp b/src/ipa/raspberrypi/controller/metadata.hpp
index 51e576cf..ff2e1ae3 100644
--- a/src/ipa/raspberrypi/controller/metadata.hpp
+++ b/src/ipa/raspberrypi/controller/metadata.hpp
@@ -10,9 +10,10 @@ 
 
 #include <any>
 #include <map>
-#include <mutex>
 #include <string>
 
+#include <libcamera/base/mutex.h>
+
 namespace RPiController {
 
 class Metadata
@@ -22,13 +23,13 @@  public:
 
 	Metadata(Metadata const &other)
 	{
-		std::scoped_lock other_lock(other.mutex_);
+		libcamera::MutexLocker other_lock(other.mutex_);
 		data_ = other.data_;
 	}
 
 	Metadata(Metadata &&other)
 	{
-		std::scoped_lock other_lock(other.mutex_);
+		libcamera::MutexLocker other_lock(other.mutex_);
 		data_ = std::move(other.data_);
 		other.data_.clear();
 	}
@@ -36,14 +37,14 @@  public:
 	template<typename T>
 	void Set(std::string const &tag, T const &value)
 	{
-		std::scoped_lock lock(mutex_);
+		libcamera::MutexLocker lock(mutex_);
 		data_[tag] = value;
 	}
 
 	template<typename T>
 	int Get(std::string const &tag, T &value) const
 	{
-		std::scoped_lock lock(mutex_);
+		libcamera::MutexLocker lock(mutex_);
 		auto it = data_.find(tag);
 		if (it == data_.end())
 			return -1;
@@ -53,20 +54,22 @@  public:
 
 	void Clear()
 	{
-		std::scoped_lock lock(mutex_);
+		libcamera::MutexLocker lock(mutex_);
 		data_.clear();
 	}
 
 	Metadata &operator=(Metadata const &other)
 	{
-		std::scoped_lock lock(mutex_, other.mutex_);
+		libcamera::MutexLocker lock(mutex_);
+		libcamera::MutexLocker other_lock(other.mutex_);
 		data_ = other.data_;
 		return *this;
 	}
 
 	Metadata &operator=(Metadata &&other)
 	{
-		std::scoped_lock lock(mutex_, other.mutex_);
+		libcamera::MutexLocker lock(mutex_);
+		libcamera::MutexLocker other_lock(other.mutex_);
 		data_ = std::move(other.data_);
 		other.data_.clear();
 		return *this;
@@ -74,7 +77,8 @@  public:
 
 	void Merge(Metadata &other)
 	{
-		std::scoped_lock lock(mutex_, other.mutex_);
+		libcamera::MutexLocker lock(mutex_);
+		libcamera::MutexLocker other_lock(other.mutex_);
 		data_.merge(other.data_);
 	}
 
@@ -99,11 +103,11 @@  public:
 	// 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() { mutex_.lock(); }
-	void unlock() { mutex_.unlock(); }
+	void lock() LIBCAMERA_TSA_ACQUIRE(mutex_) { mutex_.lock(); }
+	void unlock() LIBCAMERA_TSA_RELEASE(mutex_) { mutex_.unlock(); }
 
 private:
-	mutable std::mutex mutex_;
+	mutable libcamera::Mutex mutex_;
 	std::map<std::string, std::any> data_;
 };
 
diff --git a/src/ipa/raspberrypi/controller/rpi/alsc.cpp b/src/ipa/raspberrypi/controller/rpi/alsc.cpp
index be3d1ae4..97a05a30 100644
--- a/src/ipa/raspberrypi/controller/rpi/alsc.cpp
+++ b/src/ipa/raspberrypi/controller/rpi/alsc.cpp
@@ -35,7 +35,7 @@  Alsc::Alsc(Controller *controller)
 Alsc::~Alsc()
 {
 	{
-		std::lock_guard<std::mutex> lock(mutex_);
+		MutexLocker lock(mutex_);
 		async_abort_ = true;
 	}
 	async_signal_.notify_one();
@@ -179,7 +179,7 @@  void Alsc::waitForAysncThread()
 {
 	if (async_started_) {
 		async_started_ = false;
-		std::unique_lock<std::mutex> lock(mutex_);
+		MutexLocker lock(mutex_);
 		sync_signal_.wait(lock, [&] {
 			return async_finished_;
 		});
@@ -314,7 +314,7 @@  void Alsc::restartAsync(StatisticsPtr &stats, Metadata *image_metadata)
 	frame_phase_ = 0;
 	async_started_ = true;
 	{
-		std::lock_guard<std::mutex> lock(mutex_);
+		MutexLocker lock(mutex_);
 		async_start_ = true;
 	}
 	async_signal_.notify_one();
@@ -332,7 +332,7 @@  void Alsc::Prepare(Metadata *image_metadata)
 	LOG(RPiAlsc, Debug)
 		<< "frame_count " << frame_count_ << " speed " << speed;
 	{
-		std::unique_lock<std::mutex> lock(mutex_);
+		MutexLocker lock(mutex_);
 		if (async_started_ && async_finished_)
 			fetchAsyncResults();
 	}
@@ -370,7 +370,7 @@  void Alsc::asyncFunc()
 {
 	while (true) {
 		{
-			std::unique_lock<std::mutex> lock(mutex_);
+			MutexLocker lock(mutex_);
 			async_signal_.wait(lock, [&] {
 				return async_start_ || async_abort_;
 			});
@@ -380,7 +380,7 @@  void Alsc::asyncFunc()
 		}
 		doAlsc();
 		{
-			std::lock_guard<std::mutex> lock(mutex_);
+			MutexLocker lock(mutex_);
 			async_finished_ = true;
 		}
 		sync_signal_.notify_one();
diff --git a/src/ipa/raspberrypi/controller/rpi/alsc.hpp b/src/ipa/raspberrypi/controller/rpi/alsc.hpp
index 9616b99e..3f9d6837 100644
--- a/src/ipa/raspberrypi/controller/rpi/alsc.hpp
+++ b/src/ipa/raspberrypi/controller/rpi/alsc.hpp
@@ -6,10 +6,10 @@ 
  */
 #pragma once
 
-#include <mutex>
-#include <condition_variable>
 #include <thread>
 
+#include <libcamera/base/mutex.h>
+
 #include "../algorithm.hpp"
 #include "../alsc_status.h"
 
@@ -63,11 +63,11 @@  private:
 	double luminance_table_[ALSC_CELLS_X * ALSC_CELLS_Y];
 	std::thread async_thread_;
 	void asyncFunc(); // asynchronous thread function
-	std::mutex mutex_;
+	libcamera::Mutex mutex_;
 	// condvar for async thread to wait on
-	std::condition_variable async_signal_;
+	libcamera::ConditionVariable async_signal_;
 	// condvar for synchronous thread to wait on
-	std::condition_variable sync_signal_;
+	libcamera::ConditionVariable sync_signal_;
 	// for sync thread to check  if async thread finished (requires mutex)
 	bool async_finished_;
 	// for async thread to check if it's been told to run (requires mutex)
diff --git a/src/ipa/raspberrypi/controller/rpi/awb.cpp b/src/ipa/raspberrypi/controller/rpi/awb.cpp
index 5cfd33a3..b4e45abf 100644
--- a/src/ipa/raspberrypi/controller/rpi/awb.cpp
+++ b/src/ipa/raspberrypi/controller/rpi/awb.cpp
@@ -132,7 +132,7 @@  Awb::Awb(Controller *controller)
 Awb::~Awb()
 {
 	{
-		std::lock_guard<std::mutex> lock(mutex_);
+		MutexLocker lock(mutex_);
 		async_abort_ = true;
 	}
 	async_signal_.notify_one();
@@ -252,7 +252,7 @@  void Awb::restartAsync(StatisticsPtr &stats, double lux)
 				     sizeof(async_results_.mode) - 1);
 	async_results_.mode[len] = '\0';
 	{
-		std::lock_guard<std::mutex> lock(mutex_);
+		MutexLocker lock(mutex_);
 		async_start_ = true;
 	}
 	async_signal_.notify_one();
@@ -268,7 +268,7 @@  void Awb::Prepare(Metadata *image_metadata)
 	LOG(RPiAwb, Debug)
 		<< "frame_count " << frame_count_ << " speed " << speed;
 	{
-		std::unique_lock<std::mutex> lock(mutex_);
+		MutexLocker lock(mutex_);
 		if (async_started_ && async_finished_)
 			fetchAsyncResults();
 	}
@@ -317,7 +317,7 @@  void Awb::asyncFunc()
 {
 	while (true) {
 		{
-			std::unique_lock<std::mutex> lock(mutex_);
+			MutexLocker lock(mutex_);
 			async_signal_.wait(lock, [&] {
 				return async_start_ || async_abort_;
 			});
@@ -327,7 +327,7 @@  void Awb::asyncFunc()
 		}
 		doAwb();
 		{
-			std::lock_guard<std::mutex> lock(mutex_);
+			MutexLocker lock(mutex_);
 			async_finished_ = true;
 		}
 		sync_signal_.notify_one();
diff --git a/src/ipa/raspberrypi/controller/rpi/awb.hpp b/src/ipa/raspberrypi/controller/rpi/awb.hpp
index 8af1f27c..30f39c4a 100644
--- a/src/ipa/raspberrypi/controller/rpi/awb.hpp
+++ b/src/ipa/raspberrypi/controller/rpi/awb.hpp
@@ -6,10 +6,10 @@ 
  */
 #pragma once
 
-#include <mutex>
-#include <condition_variable>
 #include <thread>
 
+#include <libcamera/base/mutex.h>
+
 #include "../awb_algorithm.hpp"
 #include "../pwl.hpp"
 #include "../awb_status.h"
@@ -108,11 +108,11 @@  private:
 	AwbConfig config_;
 	std::thread async_thread_;
 	void asyncFunc(); // asynchronous thread function
-	std::mutex mutex_;
+	libcamera::Mutex mutex_;
 	// condvar for async thread to wait on
-	std::condition_variable async_signal_;
+	libcamera::ConditionVariable async_signal_;
 	// condvar for synchronous thread to wait on
-	std::condition_variable sync_signal_;
+	libcamera::ConditionVariable sync_signal_;
 	// for sync thread to check  if async thread finished (requires mutex)
 	bool async_finished_;
 	// for async thread to check if it's been told to run (requires mutex)
diff --git a/src/ipa/raspberrypi/controller/rpi/contrast.cpp b/src/ipa/raspberrypi/controller/rpi/contrast.cpp
index ae55aad5..6726b4e3 100644
--- a/src/ipa/raspberrypi/controller/rpi/contrast.cpp
+++ b/src/ipa/raspberrypi/controller/rpi/contrast.cpp
@@ -88,7 +88,7 @@  void Contrast::Initialise()
 
 void Contrast::Prepare(Metadata *image_metadata)
 {
-	std::unique_lock<std::mutex> lock(mutex_);
+	MutexLocker lock(mutex_);
 	image_metadata->Set("contrast.status", status_);
 }
 
@@ -172,7 +172,7 @@  void Contrast::Process(StatisticsPtr &stats,
 	ContrastStatus status;
 	fill_in_status(status, brightness_, contrast_, gamma_curve);
 	{
-		std::unique_lock<std::mutex> lock(mutex_);
+		MutexLocker lock(mutex_);
 		status_ = status;
 	}
 }
diff --git a/src/ipa/raspberrypi/controller/rpi/contrast.hpp b/src/ipa/raspberrypi/controller/rpi/contrast.hpp
index 85624539..b74e06ab 100644
--- a/src/ipa/raspberrypi/controller/rpi/contrast.hpp
+++ b/src/ipa/raspberrypi/controller/rpi/contrast.hpp
@@ -6,7 +6,7 @@ 
  */
 #pragma once
 
-#include <mutex>
+#include <libcamera/base/mutex.h>
 
 #include "../contrast_algorithm.hpp"
 #include "../pwl.hpp"
@@ -44,7 +44,7 @@  private:
 	double brightness_;
 	double contrast_;
 	ContrastStatus status_;
-	std::mutex mutex_;
+	libcamera::Mutex mutex_;
 };
 
 } // namespace RPiController
diff --git a/src/ipa/raspberrypi/controller/rpi/lux.cpp b/src/ipa/raspberrypi/controller/rpi/lux.cpp
index f77e9140..7198f64b 100644
--- a/src/ipa/raspberrypi/controller/rpi/lux.cpp
+++ b/src/ipa/raspberrypi/controller/rpi/lux.cpp
@@ -54,7 +54,7 @@  void Lux::SetCurrentAperture(double aperture)
 
 void Lux::Prepare(Metadata *image_metadata)
 {
-	std::unique_lock<std::mutex> lock(mutex_);
+	MutexLocker lock(mutex_);
 	image_metadata->Set("lux.status", status_);
 }
 
@@ -88,7 +88,7 @@  void Lux::Process(StatisticsPtr &stats, Metadata *image_metadata)
 		status.aperture = current_aperture;
 		LOG(RPiLux, Debug) << ": estimated lux " << estimated_lux;
 		{
-			std::unique_lock<std::mutex> lock(mutex_);
+			MutexLocker lock(mutex_);
 			status_ = status;
 		}
 		// Overwrite the metadata here as well, so that downstream
diff --git a/src/ipa/raspberrypi/controller/rpi/lux.hpp b/src/ipa/raspberrypi/controller/rpi/lux.hpp
index 3ebd35d1..caa018da 100644
--- a/src/ipa/raspberrypi/controller/rpi/lux.hpp
+++ b/src/ipa/raspberrypi/controller/rpi/lux.hpp
@@ -6,8 +6,7 @@ 
  */
 #pragma once
 
-#include <mutex>
-
+#include <libcamera/base/mutex.h>
 #include <libcamera/base/utils.h>
 
 #include "../lux_status.h"
@@ -37,7 +36,7 @@  private:
 	double reference_lux_;
 	double current_aperture_;
 	LuxStatus status_;
-	std::mutex mutex_;
+	libcamera::Mutex mutex_;
 };
 
 } // namespace RPiController