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: