From patchwork Fri Oct 29 04:14:23 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 14412 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 39828BF415 for ; Fri, 29 Oct 2021 04:14:45 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id EA614600C7; Fri, 29 Oct 2021 06:14:44 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="KpNzgyWJ"; dkim-atps=neutral Received: from mail-pj1-x1036.google.com (mail-pj1-x1036.google.com [IPv6:2607:f8b0:4864:20::1036]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 39BFB600BC for ; Fri, 29 Oct 2021 06:14:41 +0200 (CEST) Received: by mail-pj1-x1036.google.com with SMTP id nn3-20020a17090b38c300b001a03bb6c4ebso6482077pjb.1 for ; Thu, 28 Oct 2021 21:14:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=KgOwfzDtDao2NIDek9VLlCcxhNW6xP6USuMs9aBm/EY=; b=KpNzgyWJ4cHNw3VImBzFNWroqOIbitAVfeAMjrsZ/s2FWcRz13de37AcanaRsg3h90 zMRUMIQ+u52o1CzhupJ/HYdc+JLj+7CC95a3KW92C0rEihUCSTMAFIQR7SM9yQJVv89A Bf3NmT262TnITwqC3gaK8t94gF+cb05B+LfZM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=KgOwfzDtDao2NIDek9VLlCcxhNW6xP6USuMs9aBm/EY=; b=Gqp6id+7YcZuMHenXZyf2+pgQUac6T/xe7GGDdn89giwP8RDGs16uLLzYqV/ZbLZx4 lTcFonOFMZ48yERw/IMPq2Gs7IC9sxUyzug8QeOVHe8L90eYTygSTIk8n8lE96XwYk89 FkveqODR/JWpZhqP2WAPBYHWirSQ2bz1SIG2o8bQa5D/cPOLqRgZGgfrjjoW4Ow/crzu YZ1YvvkqdVAaoN7x/FjZIb+QeAfLqdguPmS0ySRiDR15J7lElazdDAA9c2j98D8FGJ+7 bQzmo7JY6i4wgFqZ85OAxTL1PQplMLvgleql73ChJvKGLoFkx5w/TxJbPrRuQ0RziJdg VJYQ== X-Gm-Message-State: AOAM531iygHqMdlaoE+3hh8z4DjvXZL9tuNEEE/yP7NtMyy5jaDlpGZP tq8ErDGm0rDYGDUWLLOUmfE16/r5T/Ku0A== X-Google-Smtp-Source: ABdhPJx+R+sMzS6VVJg1WgJuXCeHT8sVO4L6bGIS0bNIHXpXJ+dyPWk9is/hopeivYbNr/jzfxRvfQ== X-Received: by 2002:a17:90b:2248:: with SMTP id hk8mr8902573pjb.102.1635480879079; Thu, 28 Oct 2021 21:14:39 -0700 (PDT) Received: from hiroh2.tok.corp.google.com ([2401:fa00:8f:203:fd56:8786:5663:b946]) by smtp.gmail.com with ESMTPSA id 11sm4826714pfl.41.2021.10.28.21.14.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Oct 2021 21:14:38 -0700 (PDT) From: Hirokazu Honda To: libcamera-devel@lists.libcamera.org Date: Fri, 29 Oct 2021 13:14:23 +0900 Message-Id: <20211029041424.1430886-6-hiroh@chromium.org> X-Mailer: git-send-email 2.33.1.1089.g2158813163f-goog In-Reply-To: <20211029041424.1430886-1-hiroh@chromium.org> References: <20211029041424.1430886-1-hiroh@chromium.org> MIME-Version: 1.0 Subject: [libcamera-devel] [RFC PATCH 5/6] android: camera_device: Add thread safety annotation X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" This applies clang thread safety annotation to CameraDevice. Mutex and MutexLocker there are replaced with Mutex2 and MutexLocer2. Signed-off-by: Hirokazu Honda --- src/android/camera_device.cpp | 26 ++++++++++++++------------ src/android/camera_device.h | 18 +++++++++--------- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp index f2e0bdbd..e05b5767 100644 --- a/src/android/camera_device.cpp +++ b/src/android/camera_device.cpp @@ -14,7 +14,6 @@ #include #include -#include #include #include @@ -399,7 +398,7 @@ void CameraDevice::close() void CameraDevice::flush() { { - MutexLocker stateLock(stateMutex_); + MutexLocker2 stateLock(stateMutex_); if (state_ != State::Running) return; @@ -409,20 +408,23 @@ void CameraDevice::flush() worker_.stop(); camera_->stop(); - MutexLocker stateLock(stateMutex_); + MutexLocker2 stateLock(stateMutex_); state_ = State::Stopped; } void CameraDevice::stop() { - MutexLocker stateLock(stateMutex_); + MutexLocker2 stateLock(stateMutex_); if (state_ == State::Stopped) return; worker_.stop(); camera_->stop(); - descriptors_ = {}; + { + MutexLocker2 descriptorsLock(descriptorsMutex_); + descriptors_ = {}; + } streams_.clear(); state_ = State::Stopped; @@ -919,6 +921,7 @@ int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Reques */ FrameBuffer *frameBuffer = nullptr; int acquireFence = -1; + MutexLocker2 lock(descriptor->streamsProcessMutex_); switch (cameraStream->type()) { case CameraStream::Type::Mapped: /* @@ -926,7 +929,6 @@ int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Reques * Request. */ LOG(HAL, Debug) << ss.str() << " (mapped)"; - descriptor->pendingStreamsToProcess_.insert( { cameraStream, &buffer }); continue; @@ -986,12 +988,12 @@ int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Reques * on the queue to be later completed. If the camera has been stopped we * have to re-start it to be able to process the request. */ - MutexLocker stateLock(stateMutex_); + MutexLocker2 stateLock(stateMutex_); if (state_ == State::Flushing) { Camera3RequestDescriptor *rawDescriptor = descriptor.get(); { - MutexLocker descriptorsLock(descriptorsMutex_); + MutexLocker2 descriptorsLock(descriptorsMutex_); descriptors_.push(std::move(descriptor)); } abortRequest(rawDescriptor); @@ -1016,7 +1018,7 @@ int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Reques CaptureRequest *request = descriptor->request_.get(); { - MutexLocker descriptorsLock(descriptorsMutex_); + MutexLocker2 descriptorsLock(descriptorsMutex_); descriptors_.push(std::move(descriptor)); } @@ -1103,7 +1105,7 @@ void CameraDevice::requestComplete(Request *request) } /* Handle post-processing. */ - MutexLocker locker(descriptor->streamsProcessMutex_); + MutexLocker2 locker(descriptor->streamsProcessMutex_); /* * Queue all the post-processing streams request at once. The completion @@ -1149,7 +1151,7 @@ void CameraDevice::requestComplete(Request *request) void CameraDevice::completeDescriptor(Camera3RequestDescriptor *descriptor) { - MutexLocker lock(descriptorsMutex_); + MutexLocker2 lock(descriptorsMutex_); descriptor->complete_ = true; sendCaptureResults(); @@ -1229,7 +1231,7 @@ void CameraDevice::streamProcessingComplete(Camera3RequestDescriptor::StreamBuff Camera3RequestDescriptor *request = streamBuffer->request; { - MutexLocker locker(request->streamsProcessMutex_); + MutexLocker2 locker(request->streamsProcessMutex_); request->pendingStreamsToProcess_.erase(streamBuffer->stream); if (!request->pendingStreamsToProcess_.empty()) diff --git a/src/android/camera_device.h b/src/android/camera_device.h index 2a414020..9feb287e 100644 --- a/src/android/camera_device.h +++ b/src/android/camera_device.h @@ -9,7 +9,6 @@ #include #include -#include #include #include @@ -18,7 +17,8 @@ #include #include #include -#include +#include +#include #include #include @@ -83,7 +83,7 @@ private: Running, }; - void stop(); + void stop() EXCLUDES(stateMutex_); std::unique_ptr createFrameBuffer(const buffer_handle_t camera3buffer, @@ -95,8 +95,8 @@ 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) EXCLUDES(descriptorsMutex_); + void sendCaptureResults() REQUIRES(descriptorsMutex_); void setBufferStatus(Camera3RequestDescriptor::StreamBuffer &buffer, Camera3RequestDescriptor::Status status); std::unique_ptr getResultMetadata( @@ -107,8 +107,8 @@ private: CameraWorker worker_; - libcamera::Mutex stateMutex_; /* Protects access to the camera state. */ - State state_; + libcamera::Mutex2 stateMutex_; /* Protects access to the camera state. */ + State state_ GUARDED_BY(stateMutex_); std::shared_ptr camera_; std::unique_ptr config_; @@ -119,8 +119,8 @@ private: std::vector streams_; - libcamera::Mutex descriptorsMutex_; /* Protects descriptors_. */ - std::queue> descriptors_; + libcamera::Mutex2 descriptorsMutex_ ACQUIRED_AFTER(stateMutex_); + std::queue> descriptors_ GUARDED_BY(descriptorsMutex_); std::string maker_; std::string model_;