From patchwork Thu Jan 30 19:58:57 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= X-Patchwork-Id: 22717 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 A2C95BD808 for ; Thu, 30 Jan 2025 19:59:01 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 563F168574; Thu, 30 Jan 2025 20:59:01 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=protonmail.com header.i=@protonmail.com header.b="enXbmsrZ"; dkim-atps=neutral Received: from mail-40133.protonmail.ch (mail-40133.protonmail.ch [185.70.40.133]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 4F5DD68564 for ; Thu, 30 Jan 2025 20:58:59 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail3; t=1738267138; x=1738526338; bh=sZE2KOGbSBN3Vmzmtw3niI/Hu81qYNu7iaBrkn0Uux0=; h=Date:To:From:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector:List-Unsubscribe:List-Unsubscribe-Post; b=enXbmsrZ2KngK7mSvkIpe3p/Rq91zRhvQVlKaKz8GfkIaP5eNsJMUvoHXsZGQrt08 LNg5ZV4BbU8BxARkU/PPuLiNFo9pfUmq7lU35j/JG1ldKKxCGXghCOhw1qQt/9TNj/ TpPJfYrgbutfUVyiVvawTMSJGF/HC5rxJ5CdWgTsCcOuxS7Cvue+rLLR7He2oQ+12A epyWJYq44atQ7kHpzTvDCG5CmY2Nlk1wSTbBMb3ClJ4TmrbVNxLkFoi4yfSTibQPH3 UvJm5jTcO+cCCuZeOdTafD0jCy7P1YjY5RMSeQqwXCbZcQnKnY5c6BieSSKLm33WDz By/V66BnX+VAg== Date: Thu, 30 Jan 2025 19:58:57 +0000 To: libcamera-devel@lists.libcamera.org From: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= Subject: [RFC PATCH v2 8/9] libcamera: base: log: Protect log categories with lock Message-ID: <20250130195811.1230581-9-pobrn@protonmail.com> In-Reply-To: <20250130195811.1230581-1-pobrn@protonmail.com> References: <20250130195811.1230581-1-pobrn@protonmail.com> Feedback-ID: 20568564:user:proton X-Pm-Message-ID: 2e2a475dd5ae2b3250d6eb6c09094d6ee26e9c28 MIME-Version: 1.0 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" Log categories may be added from any thread, so it is important to synchronize access to the `Logger::categories_` list between its two users: `Logger::findOrCreateCategory()` and `Logger::logSetLevel()`. To achieve that, `Logger::{find,register}Category()` are merged into a single function: `Logger::findOrCreateCategory()`; and the mutex from `LogCategory::create()` is moved into the `Logger` class. Furthermore, appropriate `MutexLocker`s are placed in `Logger::findOrCreateCategory()` and `Logger::logSetLevel()`. Signed-off-by: Barnabás Pőcze Reviewed-by: Jacopo Mondi --- include/libcamera/base/log.h | 2 +- src/libcamera/base/log.cpp | 53 ++++++++++++------------------------ 2 files changed, 19 insertions(+), 36 deletions(-) diff --git a/include/libcamera/base/log.h b/include/libcamera/base/log.h index 1fb92603f..b32ec4516 100644 --- a/include/libcamera/base/log.h +++ b/include/libcamera/base/log.h @@ -30,6 +30,7 @@ enum LogSeverity { class LogCategory { public: + explicit LogCategory(std::string_view name); static LogCategory *create(std::string_view name); const std::string &name() const { return name_; } @@ -39,7 +40,6 @@ public: static const LogCategory &defaultCategory(); private: - explicit LogCategory(std::string_view name); const std::string name_; diff --git a/src/libcamera/base/log.cpp b/src/libcamera/base/log.cpp index 19fc5cc67..69aa16c4c 100644 --- a/src/libcamera/base/log.cpp +++ b/src/libcamera/base/log.cpp @@ -402,11 +402,11 @@ private: void parseLogFile(); friend LogCategory; - void registerCategory(LogCategory *category); - LogCategory *findCategory(std::string_view name) const; + LogCategory *findOrCreateCategory(std::string_view name); static bool destroyed_; + Mutex mutex_; std::vector categories_; std::list> levels_; @@ -657,6 +657,8 @@ void Logger::logSetLevel(const char *category, const char *level) if (severity == LogInvalid) return; + MutexLocker locker(mutex_); + for (LogCategory *c : categories_) { if (c->name() == category) { c->setSeverity(severity); @@ -707,17 +709,21 @@ void Logger::parseLogFile() } /** - * \brief Register a log category with the logger - * \param[in] category The log category - * - * Log categories must have unique names. It is invalid to call this function - * if a log category with the same name already exists. + * \brief Find an existing log category with the given name or create one + * \param[in] name Name of the log category + * \return The pointer to the log category found or created */ -void Logger::registerCategory(LogCategory *category) +LogCategory *Logger::findOrCreateCategory(std::string_view name) { - categories_.push_back(category); + MutexLocker locker(mutex_); + + for (LogCategory *category : categories_) { + if (category->name() == name) + return category; + } + + LogCategory *category = categories_.emplace_back(new LogCategory(name)); - const std::string &name = category->name(); for (const std::pair &level : levels_) { bool match = true; @@ -737,22 +743,8 @@ void Logger::registerCategory(LogCategory *category) break; } } -} -/** - * \brief Find an existing log category with the given name - * \param[in] name Name of the log category - * \return The pointer to the found log category or nullptr if not found - */ -LogCategory *Logger::findCategory(std::string_view name) const -{ - if (auto it = std::find_if(categories_.begin(), categories_.end(), - [name](auto c) { return c->name() == name; }); - it != categories_.end()) { - return *it; - } - - return nullptr; + return category; } /** @@ -790,16 +782,7 @@ LogCategory *Logger::findCategory(std::string_view name) const */ LogCategory *LogCategory::create(std::string_view name) { - static Mutex mutex_; - MutexLocker locker(mutex_); - LogCategory *category = Logger::instance()->findCategory(name); - - if (!category) { - category = new LogCategory(name); - Logger::instance()->registerCategory(category); - } - - return category; + return Logger::instance()->findOrCreateCategory(name); } /**