From patchwork Fri Oct 29 04:14:19 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 14500 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by patchwork.libcamera.org (Postfix) with ESMTPS id E3F23BDB1C for ; Tue, 9 Nov 2021 17:44:15 +0000 (UTC) Received: from pendragon.ideasonboard.com (cpc89244-aztw30-2-0-cust3082.18-1.cable.virginm.net [86.31.172.11]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 7E679DEE for ; Tue, 9 Nov 2021 18:44:15 +0100 (CET) Authentication-Results: perceval.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="YRbA2fUS"; dkim-atps=neutral Delivered-To: kbingham@ideasonboard.com Received: from perceval.ideasonboard.com by perceval.ideasonboard.com with LMTP id OJozGCl1e2FNVQAA4E0KoQ (envelope-from ) for ; Fri, 29 Oct 2021 06:14:33 +0200 Received: from mail-pj1-x102d.google.com (mail-pj1-x102d.google.com [IPv6:2607:f8b0:4864:20::102d]) by perceval.ideasonboard.com (Postfix) with ESMTPS id 52C73881 for ; Fri, 29 Oct 2021 06:14:33 +0200 (CEST) Received: by mail-pj1-x102d.google.com with SMTP id gn3so6343344pjb.0 for ; Thu, 28 Oct 2021 21:14:33 -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=14xAfBBWOIqRArbX5pK9vlWNRdDCBPlDSbbRYEJdJ3c=; b=YRbA2fUSe2v7cTslzNNL+nHW2LpVOhSpup2U1DF/whT+bsd3cRAlRdxE5w0T+kdZjM y1aiC3fznPhiFqKiTQY/gOdl+7Lqp4k2YtoO8gMilHtsdjJJ8KrP+B/blg5bDHU+10hT 9lz6zqzGQo21EbcykCWYfKCzYJUitGnen7HCU= 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=14xAfBBWOIqRArbX5pK9vlWNRdDCBPlDSbbRYEJdJ3c=; b=648UxnRhD00LGRsVYv3e4QM/Ik+4PyM6gB/TbWYPJKUbt3H79tLVNyetLnsptCCzVd iB6N85rzNQRb+5ME9xUEcpoLBCbNrosm1R2QiXCrQyRa5SILOCk9TcoK7BGXCVb33G/D 3+/sSAxanCGLaqiYx/Ms429VZtjm0N4sN9UOHsvYag00Acgj/cdvWBH9iRK1zWLKcdBk WhMwPdl+9qmNQgYUq7q7IAxEqITKTsYqfeI3ryj0w1W2sHbqtYMSo7gtC7GqcP8nFEHX xCh1n6SvvyPgDYFld6UpJaGjlFedKVk3Lordm+J8yCzJ7sXLFw3oZ4c3B8YmTHK4Yva2 1aBg== X-Gm-Message-State: AOAM5317lPtgup4voSpwV/ITYXErBN/nQhmudemUEwLlOGfHqA0xJfY7 XYrVHyT48Id653EKGyNR0g+NVw== X-Google-Smtp-Source: =?utf-8?q?ABdhPJzvIXQbO+iZRfvHjhFL8RM+0hk8SbKEBI4cI?= =?utf-8?q?0JR5gmSMTs8U+NHJU0miAMeqcLDltD12a8tPg=3D=3D?= X-Received: by 2002:a17:90b:1c02:: with SMTP id oc2mr9122817pjb.52.1635480871514; Thu, 28 Oct 2021 21:14:31 -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.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Oct 2021 21:14:31 -0700 (PDT) From: Hirokazu Honda To: libcamera-devel@lists.libcamera.org Cc: tfiga@chromium.org, laurent.pinchart@ideasonboard.com, kieran.bingham@ideasonboard.com, Hirokazu Honda Subject: [RFC PATCH 1/6] libcamera: base: Add thread safety annotation macros Date: Fri, 29 Oct 2021 13:14:19 +0900 Message-Id: <20211029041424.1430886-2-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 X-TUID: dpi2avQ7LjmL Resent-From: Kieran Bingham Resent-To: parsemail@patchwork.libcamera.org Clang complier is able to do a thread safety analysis with annotations [1]. This introduces the thread safety annotation macros and also enable the analysis by adding -Wthread-safety if a clang compiler is used. [1] https://clang.llvm.org/docs/ThreadSafetyAnalysis.html. Signed-off-by: Hirokazu Honda --- include/libcamera/base/meson.build | 1 + include/libcamera/base/thread_annotations.h | 151 ++++++++++++++++++++ meson.build | 1 + 3 files changed, 153 insertions(+) create mode 100644 include/libcamera/base/thread_annotations.h diff --git a/include/libcamera/base/meson.build b/include/libcamera/base/meson.build index 525aba9d..1a71ce5a 100644 --- a/include/libcamera/base/meson.build +++ b/include/libcamera/base/meson.build @@ -19,6 +19,7 @@ libcamera_base_headers = files([ 'signal.h', 'span.h', 'thread.h', + 'thread_annotations.h', 'timer.h', 'utils.h', ]) diff --git a/include/libcamera/base/thread_annotations.h b/include/libcamera/base/thread_annotations.h new file mode 100644 index 00000000..935d8799 --- /dev/null +++ b/include/libcamera/base/thread_annotations.h @@ -0,0 +1,151 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2021, Google Inc. + * + * thread_annotation.h - Macro of Clang thread safety analysis + */ +#ifndef __LIBCAMERA_BASE_THREAD_ANNOTATIONS_H__ +#define __LIBCAMERA_BASE_THREAD_ANNOTATIONS_H__ + +/* + * Enable thread safety attributes only with clang. + * The attributes can be safely erased when compiling with other compilers. + */ +#if defined(__clang__) && (!defined(SWIG)) +#define THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x)) +#else +#define THREAD_ANNOTATION_ATTRIBUTE__(x) /* no-op */ +#endif + +/* See https://clang.llvm.org/docs/ThreadSafetyAnalysis.html for these usages. */ + +#define CAPABILITY(x) \ + THREAD_ANNOTATION_ATTRIBUTE__(capability(x)) + +#define SCOPED_CAPABILITY \ + THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable) + +#define GUARDED_BY(x) \ + THREAD_ANNOTATION_ATTRIBUTE__(guarded_by(x)) + +#define PT_GUARDED_BY(x) \ + THREAD_ANNOTATION_ATTRIBUTE__(pt_guarded_by(x)) + +#define ACQUIRED_BEFORE(...) \ + THREAD_ANNOTATION_ATTRIBUTE__(acquired_before(__VA_ARGS__)) + +#define ACQUIRED_AFTER(...) \ + THREAD_ANNOTATION_ATTRIBUTE__(acquired_after(__VA_ARGS__)) + +#define REQUIRES(...) \ + THREAD_ANNOTATION_ATTRIBUTE__(requires_capability(__VA_ARGS__)) + +#define REQUIRES_SHARED(...) \ + THREAD_ANNOTATION_ATTRIBUTE__(requires_shared_capability(__VA_ARGS__)) + +#define ACQUIRE(...) \ + THREAD_ANNOTATION_ATTRIBUTE__(acquire_capability(__VA_ARGS__)) + +#define ACQUIRE_SHARED(...) \ + THREAD_ANNOTATION_ATTRIBUTE__(acquire_shared_capability(__VA_ARGS__)) + +#define RELEASE(...) \ + THREAD_ANNOTATION_ATTRIBUTE__(release_capability(__VA_ARGS__)) + +#define RELEASE_SHARED(...) \ + THREAD_ANNOTATION_ATTRIBUTE__(release_shared_capability(__VA_ARGS__)) + +#define RELEASE_GENERIC(...) \ + THREAD_ANNOTATION_ATTRIBUTE__(release_generic_capability(__VA_ARGS__)) + +#define TRY_ACQUIRE(...) \ + THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_capability(__VA_ARGS__)) + +#define TRY_ACQUIRE_SHARED(...) \ + THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_shared_capability(__VA_ARGS__)) + +#define EXCLUDES(...) \ + THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(__VA_ARGS__)) + +#define ASSERT_CAPABILITY(x) \ + THREAD_ANNOTATION_ATTRIBUTE__(assert_capability(x)) + +#define ASSERT_SHARED_CAPABILITY(x) \ + THREAD_ANNOTATION_ATTRIBUTE__(assert_shared_capability(x)) + +#define RETURN_CAPABILITY(x) \ + THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(x)) + +#define NO_THREAD_SAFETY_ANALYSIS \ + THREAD_ANNOTATION_ATTRIBUTE__(no_thread_safety_analysis) + +#ifdef USE_LOCK_STYLE_THREAD_SAFETY_ATTRIBUTES +/* + * The original version of thread safety analysis the following attribute + * definitions. These use a lock-based terminology. They are still in use + * by existing thread safety code, and will continue to be supported. + */ + +/* Deprecated. */ +#define PT_GUARDED_VAR \ + THREAD_ANNOTATION_ATTRIBUTE__(pt_guarded_var) + +/* Deprecated. */ +#define GUARDED_VAR \ + THREAD_ANNOTATION_ATTRIBUTE__(guarded_var) + +/* Replaced by REQUIRES */ +#define EXCLUSIVE_LOCKS_REQUIRED(...) \ + THREAD_ANNOTATION_ATTRIBUTE__(exclusive_locks_required(__VA_ARGS__)) + +/* Replaced by REQUIRES_SHARED */ +#define SHARED_LOCKS_REQUIRED(...) \ + THREAD_ANNOTATION_ATTRIBUTE__(shared_locks_required(__VA_ARGS__)) + +/* Replaced by CAPABILITY */ +#define LOCKABLE \ + THREAD_ANNOTATION_ATTRIBUTE__(lockable) + +/* Replaced by SCOPED_CAPABILITY */ +#define SCOPED_LOCKABLE \ + THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable) + +/* Replaced by ACQUIRE */ +#define EXCLUSIVE_LOCK_FUNCTION(...) \ + THREAD_ANNOTATION_ATTRIBUTE__(exclusive_lock_function(__VA_ARGS__)) + +/* Replaced by ACQUIRE_SHARED */ +#define SHARED_LOCK_FUNCTION(...) \ + THREAD_ANNOTATION_ATTRIBUTE__(shared_lock_function(__VA_ARGS__)) + +/* Replaced by RELEASE and RELEASE_SHARED */ +#define UNLOCK_FUNCTION(...) \ + THREAD_ANNOTATION_ATTRIBUTE__(unlock_function(__VA_ARGS__)) + +/* Replaced by TRY_ACQUIRE */ +#define EXCLUSIVE_TRYLOCK_FUNCTION(...) \ + THREAD_ANNOTATION_ATTRIBUTE__(exclusive_trylock_function(__VA_ARGS__)) + +/* Replaced by TRY_ACQUIRE_SHARED */ +#define SHARED_TRYLOCK_FUNCTION(...) \ + THREAD_ANNOTATION_ATTRIBUTE__(shared_trylock_function(__VA_ARGS__)) + +/* Replaced by ASSERT_CAPABILITY */ +#define ASSERT_EXCLUSIVE_LOCK(...) \ + THREAD_ANNOTATION_ATTRIBUTE__(assert_exclusive_lock(__VA_ARGS__)) + +/* Replaced by ASSERT_SHARED_CAPABILITY */ +#define ASSERT_SHARED_LOCK(...) \ + THREAD_ANNOTATION_ATTRIBUTE__(assert_shared_lock(__VA_ARGS__)) + +/* Replaced by EXCLUDE_CAPABILITY */ +#define LOCKS_EXCLUDED(...) \ + THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(__VA_ARGS__)) + +/* Replaced by RETURN_CAPABILITY */ +#define LOCK_RETURNED(x) \ + THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(x)) + +#endif /* USE_LOCK_STYLE_THREAD_SAFETY_ATTRIBUTES */ + +#endif /* __LIBCAMERA_BASE_THREAD_ANNOTATIONS_H__ */ diff --git a/meson.build b/meson.build index 7892a9e3..7147a108 100644 --- a/meson.build +++ b/meson.build @@ -72,6 +72,7 @@ if cc.get_id() == 'clang' cpp_arguments += [ '-Wextra-semi', + '-Wthread-safety', ] endif From patchwork Fri Oct 29 04:14:20 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 14501 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by patchwork.libcamera.org (Postfix) with ESMTPS id EFC36BDB1C for ; Tue, 9 Nov 2021 17:44:19 +0000 (UTC) Received: from pendragon.ideasonboard.com (cpc89244-aztw30-2-0-cust3082.18-1.cable.virginm.net [86.31.172.11]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 88135DEE for ; Tue, 9 Nov 2021 18:44:19 +0100 (CET) Authentication-Results: perceval.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="FBDT7iBL"; dkim-atps=neutral Delivered-To: kbingham@ideasonboard.com Received: from perceval.ideasonboard.com by perceval.ideasonboard.com with LMTP id QKZDOip1e2E4VQAA4E0KoQ (envelope-from ) for ; Fri, 29 Oct 2021 06:14:34 +0200 Received: from mail-pl1-x631.google.com (mail-pl1-x631.google.com [IPv6:2607:f8b0:4864:20::631]) by perceval.ideasonboard.com (Postfix) with ESMTPS id D95F9893 for ; Fri, 29 Oct 2021 06:14:34 +0200 (CEST) Received: by mail-pl1-x631.google.com with SMTP id n11so5988077plf.4 for ; Thu, 28 Oct 2021 21:14:34 -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=EHPkRzs5equ910K/c2+ZUgyZcKmqohSpRMPlVSomN7A=; b=FBDT7iBLfuHyADTO5TU2Kpdf+HZzKFnsiublcVBQI3UoLHa8XSPP7II9wCOuqA+KoQ YPFSCcLVju7iIyco9Sz+cu4k5uM6z7zxG5gxjFc2pGIbNsJVgb24nQ/2kuyOT3zlfNPj Fz0eJBB2xyepjnG3Yk8GNG6cIX4F1gGmEvjnI= 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=EHPkRzs5equ910K/c2+ZUgyZcKmqohSpRMPlVSomN7A=; b=dIP3sC68Rn1SHN8Tmdi3tnjRx6wCpY8v06/CEJGbKigGAsAJD2wqxN1p0wSGCWltdG CUL1SLIhnVKUY97eSd0UqZaMKx5ditQY809iTLYRV7zDYCkr1NQo0bpcJOKWHpS9OO+J 46i3J/HjpHNFwpgfA5N98tPR/86RZy+z42Oa9qqzQGA6XA1xMJ5gAdfZRSGgFv6OwiRd 6Wm1Zvf3XxiwHrixyzYoN75Ng+6WZvXa8dOVvEx0It7d3sK8rcO/oZYeaDwwRvHqJZMO tn9GPRa8D74Ws67EZy49kqWEZbfhNK8OS55+35xkgxpQ/k2ux0xAVGVuq9cATVdedq0W 4xkw== X-Gm-Message-State: AOAM531p3RUJkUY9eA35w1YJyf0bW/YEUJHFkRXH+OpZqCsNtGkUmFVO kY97zCI/sesDh4TOmxCMEEBqDA== X-Google-Smtp-Source: =?utf-8?q?ABdhPJz/UufzX+4JRY5vY+xBLHQNyMdtWIqXilYE6?= =?utf-8?q?Hq5iWqXfd8/5/B4c92EJufKO757QcNQuzM6ZQ=3D=3D?= X-Received: by 2002:a17:90b:1806:: with SMTP id lw6mr14147261pjb.222.1635480873396; Thu, 28 Oct 2021 21:14:33 -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.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Oct 2021 21:14:33 -0700 (PDT) From: Hirokazu Honda To: libcamera-devel@lists.libcamera.org Cc: tfiga@chromium.org, laurent.pinchart@ideasonboard.com, kieran.bingham@ideasonboard.com, Hirokazu Honda Subject: [RFC PATCH 2/6] libcamera: base: Add mutex classes with thread safety annotations Date: Fri, 29 Oct 2021 13:14:20 +0900 Message-Id: <20211029041424.1430886-3-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 X-TUID: eGeTuRKFUMY+ Resent-From: Kieran Bingham Resent-To: parsemail@patchwork.libcamera.org Mutex2 and MutexLocker2 are annotated by clang thread safety annotations. So we can add annotation to code where the classes are used. In the future, they will replace Mutex and MutexLocker. Signed-off-by: Hirokazu Honda --- include/libcamera/base/meson.build | 1 + include/libcamera/base/mutex.h | 66 ++++++++++++++++++++++++++++++ include/libcamera/base/thread.h | 5 +-- 3 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 include/libcamera/base/mutex.h diff --git a/include/libcamera/base/meson.build b/include/libcamera/base/meson.build index 1a71ce5a..37c4435a 100644 --- a/include/libcamera/base/meson.build +++ b/include/libcamera/base/meson.build @@ -13,6 +13,7 @@ libcamera_base_headers = files([ 'flags.h', 'log.h', 'message.h', + 'mutex.h', 'object.h', 'private.h', 'semaphore.h', diff --git a/include/libcamera/base/mutex.h b/include/libcamera/base/mutex.h new file mode 100644 index 00000000..d130988e --- /dev/null +++ b/include/libcamera/base/mutex.h @@ -0,0 +1,66 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2021, Google Inc. + * + * thread.h - Thread support + */ +#ifndef __LIBCAMERA_BASE_MUTEX_H__ +#define __LIBCAMERA_BASE_MUTEX_H__ + +#include + +#include + +namespace libcamera { + +using Mutex = std::mutex; +using MutexLocker = std::unique_lock; + +class CAPABILITY("mutex") Mutex2 final +{ +public: + void lock() ACQUIRE() + { + mutex_.lock(); + } + + void unlock() RELEASE() + { + mutex_.unlock(); + } + + std::mutex &get() { return mutex_; } +private: + std::mutex mutex_; +}; + +class SCOPED_CAPABILITY MutexLocker2 final +{ +public: + MutexLocker2(Mutex2 &mutex) ACQUIRE(mutex) + : lock_(mutex.get()) + { + } + + ~MutexLocker2() RELEASE() + { + } + + void lock() ACQUIRE() + { + lock_.lock(); + } + + void unlock() RELEASE() + { + lock_.unlock(); + } + + std::unique_lock &get() { return lock_; } +private: + std::unique_lock lock_; +}; + +} /* namespace libcamera */ + +#endif /* __LIBCAMERA_BASE_MUTEX_H__ */ diff --git a/include/libcamera/base/thread.h b/include/libcamera/base/thread.h index e0ca0aea..ae630563 100644 --- a/include/libcamera/base/thread.h +++ b/include/libcamera/base/thread.h @@ -8,13 +8,13 @@ #define __LIBCAMERA_BASE_THREAD_H__ #include -#include #include #include #include #include +#include #include #include @@ -26,9 +26,6 @@ class Object; class ThreadData; class ThreadMain; -using Mutex = std::mutex; -using MutexLocker = std::unique_lock; - class Thread { public: From patchwork Fri Oct 29 04:14:21 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 14410 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 D291FBF415 for ; Fri, 29 Oct 2021 04:14:39 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 8CA47600C3; Fri, 29 Oct 2021 06:14:39 +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="MxFZQXfS"; dkim-atps=neutral Received: from mail-pg1-x52b.google.com (mail-pg1-x52b.google.com [IPv6:2607:f8b0:4864:20::52b]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 3123D600C1 for ; Fri, 29 Oct 2021 06:14:37 +0200 (CEST) Received: by mail-pg1-x52b.google.com with SMTP id q187so8697058pgq.2 for ; Thu, 28 Oct 2021 21:14:37 -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=L3Ks0SBFgrAtnuHoUek3d3iZpn1ML52IY5W1kRp0HGs=; b=MxFZQXfS0F1tVQWbIPlLxQ5Ir3QngnyCxetV7mlfGUYa5+8e5Fw9cD8RlPQJvudrCg /aXXCaEZTyf1Xsl3/MMAhIt1LPFyDMf9GwRizlMT4v+LQraDoz+A/Q1rT1jrC2wy0HaT gK6wxHQ00PcY51hHeFHDtVL0MMJcj4CyKRjug= 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=L3Ks0SBFgrAtnuHoUek3d3iZpn1ML52IY5W1kRp0HGs=; b=azXDlUaxCoqVblvekQdneeJrFfdS4j3va3pZjq76Ssls90yyRO/lZ0Gjj0un0xK+dO xgySy4WWtC+o4eyls17YZhw651CB6bIY9IPDU5AheSipFXD0SN+276kFA2CAaJvB6REz bn09h0SbGZ5fEZ3vTf3eA8Jk9Nhf/0eLsZ4LP9cPLcOE9kWyxwwR7c1CfBPlI2lHlwQZ 5gPfifF3WwfHt4OHuqN44ESWrPqsjaQOk3phITeeTOmX5PQWiAhdGcInwtVjTJrx44ay xipGtzmNlzBkvUqM+qKeY8QngP8HvtA+qVkHkH1T6jHLrVaqGOIRFR8RBAMYSWtGq1pS gyYg== X-Gm-Message-State: AOAM532qgZGmNd2m5TJ6FcOvFgKPSTGv6+MJ/kweMFCLjSNU07JUU7HY ZiKfTG3j4crUQ+idhDCg4cBl5EwgqarLBA== X-Google-Smtp-Source: ABdhPJyCnd4t5M/pJVzfiKdirPIjRkUUfgiUsInyH1VAiGxbz4ob93J1SLVRGzhyknXCtgi+ODBytg== X-Received: by 2002:aa7:9099:0:b0:44c:a3b5:ca52 with SMTP id i25-20020aa79099000000b0044ca3b5ca52mr8342615pfa.85.1635480875319; Thu, 28 Oct 2021 21:14:35 -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.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Oct 2021 21:14:34 -0700 (PDT) From: Hirokazu Honda To: libcamera-devel@lists.libcamera.org Date: Fri, 29 Oct 2021 13:14:21 +0900 Message-Id: <20211029041424.1430886-4-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 3/6] android: camera_hal_manager: 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 CameraHalManager. Mutex and MutexLocker there are replaced with Mutex2 and MutexLocer2. Signed-off-by: Hirokazu Honda Reviewed-by: Umang Jain Reviewed-by: Laurent Pinchart --- src/android/camera_hal_manager.cpp | 10 +++++----- src/android/camera_hal_manager.h | 14 ++++++-------- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/src/android/camera_hal_manager.cpp b/src/android/camera_hal_manager.cpp index 5f7bfe26..26715a5f 100644 --- a/src/android/camera_hal_manager.cpp +++ b/src/android/camera_hal_manager.cpp @@ -75,7 +75,7 @@ int CameraHalManager::init() std::tuple CameraHalManager::open(unsigned int id, const hw_module_t *hardwareModule) { - MutexLocker locker(mutex_); + MutexLocker2 locker(mutex_); if (!callbacks_) { LOG(HAL, Error) << "Can't open camera before callbacks are set"; @@ -103,7 +103,7 @@ void CameraHalManager::cameraAdded(std::shared_ptr cam) bool isCameraExternal = false; bool isCameraNew = false; - MutexLocker locker(mutex_); + MutexLocker2 locker(mutex_); /* * Each camera is assigned a unique integer ID when it is seen for the @@ -198,7 +198,7 @@ void CameraHalManager::cameraAdded(std::shared_ptr cam) void CameraHalManager::cameraRemoved(std::shared_ptr cam) { - MutexLocker locker(mutex_); + MutexLocker2 locker(mutex_); auto iter = std::find_if(cameras_.begin(), cameras_.end(), [&cam](const std::unique_ptr &camera) { @@ -257,7 +257,7 @@ int CameraHalManager::getCameraInfo(unsigned int id, struct camera_info *info) if (!info) return -EINVAL; - MutexLocker locker(mutex_); + MutexLocker2 locker(mutex_); CameraDevice *camera = cameraDeviceFromHalId(id); if (!camera) { @@ -280,7 +280,7 @@ void CameraHalManager::setCallbacks(const camera_module_callbacks_t *callbacks) { callbacks_ = callbacks; - MutexLocker locker(mutex_); + MutexLocker2 locker(mutex_); /* * Some external cameras may have been identified before the callbacks_ diff --git a/src/android/camera_hal_manager.h b/src/android/camera_hal_manager.h index 3f6d302a..9b8c3a1f 100644 --- a/src/android/camera_hal_manager.h +++ b/src/android/camera_hal_manager.h @@ -8,7 +8,6 @@ #define __ANDROID_CAMERA_MANAGER_H__ #include -#include #include #include #include @@ -18,6 +17,8 @@ #include #include +#include +#include #include @@ -44,9 +45,6 @@ public: private: LIBCAMERA_DISABLE_COPY_AND_MOVE(CameraHalManager) - using Mutex = std::mutex; - using MutexLocker = std::unique_lock; - static constexpr unsigned int firstExternalCameraId_ = 1000; CameraHalManager(); @@ -56,15 +54,15 @@ private: void cameraAdded(std::shared_ptr cam); void cameraRemoved(std::shared_ptr cam); - CameraDevice *cameraDeviceFromHalId(unsigned int id); + CameraDevice *cameraDeviceFromHalId(unsigned int id) REQUIRES(mutex_); std::unique_ptr cameraManager_; CameraHalConfig halConfig_; const camera_module_callbacks_t *callbacks_; - std::vector> cameras_; - std::map cameraIdsMap_; - Mutex mutex_; + std::vector> cameras_ GUARDED_BY(mutex_); + std::map cameraIdsMap_ GUARDED_BY(mutex_); + libcamera::Mutex2 mutex_; unsigned int numInternalCameras_; unsigned int nextExternalCameraId_; From patchwork Fri Oct 29 04:14:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 14411 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 B17EBBF415 for ; Fri, 29 Oct 2021 04:14:42 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 6E3FD600BA; Fri, 29 Oct 2021 06:14:42 +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="Y3i4XMcj"; dkim-atps=neutral Received: from mail-pg1-x52e.google.com (mail-pg1-x52e.google.com [IPv6:2607:f8b0:4864:20::52e]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id DFB6D600BC for ; Fri, 29 Oct 2021 06:14:38 +0200 (CEST) Received: by mail-pg1-x52e.google.com with SMTP id r28so8720762pga.0 for ; Thu, 28 Oct 2021 21:14:38 -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=McoQw3rc9fJF6a1dSKvOmudXfrS2GJYCbsikZQKHDsU=; b=Y3i4XMcj4Bgh2SOEakHNHVarSc7Q85OhoPKU99Pd285JYh3NhaFAHaNf1/QArmDayv CWkAvh07SJ9OLdc5W5m19G8IRvwGWfNY107tnMt6LBL02X4/J74J7cHtq0sn+Tee0Ulr rJVYxGjgjDjdD/kYRU49KOxBrd5EhF2QZCOYM= 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=McoQw3rc9fJF6a1dSKvOmudXfrS2GJYCbsikZQKHDsU=; b=P0+MZn77HzCY1FHjs59MtQrj1FPrg2a91UT0QO1+ehDT8qan+HhaPxffIPmbOfURWX woFKahSY7yz5WtSwK1sWnwZwX/I7vsXLtdPCtcb4dCjhHe2QrGdoZaVovGNlEbRHOdtI O+TZunsHtd7BpiUnzDuV3asIP6I7e3OBssU79wOCPrC0nf+eoVkW+AjkXuCSJFWtJfJF 6IlhwHDHJ76DhgDrN5D9YIrRJpESIpPXO60N9Kmx8TgMyieSXtxwjEfX+DloH9GPypry wNIyNetXaHDai4wTmNZTDkTEpC8k542oKQXJSHFxpi2zVYqWQvRAdF3Uuh41UL2mDf0R nlIQ== X-Gm-Message-State: AOAM532peG5VMjuM+7bVy/uRRK5FuDYAbff5b16l+Hgq1kbWzXckSeYW x4mCp5vi5tBwDcnpNhlktKXyIlnpGe23sw== X-Google-Smtp-Source: ABdhPJwlZwiYdyZLTcTwTCnjBJ9JZfj0nmDzOU76f/YWv0H2mHvY4NxEC5zv6968g3bOuQgIbBPT3w== X-Received: by 2002:a05:6a00:14c1:b0:47f:5f9:8675 with SMTP id w1-20020a056a0014c100b0047f05f98675mr451763pfu.22.1635480877193; Thu, 28 Oct 2021 21:14:37 -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.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Oct 2021 21:14:36 -0700 (PDT) From: Hirokazu Honda 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 Subject: [libcamera-devel] [RFC PATCH 4/6] android: camera_stream: 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 CameraStream. Mutex and MutexLocker there are replaced with Mutex2 and MutexLocer2. Signed-off-by: Hirokazu Honda Reviewed-by: Umang Jain --- src/android/camera_stream.cpp | 22 ++++++++++++---------- src/android/camera_stream.h | 13 +++++++------ 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/android/camera_stream.cpp b/src/android/camera_stream.cpp index 9023c13c..c5272445 100644 --- a/src/android/camera_stream.cpp +++ b/src/android/camera_stream.cpp @@ -119,12 +119,13 @@ int CameraStream::configure() if (type_ == Type::Internal) { allocator_ = std::make_unique(cameraDevice_->camera()); - mutex_ = std::make_unique(); + mutex_ = std::make_unique(); int ret = allocator_->allocate(stream()); if (ret < 0) return ret; + MutexLocker2 lock(*mutex_); /* Save a pointer to the reserved frame buffers */ for (const auto &frameBuffer : allocator_->buffers(stream())) buffers_.push_back(frameBuffer.get()); @@ -208,7 +209,7 @@ FrameBuffer *CameraStream::getBuffer() if (!allocator_) return nullptr; - std::lock_guard locker(*mutex_); + MutexLocker2 lock(*mutex_); if (buffers_.empty()) { LOG(HAL, Error) << "Buffer underrun"; @@ -226,20 +227,21 @@ void CameraStream::putBuffer(FrameBuffer *buffer) if (!allocator_) return; - std::lock_guard locker(*mutex_); + MutexLocker2 lock(*mutex_); buffers_.push_back(buffer); } CameraStream::PostProcessorWorker::PostProcessorWorker(PostProcessor *postProcessor) - : postProcessor_(postProcessor) + : postProcessor_(postProcessor), + state_(State::Stopped) { } CameraStream::PostProcessorWorker::~PostProcessorWorker() { { - libcamera::MutexLocker lock(mutex_); + libcamera::MutexLocker2 lock(mutex_); state_ = State::Stopped; } @@ -250,7 +252,7 @@ CameraStream::PostProcessorWorker::~PostProcessorWorker() void CameraStream::PostProcessorWorker::start() { { - libcamera::MutexLocker lock(mutex_); + libcamera::MutexLocker2 lock(mutex_); ASSERT(state_ != State::Running); state_ = State::Running; } @@ -261,7 +263,7 @@ void CameraStream::PostProcessorWorker::start() void CameraStream::PostProcessorWorker::queueRequest(Camera3RequestDescriptor::StreamBuffer *dest) { { - MutexLocker lock(mutex_); + MutexLocker2 lock(mutex_); ASSERT(state_ == State::Running); requests_.push(dest); } @@ -271,10 +273,10 @@ void CameraStream::PostProcessorWorker::queueRequest(Camera3RequestDescriptor::S void CameraStream::PostProcessorWorker::run() { - MutexLocker locker(mutex_); + MutexLocker2 locker(mutex_); while (1) { - cv_.wait(locker, [&] { + cv_.wait(locker.get(), [&]() REQUIRES(mutex_) { return state_ != State::Running || !requests_.empty(); }); @@ -308,7 +310,7 @@ void CameraStream::PostProcessorWorker::run() void CameraStream::PostProcessorWorker::flush() { - libcamera::MutexLocker lock(mutex_); + MutexLocker2 lock(mutex_); state_ = State::Flushing; lock.unlock(); diff --git a/src/android/camera_stream.h b/src/android/camera_stream.h index 0c402deb..665bdf5c 100644 --- a/src/android/camera_stream.h +++ b/src/android/camera_stream.h @@ -9,13 +9,14 @@ #include #include -#include #include #include #include +#include #include +#include #include #include @@ -153,11 +154,11 @@ private: private: PostProcessor *postProcessor_; - libcamera::Mutex mutex_; + libcamera::Mutex2 mutex_; std::condition_variable cv_; - std::queue requests_; - State state_ = State::Stopped; + std::queue requests_ GUARDED_BY(mutex_); + State state_ GUARDED_BY(mutex_); }; int waitFence(int fence); @@ -169,12 +170,12 @@ private: const unsigned int index_; std::unique_ptr allocator_; - std::vector buffers_; + std::vector buffers_ GUARDED_BY(mutex_); /* * The class has to be MoveConstructible as instances are stored in * an std::vector in CameraDevice. */ - std::unique_ptr mutex_; + std::unique_ptr mutex_; std::unique_ptr postProcessor_; std::unique_ptr worker_; 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_; From patchwork Fri Oct 29 04:14:24 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 14413 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 A513CC324E 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 58152600C3; Fri, 29 Oct 2021 06:14:45 +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="EunkjQ+k"; dkim-atps=neutral Received: from mail-pj1-x102d.google.com (mail-pj1-x102d.google.com [IPv6:2607:f8b0:4864:20::102d]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id A7945600BF for ; Fri, 29 Oct 2021 06:14:42 +0200 (CEST) Received: by mail-pj1-x102d.google.com with SMTP id gn3so6343595pjb.0 for ; Thu, 28 Oct 2021 21:14:42 -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=6v2IZFgdAKxfsaRmSEdNBR037Vi1LIHxKaYs8A+IT00=; b=EunkjQ+kq/NXaAJgTQsw1vvXRUu9MjoZGVQC8hBEai7sUnw22NkWHmbGzWU6lVg+WL 7fSTZmALItosugJ+4bgCVsf+pT7pmy/lmoWQkz995nw3QVKjnulaT0xzwhzBqH9oRCme a6V/ympoTCD30gx9Uaf+6cEXx5YDm6VAhfnUc= 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=6v2IZFgdAKxfsaRmSEdNBR037Vi1LIHxKaYs8A+IT00=; b=wm1HM8DjdFvfl+luKfsBlOLQw4UOcH2O+84cFABSzpL5alXnAEQXrjcbo0F9+Gdxtr 8Of+FISZM6cS65uxN/iIf6HvSLXv6yMM8xpqMbnVDcKsNEs4wOe9DP8IDjJWT0veU68q IHuF1mLRunlAtrSEAte2CFYrvzxxUlym3q7yA8V22kEHXrE0tuncxl91xJwxRBixHhPw z48dY7e0f1dFNk/cAyfwgBFhWjA0pHbO4EszFkGCwqoRlBuEMR6VbYPyx77Zpx7R5fFK jXhrLTiNkJwyDX0T+KH1Lqk6s/pcxVsbnpT6mUvAKrMaYdlTpLAwTVVIKu9w8F5Rj28/ 8kVg== X-Gm-Message-State: AOAM531T3vxQ+BzRsNVANH9rSvWhEJps7tGk15GrCdmU8/pBVCJ6LoQd gC20z16vU0dNP9L64/7e/Bhx9ZhlEhPBgQ== X-Google-Smtp-Source: ABdhPJyli5Ufi3FIsyOfhQGGsFD/2GS+v+ES9ehsaJ+Z54FzqjWRiJvsq4eC92JYXmZOz8PLwfC5xQ== X-Received: by 2002:a17:90a:7384:: with SMTP id j4mr8986444pjg.185.1635480880995; Thu, 28 Oct 2021 21:14:40 -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.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Oct 2021 21:14:40 -0700 (PDT) From: Hirokazu Honda To: libcamera-devel@lists.libcamera.org Date: Fri, 29 Oct 2021 13:14:24 +0900 Message-Id: <20211029041424.1430886-7-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 6/6] android: camera_request: 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 Camera3RequestDescriptor. Mutex and MutexLocker there are replaced with Mutex2 and MutexLocer2. Signed-off-by: Hirokazu Honda Reviewed-by: Umang Jain --- src/android/camera_request.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/android/camera_request.h b/src/android/camera_request.h index cfafa445..0c8e7287 100644 --- a/src/android/camera_request.h +++ b/src/android/camera_request.h @@ -9,10 +9,11 @@ #include #include -#include #include #include +#include +#include #include #include @@ -46,8 +47,8 @@ public: }; /* Keeps track of streams requiring post-processing. */ - std::map pendingStreamsToProcess_; - std::mutex streamsProcessMutex_; + std::map pendingStreamsToProcess_ GUARDED_BY(streamsProcessMutex_); + libcamera::Mutex2 streamsProcessMutex_; Camera3RequestDescriptor(libcamera::Camera *camera, const camera3_capture_request_t *camera3Request);