From patchwork Sun Dec 26 23:12:55 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 15221 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 45829BF415 for ; Sun, 26 Dec 2021 23:13:10 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id E81D360918; Mon, 27 Dec 2021 00:13:09 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="Xc+kGqp5"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 3E981608E6 for ; Mon, 27 Dec 2021 00:13:06 +0100 (CET) Received: from pendragon.lan (62-78-145-57.bb.dnainternet.fi [62.78.145.57]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id DA3C5148B for ; Mon, 27 Dec 2021 00:13:05 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1640560386; bh=t3SMdk0VGcMS6YGie3R8h/BleNltHY+0ZzOxkUkRncs=; h=From:To:Subject:Date:In-Reply-To:References:From; b=Xc+kGqp59cMmCktum+H7FwOofiUpXyPM1b+UNlRbx73wuEYhQqlA5isV6Ml04ML/y RfFdCwo6tLT3rUYN+47JRy/jUEfqXV943CSMfy96BUiSF76ddHM1QHUGOvvWS8Ha6o 6qX9rFnBqTA+sYXdnVmoMhT1Uklu99jhiabOEXqY= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Mon, 27 Dec 2021 01:12:55 +0200 Message-Id: <20211226231255.18653-3-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211226231255.18653-1-laurent.pinchart@ideasonboard.com> References: <20211226231255.18653-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 2/2] libcamera: pipeline_handler: Make lock() and unlock() thread-safe 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" The PipelineHandler lock() and unlock() functions are documented as thread-safe, but they're not. Fix them using a mutex. Signed-off-by: Laurent Pinchart Reviewed-by: Kieran Bingham Reviewed-by: Jacopo Mondi Reviewed-by: Umang Jain --- Changes since v1: - Add thread safety annotation - Comment that lockOwner_ does not indicate ownership of lock_ --- include/libcamera/internal/pipeline_handler.h | 4 +++- src/libcamera/pipeline_handler.cpp | 5 +++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/include/libcamera/internal/pipeline_handler.h b/include/libcamera/internal/pipeline_handler.h index a7331cedfb16..e5b8ffb4db3d 100644 --- a/include/libcamera/internal/pipeline_handler.h +++ b/include/libcamera/internal/pipeline_handler.h @@ -14,6 +14,7 @@ #include #include +#include #include #include @@ -88,7 +89,8 @@ private: const char *name_; - bool lockOwner_; + Mutex lock_; + bool lockOwner_ LIBCAMERA_TSA_GUARDED_BY(lock_); /* *Not* ownership of lock_ */ friend class PipelineHandlerFactory; }; diff --git a/src/libcamera/pipeline_handler.cpp b/src/libcamera/pipeline_handler.cpp index a9241ac62979..03e4b9e66aa6 100644 --- a/src/libcamera/pipeline_handler.cpp +++ b/src/libcamera/pipeline_handler.cpp @@ -11,6 +11,7 @@ #include #include +#include #include #include @@ -155,6 +156,8 @@ MediaDevice *PipelineHandler::acquireMediaDevice(DeviceEnumerator *enumerator, */ bool PipelineHandler::lock() { + MutexLocker locker(lock_); + /* Do not allow nested locking in the same libcamera instance. */ if (lockOwner_) return false; @@ -183,6 +186,8 @@ bool PipelineHandler::lock() */ void PipelineHandler::unlock() { + MutexLocker locker(lock_); + if (!lockOwner_) return;