{"id":14411,"url":"https://patchwork.libcamera.org/api/1.1/patches/14411/?format=json","web_url":"https://patchwork.libcamera.org/patch/14411/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/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-5-hiroh@chromium.org>","date":"2021-10-29T04:14:22","name":"[libcamera-devel,RFC,4/6] android: camera_stream: Add thread safety annotation","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"e438700d86cb5b4f11dff21f9bd6968215bf1d28","submitter":{"id":63,"url":"https://patchwork.libcamera.org/api/1.1/people/63/?format=json","name":"Hirokazu Honda","email":"hiroh@chromium.org"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/14411/mbox/","series":[{"id":2678,"url":"https://patchwork.libcamera.org/api/1.1/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/14411/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/14411/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 B17EBBF415\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 29 Oct 2021 04:14:42 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 6E3FD600BA;\n\tFri, 29 Oct 2021 06:14:42 +0200 (CEST)","from mail-pg1-x52e.google.com (mail-pg1-x52e.google.com\n\t[IPv6:2607:f8b0:4864:20::52e])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id DFB6D600BC\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 29 Oct 2021 06:14:38 +0200 (CEST)","by mail-pg1-x52e.google.com with SMTP id r28so8720762pga.0\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 28 Oct 2021 21:14:38 -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.35\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tThu, 28 Oct 2021 21:14:36 -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=\"Y3i4XMcj\"; 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=McoQw3rc9fJF6a1dSKvOmudXfrS2GJYCbsikZQKHDsU=;\n\tb=Y3i4XMcj4Bgh2SOEakHNHVarSc7Q85OhoPKU99Pd285JYh3NhaFAHaNf1/QArmDayv\n\tCWkAvh07SJ9OLdc5W5m19G8IRvwGWfNY107tnMt6LBL02X4/J74J7cHtq0sn+Tee0Ulr\n\trJVYxGjgjDjdD/kYRU49KOxBrd5EhF2QZCOYM=","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=McoQw3rc9fJF6a1dSKvOmudXfrS2GJYCbsikZQKHDsU=;\n\tb=P0+MZn77HzCY1FHjs59MtQrj1FPrg2a91UT0QO1+ehDT8qan+HhaPxffIPmbOfURWX\n\twoFKahSY7yz5WtSwK1sWnwZwX/I7vsXLtdPCtcb4dCjhHe2QrGdoZaVovGNlEbRHOdtI\n\tO+TZunsHtd7BpiUnzDuV3asIP6I7e3OBssU79wOCPrC0nf+eoVkW+AjkXuCSJFWtJfJF\n\t6IlhwHDHJ76DhgDrN5D9YIrRJpESIpPXO60N9Kmx8TgMyieSXtxwjEfX+DloH9GPypry\n\twNIyNetXaHDai4wTmNZTDkTEpC8k542oKQXJSHFxpi2zVYqWQvRAdF3Uuh41UL2mDf0R\n\tnlIQ==","X-Gm-Message-State":"AOAM532peG5VMjuM+7bVy/uRRK5FuDYAbff5b16l+Hgq1kbWzXckSeYW\n\tx4mCp5vi5tBwDcnpNhlktKXyIlnpGe23sw==","X-Google-Smtp-Source":"ABdhPJwlZwiYdyZLTcTwTCnjBJ9JZfj0nmDzOU76f/YWv0H2mHvY4NxEC5zv6968g3bOuQgIbBPT3w==","X-Received":"by 2002:a05:6a00:14c1:b0:47f:5f9:8675 with SMTP id\n\tw1-20020a056a0014c100b0047f05f98675mr451763pfu.22.1635480877193; \n\tThu, 28 Oct 2021 21:14:37 -0700 (PDT)","From":"Hirokazu Honda <hiroh@chromium.org>","To":"libcamera-devel@lists.libcamera.org","Date":"Fri, 29 Oct 2021 13:14:22 +0900","Message-Id":"<20211029041424.1430886-5-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 4/6] android: camera_stream: 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 CameraStream.\nMutex and MutexLocker there are replaced with Mutex2 and\nMutexLocer2.\n\nSigned-off-by: Hirokazu Honda <hiroh@chromium.org>\n---\n src/android/camera_stream.cpp | 22 ++++++++++++----------\n src/android/camera_stream.h   | 13 +++++++------\n 2 files changed, 19 insertions(+), 16 deletions(-)","diff":"diff --git a/src/android/camera_stream.cpp b/src/android/camera_stream.cpp\nindex 9023c13c..c5272445 100644\n--- a/src/android/camera_stream.cpp\n+++ b/src/android/camera_stream.cpp\n@@ -119,12 +119,13 @@ int CameraStream::configure()\n \n \tif (type_ == Type::Internal) {\n \t\tallocator_ = std::make_unique<FrameBufferAllocator>(cameraDevice_->camera());\n-\t\tmutex_ = std::make_unique<std::mutex>();\n+\t\tmutex_ = std::make_unique<Mutex2>();\n \n \t\tint ret = allocator_->allocate(stream());\n \t\tif (ret < 0)\n \t\t\treturn ret;\n \n+\t\tMutexLocker2 lock(*mutex_);\n \t\t/* Save a pointer to the reserved frame buffers */\n \t\tfor (const auto &frameBuffer : allocator_->buffers(stream()))\n \t\t\tbuffers_.push_back(frameBuffer.get());\n@@ -208,7 +209,7 @@ FrameBuffer *CameraStream::getBuffer()\n \tif (!allocator_)\n \t\treturn nullptr;\n \n-\tstd::lock_guard<std::mutex> locker(*mutex_);\n+\tMutexLocker2 lock(*mutex_);\n \n \tif (buffers_.empty()) {\n \t\tLOG(HAL, Error) << \"Buffer underrun\";\n@@ -226,20 +227,21 @@ void CameraStream::putBuffer(FrameBuffer *buffer)\n \tif (!allocator_)\n \t\treturn;\n \n-\tstd::lock_guard<std::mutex> locker(*mutex_);\n+\tMutexLocker2 lock(*mutex_);\n \n \tbuffers_.push_back(buffer);\n }\n \n CameraStream::PostProcessorWorker::PostProcessorWorker(PostProcessor *postProcessor)\n-\t: postProcessor_(postProcessor)\n+\t: postProcessor_(postProcessor),\n+\t  state_(State::Stopped)\n {\n }\n \n CameraStream::PostProcessorWorker::~PostProcessorWorker()\n {\n \t{\n-\t\tlibcamera::MutexLocker lock(mutex_);\n+\t\tlibcamera::MutexLocker2 lock(mutex_);\n \t\tstate_ = State::Stopped;\n \t}\n \n@@ -250,7 +252,7 @@ CameraStream::PostProcessorWorker::~PostProcessorWorker()\n void CameraStream::PostProcessorWorker::start()\n {\n \t{\n-\t\tlibcamera::MutexLocker lock(mutex_);\n+\t\tlibcamera::MutexLocker2 lock(mutex_);\n \t\tASSERT(state_ != State::Running);\n \t\tstate_ = State::Running;\n \t}\n@@ -261,7 +263,7 @@ void CameraStream::PostProcessorWorker::start()\n void CameraStream::PostProcessorWorker::queueRequest(Camera3RequestDescriptor::StreamBuffer *dest)\n {\n \t{\n-\t\tMutexLocker lock(mutex_);\n+\t\tMutexLocker2 lock(mutex_);\n \t\tASSERT(state_ == State::Running);\n \t\trequests_.push(dest);\n \t}\n@@ -271,10 +273,10 @@ void CameraStream::PostProcessorWorker::queueRequest(Camera3RequestDescriptor::S\n \n void CameraStream::PostProcessorWorker::run()\n {\n-\tMutexLocker locker(mutex_);\n+\tMutexLocker2 locker(mutex_);\n \n \twhile (1) {\n-\t\tcv_.wait(locker, [&] {\n+\t\tcv_.wait(locker.get(), [&]() REQUIRES(mutex_) {\n \t\t\treturn state_ != State::Running || !requests_.empty();\n \t\t});\n \n@@ -308,7 +310,7 @@ void CameraStream::PostProcessorWorker::run()\n \n void CameraStream::PostProcessorWorker::flush()\n {\n-\tlibcamera::MutexLocker lock(mutex_);\n+\tMutexLocker2 lock(mutex_);\n \tstate_ = State::Flushing;\n \tlock.unlock();\n \ndiff --git a/src/android/camera_stream.h b/src/android/camera_stream.h\nindex 0c402deb..665bdf5c 100644\n--- a/src/android/camera_stream.h\n+++ b/src/android/camera_stream.h\n@@ -9,13 +9,14 @@\n \n #include <condition_variable>\n #include <memory>\n-#include <mutex>\n #include <queue>\n #include <vector>\n \n #include <hardware/camera3.h>\n \n+#include <libcamera/base/mutex.h>\n #include <libcamera/base/thread.h>\n+#include <libcamera/base/thread_annotations.h>\n \n #include <libcamera/camera.h>\n #include <libcamera/framebuffer.h>\n@@ -153,11 +154,11 @@ private:\n \tprivate:\n \t\tPostProcessor *postProcessor_;\n \n-\t\tlibcamera::Mutex mutex_;\n+\t\tlibcamera::Mutex2 mutex_;\n \t\tstd::condition_variable cv_;\n \n-\t\tstd::queue<Camera3RequestDescriptor::StreamBuffer *> requests_;\n-\t\tState state_ = State::Stopped;\n+\t\tstd::queue<Camera3RequestDescriptor::StreamBuffer *> requests_ GUARDED_BY(mutex_);\n+\t\tState state_ GUARDED_BY(mutex_);\n \t};\n \n \tint waitFence(int fence);\n@@ -169,12 +170,12 @@ private:\n \tconst unsigned int index_;\n \n \tstd::unique_ptr<libcamera::FrameBufferAllocator> allocator_;\n-\tstd::vector<libcamera::FrameBuffer *> buffers_;\n+\tstd::vector<libcamera::FrameBuffer *> buffers_ GUARDED_BY(mutex_);\n \t/*\n \t * The class has to be MoveConstructible as instances are stored in\n \t * an std::vector in CameraDevice.\n \t */\n-\tstd::unique_ptr<std::mutex> mutex_;\n+\tstd::unique_ptr<libcamera::Mutex2> mutex_;\n \tstd::unique_ptr<PostProcessor> postProcessor_;\n \n \tstd::unique_ptr<PostProcessorWorker> worker_;\n","prefixes":["libcamera-devel","RFC","4/6"]}