{"id":21046,"url":"https://patchwork.libcamera.org/api/1.1/patches/21046/?format=json","web_url":"https://patchwork.libcamera.org/patch/21046/","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":"<20240827164255.314432-3-hdegoede@redhat.com>","date":"2024-08-27T16:42:54","name":"[v2,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/21046/mbox/","series":[{"id":4546,"url":"https://patchwork.libcamera.org/api/1.1/series/4546/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=4546","date":"2024-08-27T16:42:52","name":"Fix uvcvideo pipelinehandler keeping /dev/video# open","version":2,"mbox":"https://patchwork.libcamera.org/series/4546/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/21046/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/21046/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 6241EC323E\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 27 Aug 2024 16:43:18 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id C8BC563486;\n\tTue, 27 Aug 2024 18:43:16 +0200 (CEST)","from us-smtp-delivery-124.mimecast.com\n\t(us-smtp-delivery-124.mimecast.com [170.10.133.124])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 6CCB463460\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 27 Aug 2024 18:43:12 +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-404-gt0_-DgCOhml9JXFLUg4vQ-1;\n\tTue, 27 Aug 2024 12:43:09 -0400","from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com\n\t(mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com\n\t[10.30.177.12])\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\n\tid 0E41A1954B20 for <libcamera-devel@lists.libcamera.org>;\n\tTue, 27 Aug 2024 16:43:09 +0000 (UTC)","from localhost.localdomain (unknown [10.39.192.136])\n\tby mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix)\n\twith ESMTP id 911FB1955D89; Tue, 27 Aug 2024 16:43:07 +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=\"T65Ii2/6\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;\n\ts=mimecast20190719; t=1724776991;\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=Ucr6zEA1s6EVz+HJUIm3v0cDc56v2PV3fdRM9iNBc/M=;\n\tb=T65Ii2/61A4cPul1tObLLGBrRfBHxJiIH0Il0mQ3SZ0Hq103BF60kdiFkdfmBczOyAsZ4D\n\tr4byAR6ctfkzmi6GEsM2H6bZ1uOLi+f7A+vOPHgkOKMmQkWMzHDXj6t1bGCPWw7LLOyH4O\n\tGC/Yuz87giKBgtf2pACu0C0jk8G+4tY=","X-MC-Unique":"gt0_-DgCOhml9JXFLUg4vQ-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>","Subject":"[PATCH v2 2/3] camera: Use invokeMethod() for pipe_->acquire() and\n\tpipe_->release()","Date":"Tue, 27 Aug 2024 18:42:54 +0200","Message-ID":"<20240827164255.314432-3-hdegoede@redhat.com>","In-Reply-To":"<20240827164255.314432-1-hdegoede@redhat.com>","References":"<20240827164255.314432-1-hdegoede@redhat.com>","MIME-Version":"1.0","X-Scanned-By":"MIMEDefang 3.0 on 10.30.177.12","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\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":["v2","2/3"]}