From patchwork Mon Nov 29 11:44:46 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 14840 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 98A3DC324F for ; Mon, 29 Nov 2021 11:45:11 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 3CDDB605B6; Mon, 29 Nov 2021 12:45:11 +0100 (CET) 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="DHRVfr0c"; dkim-atps=neutral Received: from mail-pl1-x635.google.com (mail-pl1-x635.google.com [IPv6:2607:f8b0:4864:20::635]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 51BE660597 for ; Mon, 29 Nov 2021 12:45:08 +0100 (CET) Received: by mail-pl1-x635.google.com with SMTP id v19so11928121plo.7 for ; Mon, 29 Nov 2021 03:45:08 -0800 (PST) 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=d5yT8zNAlqQjYlwlQgZSp7pbadJUD9JTM9ocP49aJHc=; b=DHRVfr0cdtBoR37g1NrN3hVgThd8Q0aYe6+1RrrDOk4wFzMFQRi08iZxL5rb6mJcx5 ttVrX8WOgtXp53DcNFFdxWwX46HQ6eANviQ7GY2f0K60VipWrOFUuwLfP2l6ux5KAZD0 bXp0HldFTWtNIRDiKgSOGTYotvswX6u9o1ohE= 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=d5yT8zNAlqQjYlwlQgZSp7pbadJUD9JTM9ocP49aJHc=; b=QGzcYh1Hv0zSfiBdEd0mt6gjNAV0bSVVlgI4umEbk3ccdLqGROSuW0OlHv2czLtMKJ 9whMHWXOOTd0RZNxHwu1IE1s2LRwZlHYK/iUfGuFy9yTotONvIk/LJXwnPgq3FGFGWzH 2NJrRXWYvP01/ZGAOVHlAtBfr7hT34xsoZ/l1UYEM/PUc76AONyLe12bMupDEDSV64Eu wguSLM6EYmQWlhm1M3D/MyMhtENBUEwd+DSExBrQQI99tqF7e2F43LSRG60lXuz+uYGP kYijcVbu7XetDzBDLiTi3ZD+UcnsVDrP+LIsyGXxlLCEBehlGpG0wv6jDbu2U/3g3Udu lAHQ== X-Gm-Message-State: AOAM531DK5G5AgE8X7zCE42uoTjqgiz3pokiQfn7pgVt52QzbGaJMIK8 fjbwf2vxjjqTnHY2wylREWGG9RSk2Pu9SA== X-Google-Smtp-Source: ABdhPJxsCTk2N0uzctIEPF1v66/Kk/v1Ba+A6o2GvW00lFRKTiUjDNczwe1AAeoqnpYHpBmkPJDizw== X-Received: by 2002:a17:90a:46c9:: with SMTP id x9mr38261632pjg.183.1638186306617; Mon, 29 Nov 2021 03:45:06 -0800 (PST) Received: from hiroh2.tok.corp.google.com ([2401:fa00:8f:203:f254:cda9:46e8:17b7]) by smtp.gmail.com with ESMTPSA id hg4sm17951966pjb.1.2021.11.29.03.45.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Nov 2021 03:45:06 -0800 (PST) From: Hirokazu Honda To: libcamera-devel@lists.libcamera.org Date: Mon, 29 Nov 2021 20:44:46 +0900 Message-Id: <20211129114453.3186042-5-hiroh@chromium.org> X-Mailer: git-send-email 2.34.0.rc2.393.gf8c9666880-goog In-Reply-To: <20211129114453.3186042-1-hiroh@chromium.org> References: <20211129114453.3186042-1-hiroh@chromium.org> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 04/11] libcamera: base: Add thread safety annotation macros 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" 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 Reviewed-by: Laurent Pinchart --- include/libcamera/base/meson.build | 1 + include/libcamera/base/thread_annotations.h | 80 +++++++++++++++++++++ meson.build | 1 + 3 files changed, 82 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..e81929f6 --- /dev/null +++ b/include/libcamera/base/thread_annotations.h @@ -0,0 +1,80 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2021, Google Inc. + * + * thread_annotation.h - Macro of Clang thread safety analysis + */ + +#pragma once + +/* + * Enable thread safety attributes only with clang. + * The attributes can be safely erased when compiling with other compilers. + */ +#if defined(__clang__) && !defined(SWIG) +#define LIBCAMERA_TSA_ATTRIBUTE__(x) __attribute__((x)) +#else +#define LIBCAMERA_TSA_ATTRIBUTE__(x) /* no-op */ +#endif + +/* See https://clang.llvm.org/docs/ThreadSafetyAnalysis.html for these usages. */ + +#define LIBCAMERA_TSA_CAPABILITY(x) \ + LIBCAMERA_TSA_ATTRIBUTE__(capability(x)) + +#define LIBCAMERA_TSA_SCOPED_CAPABILITY \ + LIBCAMERA_TSA_ATTRIBUTE__(scoped_lockable) + +#define LIBCAMERA_TSA_GUARDED_BY(x) \ + LIBCAMERA_TSA_ATTRIBUTE__(guarded_by(x)) + +#define LIBCAMERA_TSA_PT_GUARDED_BY(x) \ + LIBCAMERA_TSA_ATTRIBUTE__(pt_guarded_by(x)) + +#define LIBCAMERA_TSA_ACQUIRED_BEFORE(...) \ + LIBCAMERA_TSA_ATTRIBUTE__(acquired_before(__VA_ARGS__)) + +#define LIBCAMERA_TSA_ACQUIRED_AFTER(...) \ + LIBCAMERA_TSA_ATTRIBUTE__(acquired_after(__VA_ARGS__)) + +#define LIBCAMERA_TSA_REQUIRES(...) \ + LIBCAMERA_TSA_ATTRIBUTE__(requires_capability(__VA_ARGS__)) + +#define LIBCAMERA_TSA_REQUIRES_SHARED(...) \ + LIBCAMERA_TSA_ATTRIBUTE__(requires_shared_capability(__VA_ARGS__)) + +#define LIBCAMERA_TSA_ACQUIRE(...) \ + LIBCAMERA_TSA_ATTRIBUTE__(acquire_capability(__VA_ARGS__)) + +#define LIBCAMERA_TSA_ACQUIRE_SHARED(...) \ + LIBCAMERA_TSA_ATTRIBUTE__(acquire_shared_capability(__VA_ARGS__)) + +#define LIBCAMERA_TSA_RELEASE(...) \ + LIBCAMERA_TSA_ATTRIBUTE__(release_capability(__VA_ARGS__)) + +#define LIBCAMERA_TSA_RELEASE_SHARED(...) \ + LIBCAMERA_TSA_ATTRIBUTE__(release_shared_capability(__VA_ARGS__)) + +#define LIBCAMERA_TSA_RELEASE_GENERIC(...) \ + LIBCAMERA_TSA_ATTRIBUTE__(release_generic_capability(__VA_ARGS__)) + +#define LIBCAMERA_TSA_TRY_ACQUIRE(...) \ + LIBCAMERA_TSA_ATTRIBUTE__(try_acquire_capability(__VA_ARGS__)) + +#define LIBCAMERA_TSA_TRY_ACQUIRE_SHARED(...) \ + LIBCAMERA_TSA_ATTRIBUTE__(try_acquire_shared_capability(__VA_ARGS__)) + +#define LIBCAMERA_TSA_EXCLUDES(...) \ + LIBCAMERA_TSA_ATTRIBUTE__(locks_excluded(__VA_ARGS__)) + +#define LIBCAMERA_TSA_ASSERT_CAPABILITY(x) \ + LIBCAMERA_TSA_ATTRIBUTE__(assert_capability(x)) + +#define LIBCAMERA_TSA_ASSERT_SHARED_CAPABILITY(x) \ + LIBCAMERA_TSA_ATTRIBUTE__(assert_shared_capability(x)) + +#define LIBCAMERA_TSA_RETURN_CAPABILITY(x) \ + LIBCAMERA_TSA_ATTRIBUTE__(lock_returned(x)) + +#define LIBCAMERA_TSA_NO_THREAD_SAFETY_ANALYSIS \ + LIBCAMERA_TSA_ATTRIBUTE__(no_thread_safety_analysis) 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