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