From patchwork Thu Jul 21 21:03:51 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 16728 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 A548DBD1F1 for ; Thu, 21 Jul 2022 21:03:55 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 274E96330F; Thu, 21 Jul 2022 23:03:55 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1658437435; bh=XiK4AZ0YODet26M7qUZJ6up+kfd0EPLphcdJuHxwkag=; h=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=omb1WHnRPtlePSAtOyyCCGFI8VDy7wosSjNolcLGEgZEYI9nZPpELjKlQTc5zFk8A 6s6bL9UbfjGdvnd7TTSdnpgtN19DbYmgK+vWU1SAsPxvMAf7HaVnfzHunGq/sOTNuJ c2d1bkqKJhMv2ynOf8UGahYPaU9izjZgIdejACcPB6H9Ns0/IWfKwAmIp/MyjBrqR8 DKllvBA9FN6I8lbaMRyLiCBlshgjIx2H+Ola9zi2EDKf/HDgMKT1zyLuOd2X0iROxz Y2gaQxLfY5iOWtk+qOja/bB/3yBBSqxT4xNfHH89Qolou3yyqavuoCQNMtBDkHF3JP 6/YMMxEAHM0xg== Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 6F57A601B8 for ; Thu, 21 Jul 2022 23:03:53 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="FwnkIsNs"; dkim-atps=neutral Received: from pendragon.ideasonboard.com (62-78-145-57.bb.dnainternet.fi [62.78.145.57]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id D9372496; Thu, 21 Jul 2022 23:03:52 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1658437433; bh=XiK4AZ0YODet26M7qUZJ6up+kfd0EPLphcdJuHxwkag=; h=From:To:Cc:Subject:Date:From; b=FwnkIsNsqgW+lMddZ7JN7s6sgMUQMe2W2ZPhqS/0kxqTtsl44C60zt6q53T1t48wu uJq2Raelimw4amgHflad8NIaJNU5+PD5sG4sEtxbNKfBO2PsrOUU5l0fO6X9UNVsRR IihwHAyorwvF8K3u09sG/tJ+B0ZHt8Q8SqN+Uf6g= To: libcamera-devel@lists.libcamera.org Date: Fri, 22 Jul 2022 00:03:51 +0300 Message-Id: <20220721210351.1908-1-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH] libcamera: Allow concurrent use of cameras from same pipeline handler 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: , X-Patchwork-Original-From: Laurent Pinchart via libcamera-devel From: Laurent Pinchart Reply-To: Laurent Pinchart Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" libcamera implements a pipeline handler locking mechanism based on advisory locks on media devices, to prevent concurrent access to cameras from the same pipeline handler from different processes (this only works between multiple libcamera instances, as other processes won't use advisory locks on media devices). A side effect of the implementation prevents multiple cameras created by the same pipeline handler from being used concurrently. Fix this by turning the PipelineHandler lock() and unlock() functions into acquire() and release(), with a use count to replace the boolean lock flag. The Camera class is updated accordingly. Signed-off-by: Laurent Pinchart Reviewed-by: Kieran Bingham Tested-by: David Plowman --- This may help addressing the problem reported in "[RFC PATCH] qcam: Fix IPU3 camera switching". --- include/libcamera/internal/camera.h | 1 + include/libcamera/internal/pipeline_handler.h | 8 ++- src/libcamera/camera.cpp | 12 +++- src/libcamera/pipeline_handler.cpp | 63 ++++++++++++------- 4 files changed, 55 insertions(+), 29 deletions(-) base-commit: d4eee094e684806dbdb54fbe1bc02c9c590aa7f4 diff --git a/include/libcamera/internal/camera.h b/include/libcamera/internal/camera.h index 597426a6f9d2..38dd94ff8156 100644 --- a/include/libcamera/internal/camera.h +++ b/include/libcamera/internal/camera.h @@ -50,6 +50,7 @@ private: CameraRunning, }; + bool isAcquired() const; bool isRunning() const; int isAccessAllowed(State state, bool allowDisconnected = false, const char *from = __builtin_FUNCTION()) const; diff --git a/include/libcamera/internal/pipeline_handler.h b/include/libcamera/internal/pipeline_handler.h index c3e4c2587ecd..20f1cbb07fea 100644 --- a/include/libcamera/internal/pipeline_handler.h +++ b/include/libcamera/internal/pipeline_handler.h @@ -45,8 +45,8 @@ public: MediaDevice *acquireMediaDevice(DeviceEnumerator *enumerator, const DeviceMatch &dm); - bool lock(); - void unlock(); + bool acquire(); + void release(); virtual CameraConfiguration *generateConfiguration(Camera *camera, const StreamRoles &roles) = 0; @@ -77,6 +77,8 @@ protected: CameraManager *manager_; private: + void unlockMediaDevices(); + void mediaDeviceDisconnected(MediaDevice *media); virtual void disconnect(); @@ -91,7 +93,7 @@ private: const char *name_; Mutex lock_; - bool lockOwner_ LIBCAMERA_TSA_GUARDED_BY(lock_); /* *Not* ownership of lock_ */ + unsigned int useCount_ LIBCAMERA_TSA_GUARDED_BY(lock_); friend class PipelineHandlerFactory; }; diff --git a/src/libcamera/camera.cpp b/src/libcamera/camera.cpp index 2a8ef60e3862..9ce32db1ac78 100644 --- a/src/libcamera/camera.cpp +++ b/src/libcamera/camera.cpp @@ -508,6 +508,11 @@ static const char *const camera_state_names[] = { "Running", }; +bool Camera::Private::isAcquired() const +{ + return state_.load(std::memory_order_acquire) == CameraRunning; +} + bool Camera::Private::isRunning() const { return state_.load(std::memory_order_acquire) == CameraRunning; @@ -811,7 +816,7 @@ int Camera::exportFrameBuffers(Stream *stream, * not blocking, if the device has already been acquired (by the same or another * process) the -EBUSY error code is returned. * - * Acquiring a camera will limit usage of any other camera(s) provided by the + * Acquiring a camera may limit usage of any other camera(s) provided by the * same pipeline handler to the same instance of libcamera. The limit is in * effect until all cameras from the pipeline handler are released. Other * instances of libcamera can still list and examine the cameras but will fail @@ -839,7 +844,7 @@ int Camera::acquire() if (ret < 0) return ret == -EACCES ? -EBUSY : ret; - if (!d->pipe_->lock()) { + if (!d->pipe_->acquire()) { LOG(Camera, Info) << "Pipeline handler in use by another process"; return -EBUSY; @@ -873,7 +878,8 @@ int Camera::release() if (ret < 0) return ret == -EACCES ? -EBUSY : ret; - d->pipe_->unlock(); + if (d->isAcquired()) + d->pipe_->release(); d->setState(Private::CameraAvailable); diff --git a/src/libcamera/pipeline_handler.cpp b/src/libcamera/pipeline_handler.cpp index 675405330f45..7d2d00ef45e6 100644 --- a/src/libcamera/pipeline_handler.cpp +++ b/src/libcamera/pipeline_handler.cpp @@ -68,7 +68,7 @@ LOG_DEFINE_CATEGORY(Pipeline) * respective factories. */ PipelineHandler::PipelineHandler(CameraManager *manager) - : manager_(manager), lockOwner_(false) + : manager_(manager), useCount_(0) { } @@ -143,58 +143,75 @@ MediaDevice *PipelineHandler::acquireMediaDevice(DeviceEnumerator *enumerator, } /** - * \brief Lock all media devices acquired by the pipeline + * \brief Acquire exclusive access to the pipeline handler for the process * - * This function shall not be called from pipeline handler implementation, as - * the Camera class handles locking directly. + * This function locks all the media devices used by the pipeline to ensure + * that no other process can access them concurrently. + * + * Access to a pipeline handler may be acquired recursively from within the + * same process. Every successful acquire() call shall be matched with a + * release() call. This allows concurrent access to the same pipeline handler + * from different cameras within the same process. + * + * Pipeline handlers shall not call this function directly as the Camera class + * handles access internally. * * \context This function is \threadsafe. * - * \return True if the devices could be locked, false otherwise - * \sa unlock() - * \sa MediaDevice::lock() + * \return True if the pipeline handler was acquired, false if another process + * has already acquired it + * \sa release() */ -bool PipelineHandler::lock() +bool PipelineHandler::acquire() { MutexLocker locker(lock_); - /* Do not allow nested locking in the same libcamera instance. */ - if (lockOwner_) - return false; + if (useCount_) { + ++useCount_; + return true; + } for (std::shared_ptr &media : mediaDevices_) { if (!media->lock()) { - unlock(); + unlockMediaDevices(); return false; } } - lockOwner_ = true; - + ++useCount_; return true; } /** - * \brief Unlock all media devices acquired by the pipeline + * \brief Release exclusive access to the pipeline handler * - * This function shall not be called from pipeline handler implementation, as - * the Camera class handles locking directly. + * This function releases access to the pipeline handler previously acquired by + * a call to acquire(). Every release() call shall match a previous successful + * acquire() call. Calling this function on a pipeline handler that hasn't been + * acquired results in undefined behaviour. + * + * Pipeline handlers shall not call this function directly as the Camera class + * handles access internally. * * \context This function is \threadsafe. * - * \sa lock() + * \sa acquire() */ -void PipelineHandler::unlock() +void PipelineHandler::release() { MutexLocker locker(lock_); - if (!lockOwner_) - return; + ASSERT(useCount_); + unlockMediaDevices(); + + --useCount_; +} + +void PipelineHandler::unlockMediaDevices() +{ for (std::shared_ptr &media : mediaDevices_) media->unlock(); - - lockOwner_ = false; } /**