{"id":14412,"url":"https://patchwork.libcamera.org/api/patches/14412/?format=json","web_url":"https://patchwork.libcamera.org/patch/14412/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20211029041424.1430886-6-hiroh@chromium.org>","date":"2021-10-29T04:14:23","name":"[libcamera-devel,RFC,5/6] android: camera_device: Add thread safety annotation","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"47b209f214b9c3303278dbd22b51b7b10a1ba4e0","submitter":{"id":63,"url":"https://patchwork.libcamera.org/api/people/63/?format=json","name":"Hirokazu Honda","email":"hiroh@chromium.org"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/14412/mbox/","series":[{"id":2678,"url":"https://patchwork.libcamera.org/api/series/2678/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=2678","date":"2021-10-29T04:14:18","name":"Introduce clang thread safety annotations","version":1,"mbox":"https://patchwork.libcamera.org/series/2678/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/14412/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/14412/checks/","tags":{},"headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 39828BF415\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 29 Oct 2021 04:14:45 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id EA614600C7;\n\tFri, 29 Oct 2021 06:14:44 +0200 (CEST)","from mail-pj1-x1036.google.com (mail-pj1-x1036.google.com\n\t[IPv6:2607:f8b0:4864:20::1036])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 39BFB600BC\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 29 Oct 2021 06:14:41 +0200 (CEST)","by mail-pj1-x1036.google.com with SMTP id\n\tnn3-20020a17090b38c300b001a03bb6c4ebso6482077pjb.1\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 28 Oct 2021 21:14:41 -0700 (PDT)","from hiroh2.tok.corp.google.com\n\t([2401:fa00:8f:203:fd56:8786:5663:b946])\n\tby smtp.gmail.com with ESMTPSA id\n\t11sm4826714pfl.41.2021.10.28.21.14.37\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tThu, 28 Oct 2021 21:14:38 -0700 (PDT)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=chromium.org header.i=@chromium.org\n\theader.b=\"KpNzgyWJ\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org;\n\ts=google; \n\th=from:to:cc:subject:date:message-id:in-reply-to:references\n\t:mime-version:content-transfer-encoding;\n\tbh=KgOwfzDtDao2NIDek9VLlCcxhNW6xP6USuMs9aBm/EY=;\n\tb=KpNzgyWJ4cHNw3VImBzFNWroqOIbitAVfeAMjrsZ/s2FWcRz13de37AcanaRsg3h90\n\tzMRUMIQ+u52o1CzhupJ/HYdc+JLj+7CC95a3KW92C0rEihUCSTMAFIQR7SM9yQJVv89A\n\tBf3NmT262TnITwqC3gaK8t94gF+cb05B+LfZM=","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20210112;\n\th=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to\n\t:references:mime-version:content-transfer-encoding;\n\tbh=KgOwfzDtDao2NIDek9VLlCcxhNW6xP6USuMs9aBm/EY=;\n\tb=Gqp6id+7YcZuMHenXZyf2+pgQUac6T/xe7GGDdn89giwP8RDGs16uLLzYqV/ZbLZx4\n\tlTcFonOFMZ48yERw/IMPq2Gs7IC9sxUyzug8QeOVHe8L90eYTygSTIk8n8lE96XwYk89\n\tFkveqODR/JWpZhqP2WAPBYHWirSQ2bz1SIG2o8bQa5D/cPOLqRgZGgfrjjoW4Ow/crzu\n\tYZ1YvvkqdVAaoN7x/FjZIb+QeAfLqdguPmS0ySRiDR15J7lElazdDAA9c2j98D8FGJ+7\n\tbQzmo7JY6i4wgFqZ85OAxTL1PQplMLvgleql73ChJvKGLoFkx5w/TxJbPrRuQ0RziJdg\n\tVJYQ==","X-Gm-Message-State":"AOAM531iygHqMdlaoE+3hh8z4DjvXZL9tuNEEE/yP7NtMyy5jaDlpGZP\n\ttq8ErDGm0rDYGDUWLLOUmfE16/r5T/Ku0A==","X-Google-Smtp-Source":"ABdhPJx+R+sMzS6VVJg1WgJuXCeHT8sVO4L6bGIS0bNIHXpXJ+dyPWk9is/hopeivYbNr/jzfxRvfQ==","X-Received":"by 2002:a17:90b:2248:: with SMTP id\n\thk8mr8902573pjb.102.1635480879079; \n\tThu, 28 Oct 2021 21:14:39 -0700 (PDT)","From":"Hirokazu Honda <hiroh@chromium.org>","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","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [RFC PATCH 5/6] android: camera_device: Add\n\tthread safety annotation","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"This applies clang thread safety annotation to CameraDevice.\nMutex and MutexLocker there are replaced with Mutex2 and\nMutexLocer2.\n\nSigned-off-by: Hirokazu Honda <hiroh@chromium.org>\n---\n src/android/camera_device.cpp | 26 ++++++++++++++------------\n src/android/camera_device.h   | 18 +++++++++---------\n 2 files changed, 23 insertions(+), 21 deletions(-)","diff":"diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp\nindex f2e0bdbd..e05b5767 100644\n--- a/src/android/camera_device.cpp\n+++ b/src/android/camera_device.cpp\n@@ -14,7 +14,6 @@\n #include <vector>\n \n #include <libcamera/base/log.h>\n-#include <libcamera/base/thread.h>\n #include <libcamera/base/utils.h>\n \n #include <libcamera/control_ids.h>\n@@ -399,7 +398,7 @@ void CameraDevice::close()\n void CameraDevice::flush()\n {\n \t{\n-\t\tMutexLocker stateLock(stateMutex_);\n+\t\tMutexLocker2 stateLock(stateMutex_);\n \t\tif (state_ != State::Running)\n \t\t\treturn;\n \n@@ -409,20 +408,23 @@ void CameraDevice::flush()\n \tworker_.stop();\n \tcamera_->stop();\n \n-\tMutexLocker stateLock(stateMutex_);\n+\tMutexLocker2 stateLock(stateMutex_);\n \tstate_ = State::Stopped;\n }\n \n void CameraDevice::stop()\n {\n-\tMutexLocker stateLock(stateMutex_);\n+\tMutexLocker2 stateLock(stateMutex_);\n \tif (state_ == State::Stopped)\n \t\treturn;\n \n \tworker_.stop();\n \tcamera_->stop();\n \n-\tdescriptors_ = {};\n+\t{\n+\t\tMutexLocker2 descriptorsLock(descriptorsMutex_);\n+\t\tdescriptors_ = {};\n+\t}\n \tstreams_.clear();\n \n \tstate_ = State::Stopped;\n@@ -919,6 +921,7 @@ int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Reques\n \t\t */\n \t\tFrameBuffer *frameBuffer = nullptr;\n \t\tint acquireFence = -1;\n+\t\tMutexLocker2 lock(descriptor->streamsProcessMutex_);\n \t\tswitch (cameraStream->type()) {\n \t\tcase CameraStream::Type::Mapped:\n \t\t\t/*\n@@ -926,7 +929,6 @@ int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Reques\n \t\t\t * Request.\n \t\t\t */\n \t\t\tLOG(HAL, Debug) << ss.str() << \" (mapped)\";\n-\n \t\t\tdescriptor->pendingStreamsToProcess_.insert(\n \t\t\t\t{ cameraStream, &buffer });\n \t\t\tcontinue;\n@@ -986,12 +988,12 @@ int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Reques\n \t * on the queue to be later completed. If the camera has been stopped we\n \t * have to re-start it to be able to process the request.\n \t */\n-\tMutexLocker stateLock(stateMutex_);\n+\tMutexLocker2 stateLock(stateMutex_);\n \n \tif (state_ == State::Flushing) {\n \t\tCamera3RequestDescriptor *rawDescriptor = descriptor.get();\n \t\t{\n-\t\t\tMutexLocker descriptorsLock(descriptorsMutex_);\n+\t\t\tMutexLocker2 descriptorsLock(descriptorsMutex_);\n \t\t\tdescriptors_.push(std::move(descriptor));\n \t\t}\n \t\tabortRequest(rawDescriptor);\n@@ -1016,7 +1018,7 @@ int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Reques\n \tCaptureRequest *request = descriptor->request_.get();\n \n \t{\n-\t\tMutexLocker descriptorsLock(descriptorsMutex_);\n+\t\tMutexLocker2 descriptorsLock(descriptorsMutex_);\n \t\tdescriptors_.push(std::move(descriptor));\n \t}\n \n@@ -1103,7 +1105,7 @@ void CameraDevice::requestComplete(Request *request)\n \t}\n \n \t/* Handle post-processing. */\n-\tMutexLocker locker(descriptor->streamsProcessMutex_);\n+\tMutexLocker2 locker(descriptor->streamsProcessMutex_);\n \n \t/*\n \t * Queue all the post-processing streams request at once. The completion\n@@ -1149,7 +1151,7 @@ void CameraDevice::requestComplete(Request *request)\n \n void CameraDevice::completeDescriptor(Camera3RequestDescriptor *descriptor)\n {\n-\tMutexLocker lock(descriptorsMutex_);\n+\tMutexLocker2 lock(descriptorsMutex_);\n \tdescriptor->complete_ = true;\n \n \tsendCaptureResults();\n@@ -1229,7 +1231,7 @@ void CameraDevice::streamProcessingComplete(Camera3RequestDescriptor::StreamBuff\n \tCamera3RequestDescriptor *request = streamBuffer->request;\n \n \t{\n-\t\tMutexLocker locker(request->streamsProcessMutex_);\n+\t\tMutexLocker2 locker(request->streamsProcessMutex_);\n \n \t\trequest->pendingStreamsToProcess_.erase(streamBuffer->stream);\n \t\tif (!request->pendingStreamsToProcess_.empty())\ndiff --git a/src/android/camera_device.h b/src/android/camera_device.h\nindex 2a414020..9feb287e 100644\n--- a/src/android/camera_device.h\n+++ b/src/android/camera_device.h\n@@ -9,7 +9,6 @@\n \n #include <map>\n #include <memory>\n-#include <mutex>\n #include <queue>\n #include <vector>\n \n@@ -18,7 +17,8 @@\n #include <libcamera/base/class.h>\n #include <libcamera/base/log.h>\n #include <libcamera/base/message.h>\n-#include <libcamera/base/thread.h>\n+#include <libcamera/base/mutex.h>\n+#include <libcamera/base/thread_annotations.h>\n \n #include <libcamera/camera.h>\n #include <libcamera/framebuffer.h>\n@@ -83,7 +83,7 @@ private:\n \t\tRunning,\n \t};\n \n-\tvoid stop();\n+\tvoid stop() EXCLUDES(stateMutex_);\n \n \tstd::unique_ptr<libcamera::FrameBuffer>\n \tcreateFrameBuffer(const buffer_handle_t camera3buffer,\n@@ -95,8 +95,8 @@ private:\n \tvoid notifyError(uint32_t frameNumber, camera3_stream_t *stream,\n \t\t\t camera3_error_msg_code code) const;\n \tint processControls(Camera3RequestDescriptor *descriptor);\n-\tvoid completeDescriptor(Camera3RequestDescriptor *descriptor);\n-\tvoid sendCaptureResults();\n+\tvoid completeDescriptor(Camera3RequestDescriptor *descriptor) EXCLUDES(descriptorsMutex_);\n+\tvoid sendCaptureResults() REQUIRES(descriptorsMutex_);\n \tvoid setBufferStatus(Camera3RequestDescriptor::StreamBuffer &buffer,\n \t\t\t     Camera3RequestDescriptor::Status status);\n \tstd::unique_ptr<CameraMetadata> getResultMetadata(\n@@ -107,8 +107,8 @@ private:\n \n \tCameraWorker worker_;\n \n-\tlibcamera::Mutex stateMutex_; /* Protects access to the camera state. */\n-\tState state_;\n+\tlibcamera::Mutex2 stateMutex_; /* Protects access to the camera state. */\n+\tState state_ GUARDED_BY(stateMutex_);\n \n \tstd::shared_ptr<libcamera::Camera> camera_;\n \tstd::unique_ptr<libcamera::CameraConfiguration> config_;\n@@ -119,8 +119,8 @@ private:\n \n \tstd::vector<CameraStream> streams_;\n \n-\tlibcamera::Mutex descriptorsMutex_; /* Protects descriptors_. */\n-\tstd::queue<std::unique_ptr<Camera3RequestDescriptor>> descriptors_;\n+\tlibcamera::Mutex2 descriptorsMutex_ ACQUIRED_AFTER(stateMutex_);\n+\tstd::queue<std::unique_ptr<Camera3RequestDescriptor>> descriptors_ GUARDED_BY(descriptorsMutex_);\n \n \tstd::string maker_;\n \tstd::string model_;\n","prefixes":["libcamera-devel","RFC","5/6"]}