{"id":21077,"url":"https://patchwork.libcamera.org/api/1.1/patches/21077/?format=json","web_url":"https://patchwork.libcamera.org/patch/21077/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20240830111207.46455-3-hdegoede@redhat.com>","date":"2024-08-30T11:12:06","name":"[v3,2/3] camera: Use invokeMethod() for pipe_->acquire() and pipe_->release()","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"b2a451bc6000064e9274ced97c81d41d9197287c","submitter":{"id":102,"url":"https://patchwork.libcamera.org/api/1.1/people/102/?format=json","name":"Hans de Goede","email":"hdegoede@redhat.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/21077/mbox/","series":[{"id":4549,"url":"https://patchwork.libcamera.org/api/1.1/series/4549/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=4549","date":"2024-08-30T11:12:04","name":"Fix uvcvideo pipelinehandler keeping /dev/video# open","version":3,"mbox":"https://patchwork.libcamera.org/series/4549/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/21077/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/21077/checks/","tags":{},"headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id CDC27C32C2\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 30 Aug 2024 11:12:29 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 4B519634CC;\n\tFri, 30 Aug 2024 13:12:29 +0200 (CEST)","from us-smtp-delivery-124.mimecast.com\n\t(us-smtp-delivery-124.mimecast.com [170.10.129.124])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 7ADAF6341E\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 30 Aug 2024 13:12:24 +0200 (CEST)","from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com\n\t(ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63])\n\tby relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3,\n\tcipher=TLS_AES_256_GCM_SHA384) id us-mta-517-2B86pn5JPpizPv6TUk2FLQ-1;\n\tFri, 30 Aug 2024 07:12:20 -0400","from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com\n\t(mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com\n\t[10.30.177.15])\n\t(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n\tkey-exchange X25519 server-signature RSA-PSS (2048 bits)\n\tserver-digest SHA256) (No client certificate requested)\n\tby mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix)\n\twith ESMTPS id 6970D1956064; Fri, 30 Aug 2024 11:12:19 +0000 (UTC)","from localhost.localdomain (unknown [10.39.193.217])\n\tby mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix)\n\twith ESMTP id CBCAD1955F1B; Fri, 30 Aug 2024 11:12:17 +0000 (UTC)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=redhat.com header.i=@redhat.com\n\theader.b=\"YvWZqXTK\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;\n\ts=mimecast20190719; t=1725016343;\n\th=from:from:reply-to:subject:subject:date:date:message-id:message-id:\n\tto:to:cc:cc:mime-version:mime-version:content-type:content-type:\n\tcontent-transfer-encoding:content-transfer-encoding:\n\tin-reply-to:in-reply-to:references:references;\n\tbh=sp6s1+UJtIvT5Ha33B41edOQJjVcHMpRWZ9VIqSlOas=;\n\tb=YvWZqXTKcXs2KwMx1M5H9raCS3/ps+cEQ446IUBT0aNI4cdlPNpGvlTyo9O+D3Bt3C4JR3\n\t1461gp7YoZ70Xt86V1Rsg27t5sBsAOPQKz6A0H+TY1RWNruyQ9HRuvLdvsfTNv/wA9kKol\n\t2g42Rk8EDozS+RT9Pf1SW9NxOoXQGjE=","X-MC-Unique":"2B86pn5JPpizPv6TUk2FLQ-1","From":"Hans de Goede <hdegoede@redhat.com>","To":"libcamera-devel@lists.libcamera.org","Cc":"Milan Zamazal <mzamazal@redhat.com>, Maxime Ripard <mripard@redhat.com>, \n\tHans de Goede <hdegoede@redhat.com>,\n\tLaurent Pinchart <laurent.pinchart@ideasonboard.com>","Subject":"[PATCH v3 2/3] camera: Use invokeMethod() for pipe_->acquire() and\n\tpipe_->release()","Date":"Fri, 30 Aug 2024 13:12:06 +0200","Message-ID":"<20240830111207.46455-3-hdegoede@redhat.com>","In-Reply-To":"<20240830111207.46455-1-hdegoede@redhat.com>","References":"<20240830111207.46455-1-hdegoede@redhat.com>","MIME-Version":"1.0","X-Scanned-By":"MIMEDefang 3.0 on 10.30.177.15","X-Mimecast-Spam-Score":"0","X-Mimecast-Originator":"redhat.com","Content-Transfer-Encoding":"8bit","Content-Type":"text/plain; charset=\"US-ASCII\"; x-default=true","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"The uvcvideo driver needs to open / close its /dev/video# node from\npipe_->acquireDevices() / pipe_->releaseDevices().\n\nV4L2VideoDevice::open() creates an EventNotifier and this notifier needs\nto be created from the CameraManager thread.\n\nUse invokeMethod() for pipe_->acquire() and pipe_->release() so that\nthe EventNotifiers are created from the CameraManager thread context.\n\nRunning pipe_->acquire() and pipe_->release() from the CameraManager\nthread context serializes all calls to them. Drop PipelineHandler::lock_\nthis now is no longer necessary and update the \"\\context\" part of\nthe documentation for acquire[Device]() and release[Device]() to match.\n\nNote the delayed opening of /dev/video# is a special case because\nthe kernel uvcvideo driver powers on the USB device as soon as /dev/video#\nis opened. This behavior should *not* be copied by other pipeline handlers.\n\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\nSigned-off-by: Hans de Goede <hdegoede@redhat.com>\n---\nChanges in v2:\n- Add a note to the commit message that opening/closing /dev/video# from\n  acquire()/release() as the uvcvideo pipeline handler does is an exception\n  and that this behavior should not be copied by other pipeline handlers\n- Drop lock_, update \"\\context\" in doxygen docs\n---\n include/libcamera/internal/pipeline_handler.h |  5 +----\n src/libcamera/camera.cpp                      |  6 ++++--\n src/libcamera/pipeline_handler.cpp            | 12 ++++++------\n 3 files changed, 11 insertions(+), 12 deletions(-)","diff":"diff --git a/include/libcamera/internal/pipeline_handler.h b/include/libcamera/internal/pipeline_handler.h\nindex 597f7c3e..c33cf715 100644\n--- a/include/libcamera/internal/pipeline_handler.h\n+++ b/include/libcamera/internal/pipeline_handler.h\n@@ -14,7 +14,6 @@\n #include <sys/types.h>\n #include <vector>\n \n-#include <libcamera/base/mutex.h>\n #include <libcamera/base/object.h>\n \n #include <libcamera/controls.h>\n@@ -99,9 +98,7 @@ private:\n \tstd::queue<Request *> waitingRequests_;\n \n \tconst char *name_;\n-\n-\tMutex lock_;\n-\tunsigned int useCount_ LIBCAMERA_TSA_GUARDED_BY(lock_);\n+\tunsigned int useCount_;\n \n \tfriend class PipelineHandlerFactoryBase;\n };\ndiff --git a/src/libcamera/camera.cpp b/src/libcamera/camera.cpp\nindex 4e393f89..61925e83 100644\n--- a/src/libcamera/camera.cpp\n+++ b/src/libcamera/camera.cpp\n@@ -995,7 +995,8 @@ int Camera::acquire()\n \tif (ret < 0)\n \t\treturn ret == -EACCES ? -EBUSY : ret;\n \n-\tif (!d->pipe_->acquire(this)) {\n+\tif (!d->pipe_->invokeMethod(&PipelineHandler::acquire,\n+\t\t\t\t    ConnectionTypeBlocking, this)) {\n \t\tLOG(Camera, Info)\n \t\t\t<< \"Pipeline handler in use by another process\";\n \t\treturn -EBUSY;\n@@ -1030,7 +1031,8 @@ int Camera::release()\n \t\treturn ret == -EACCES ? -EBUSY : ret;\n \n \tif (d->isAcquired())\n-\t\td->pipe_->release(this);\n+\t\td->pipe_->invokeMethod(&PipelineHandler::release,\n+\t\t\t\t       ConnectionTypeBlocking, this);\n \n \td->setState(Private::CameraAvailable);\n \ndiff --git a/src/libcamera/pipeline_handler.cpp b/src/libcamera/pipeline_handler.cpp\nindex 861815cb..b18b6d0b 100644\n--- a/src/libcamera/pipeline_handler.cpp\n+++ b/src/libcamera/pipeline_handler.cpp\n@@ -157,7 +157,7 @@ MediaDevice *PipelineHandler::acquireMediaDevice(DeviceEnumerator *enumerator,\n  * Pipeline handlers shall not call this function directly as the Camera class\n  * handles access internally.\n  *\n- * \\context This function is \\threadsafe.\n+ * \\context This function is called from the CameraManager thread.\n  *\n  * \\return True if the pipeline handler was acquired, false if another process\n  * has already acquired it\n@@ -165,8 +165,6 @@ MediaDevice *PipelineHandler::acquireMediaDevice(DeviceEnumerator *enumerator,\n  */\n bool PipelineHandler::acquire(Camera *camera)\n {\n-\tMutexLocker locker(lock_);\n-\n \tif (useCount_ == 0) {\n \t\tfor (std::shared_ptr<MediaDevice> &media : mediaDevices_) {\n \t\t\tif (!media->lock()) {\n@@ -199,14 +197,12 @@ bool PipelineHandler::acquire(Camera *camera)\n  * Pipeline handlers shall not call this function directly as the Camera class\n  * handles access internally.\n  *\n- * \\context This function is \\threadsafe.\n+ * \\context This function is called from the CameraManager thread.\n  *\n  * \\sa acquire()\n  */\n void PipelineHandler::release(Camera *camera)\n {\n-\tMutexLocker locker(lock_);\n-\n \tASSERT(useCount_);\n \n \treleaseDevice(camera);\n@@ -230,6 +226,8 @@ void PipelineHandler::release(Camera *camera)\n  * powers on the USB device as soon as /dev/video# is opened. This behavior\n  * should *not* be copied by other pipeline handlers.\n  *\n+ * \\context This function is called from the CameraManager thread.\n+ *\n  * \\return True on success, false on failure\n  * \\sa releaseDevice()\n  */\n@@ -250,6 +248,8 @@ bool PipelineHandler::acquireDevice([[maybe_unused]] Camera *camera)\n  * release them until releaseDevice() has been called for all previously\n  * acquired cameras.\n  *\n+ * \\context This function is called from the CameraManager thread.\n+ *\n  * \\sa acquireDevice()\n  */\n void PipelineHandler::releaseDevice([[maybe_unused]] Camera *camera)\n","prefixes":["v3","2/3"]}