[libcamera-devel,v2,10/11] android: camera_device: Add thread safety annotation
diff mbox series

Message ID 20211129114453.3186042-11-hiroh@chromium.org
State Superseded
Headers show
Series
  • Introduce clang thread safety annotations
Related show

Commit Message

Hirokazu Honda Nov. 29, 2021, 11:44 a.m. UTC
This applies clang thread safety annotation to CameraDevice.
Mutex and MutexLocker there are replaced with Mutex2 and
MutexLocer2.

Signed-off-by: Hirokazu Honda <hiroh@chromium.org>
---
 src/android/camera_device.cpp |  2 --
 src/android/camera_device.h   | 18 ++++++++++--------
 2 files changed, 10 insertions(+), 10 deletions(-)

Comments

Laurent Pinchart Nov. 30, 2021, 4:32 a.m. UTC | #1
Hi Hiro,

Thank you for the patch.

On Mon, Nov 29, 2021 at 08:44:52PM +0900, Hirokazu Honda wrote:
> This applies clang thread safety annotation to CameraDevice.
> Mutex and MutexLocker there are replaced with Mutex2 and
> MutexLocer2.
> 
> Signed-off-by: Hirokazu Honda <hiroh@chromium.org>
> ---
>  src/android/camera_device.cpp |  2 --
>  src/android/camera_device.h   | 18 ++++++++++--------
>  2 files changed, 10 insertions(+), 10 deletions(-)
> 
> diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp
> index 59185e7f..e7365175 100644
> --- a/src/android/camera_device.cpp
> +++ b/src/android/camera_device.cpp
> @@ -14,7 +14,6 @@
>  #include <vector>
>  
>  #include <libcamera/base/log.h>
> -#include <libcamera/base/thread.h>

This belongs to a different patch.

>  #include <libcamera/base/utils.h>
>  
>  #include <libcamera/control_ids.h>
> @@ -932,7 +931,6 @@ int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Reques
>  			 * Request.
>  			 */
>  			LOG(HAL, Debug) << ss.str() << " (mapped)";
> -

Not needed.

>  			descriptor->pendingStreamsToProcess_.insert(
>  				{ cameraStream, &buffer });
>  			continue;
> diff --git a/src/android/camera_device.h b/src/android/camera_device.h
> index 51fe7da2..2083dac1 100644
> --- a/src/android/camera_device.h
> +++ b/src/android/camera_device.h
> @@ -9,7 +9,6 @@
>  
>  #include <map>
>  #include <memory>
> -#include <mutex>
>  #include <queue>
>  #include <vector>
>  
> @@ -18,7 +17,8 @@
>  #include <libcamera/base/class.h>
>  #include <libcamera/base/log.h>
>  #include <libcamera/base/message.h>
> -#include <libcamera/base/thread.h>
> +#include <libcamera/base/mutex.h>
> +#include <libcamera/base/thread_annotations.h>

These belong to 02/11 too.

Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

>  
>  #include <libcamera/camera.h>
>  #include <libcamera/framebuffer.h>
> @@ -83,7 +83,7 @@ private:
>  		Running,
>  	};
>  
> -	void stop();
> +	void stop() LIBCAMERA_TSA_EXCLUDES(stateMutex_);
>  
>  	std::unique_ptr<libcamera::FrameBuffer>
>  	createFrameBuffer(const buffer_handle_t camera3buffer,
> @@ -95,8 +95,9 @@ private:
>  	void notifyError(uint32_t frameNumber, camera3_stream_t *stream,
>  			 camera3_error_msg_code code) const;
>  	int processControls(Camera3RequestDescriptor *descriptor);
> -	void completeDescriptor(Camera3RequestDescriptor *descriptor);
> -	void sendCaptureResults();
> +	void completeDescriptor(Camera3RequestDescriptor *descriptor)
> +		LIBCAMERA_TSA_EXCLUDES(descriptorsMutex_);
> +	void sendCaptureResults() LIBCAMERA_TSA_REQUIRES(descriptorsMutex_);
>  	void setBufferStatus(Camera3RequestDescriptor::StreamBuffer &buffer,
>  			     Camera3RequestDescriptor::Status status);
>  	std::unique_ptr<CameraMetadata> getResultMetadata(
> @@ -108,7 +109,7 @@ private:
>  	CameraWorker worker_;
>  
>  	libcamera::Mutex stateMutex_; /* Protects access to the camera state. */
> -	State state_;
> +	State state_ LIBCAMERA_TSA_GUARDED_BY(stateMutex_);
>  
>  	std::shared_ptr<libcamera::Camera> camera_;
>  	std::unique_ptr<libcamera::CameraConfiguration> config_;
> @@ -119,8 +120,9 @@ private:
>  
>  	std::vector<CameraStream> streams_;
>  
> -	libcamera::Mutex descriptorsMutex_; /* Protects descriptors_. */
> -	std::queue<std::unique_ptr<Camera3RequestDescriptor>> descriptors_;
> +	libcamera::Mutex descriptorsMutex_ LIBCAMERA_TSA_ACQUIRED_AFTER(stateMutex_);
> +	std::queue<std::unique_ptr<Camera3RequestDescriptor>> descriptors_
> +		LIBCAMERA_TSA_GUARDED_BY(descriptorsMutex_);
>  
>  	std::string maker_;
>  	std::string model_;

Patch
diff mbox series

diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp
index 59185e7f..e7365175 100644
--- a/src/android/camera_device.cpp
+++ b/src/android/camera_device.cpp
@@ -14,7 +14,6 @@ 
 #include <vector>
 
 #include <libcamera/base/log.h>
-#include <libcamera/base/thread.h>
 #include <libcamera/base/utils.h>
 
 #include <libcamera/control_ids.h>
@@ -932,7 +931,6 @@  int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Reques
 			 * Request.
 			 */
 			LOG(HAL, Debug) << ss.str() << " (mapped)";
-
 			descriptor->pendingStreamsToProcess_.insert(
 				{ cameraStream, &buffer });
 			continue;
diff --git a/src/android/camera_device.h b/src/android/camera_device.h
index 51fe7da2..2083dac1 100644
--- a/src/android/camera_device.h
+++ b/src/android/camera_device.h
@@ -9,7 +9,6 @@ 
 
 #include <map>
 #include <memory>
-#include <mutex>
 #include <queue>
 #include <vector>
 
@@ -18,7 +17,8 @@ 
 #include <libcamera/base/class.h>
 #include <libcamera/base/log.h>
 #include <libcamera/base/message.h>
-#include <libcamera/base/thread.h>
+#include <libcamera/base/mutex.h>
+#include <libcamera/base/thread_annotations.h>
 
 #include <libcamera/camera.h>
 #include <libcamera/framebuffer.h>
@@ -83,7 +83,7 @@  private:
 		Running,
 	};
 
-	void stop();
+	void stop() LIBCAMERA_TSA_EXCLUDES(stateMutex_);
 
 	std::unique_ptr<libcamera::FrameBuffer>
 	createFrameBuffer(const buffer_handle_t camera3buffer,
@@ -95,8 +95,9 @@  private:
 	void notifyError(uint32_t frameNumber, camera3_stream_t *stream,
 			 camera3_error_msg_code code) const;
 	int processControls(Camera3RequestDescriptor *descriptor);
-	void completeDescriptor(Camera3RequestDescriptor *descriptor);
-	void sendCaptureResults();
+	void completeDescriptor(Camera3RequestDescriptor *descriptor)
+		LIBCAMERA_TSA_EXCLUDES(descriptorsMutex_);
+	void sendCaptureResults() LIBCAMERA_TSA_REQUIRES(descriptorsMutex_);
 	void setBufferStatus(Camera3RequestDescriptor::StreamBuffer &buffer,
 			     Camera3RequestDescriptor::Status status);
 	std::unique_ptr<CameraMetadata> getResultMetadata(
@@ -108,7 +109,7 @@  private:
 	CameraWorker worker_;
 
 	libcamera::Mutex stateMutex_; /* Protects access to the camera state. */
-	State state_;
+	State state_ LIBCAMERA_TSA_GUARDED_BY(stateMutex_);
 
 	std::shared_ptr<libcamera::Camera> camera_;
 	std::unique_ptr<libcamera::CameraConfiguration> config_;
@@ -119,8 +120,9 @@  private:
 
 	std::vector<CameraStream> streams_;
 
-	libcamera::Mutex descriptorsMutex_; /* Protects descriptors_. */
-	std::queue<std::unique_ptr<Camera3RequestDescriptor>> descriptors_;
+	libcamera::Mutex descriptorsMutex_ LIBCAMERA_TSA_ACQUIRED_AFTER(stateMutex_);
+	std::queue<std::unique_ptr<Camera3RequestDescriptor>> descriptors_
+		LIBCAMERA_TSA_GUARDED_BY(descriptorsMutex_);
 
 	std::string maker_;
 	std::string model_;