From patchwork Fri May 17 00:54:44 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Niklas_S=C3=B6derlund?= X-Patchwork-Id: 1213 Return-Path: Received: from bin-mail-out-05.binero.net (bin-mail-out-05.binero.net [195.74.38.228]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id E267D61878 for ; Fri, 17 May 2019 02:55:14 +0200 (CEST) X-Halon-ID: 6d46c880-783e-11e9-8d05-005056917f90 Authorized-sender: niklas@soderlund.pp.se Received: from bismarck.berto.se (unknown [89.233.230.99]) by bin-vsp-out-02.atm.binero.net (Halon) with ESMTPA id 6d46c880-783e-11e9-8d05-005056917f90; Fri, 17 May 2019 02:55:13 +0200 (CEST) From: =?utf-8?q?Niklas_S=C3=B6derlund?= To: libcamera-devel@lists.libcamera.org Date: Fri, 17 May 2019 02:54:44 +0200 Message-Id: <20190517005447.27171-9-niklas.soderlund@ragnatech.se> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190517005447.27171-1-niklas.soderlund@ragnatech.se> References: <20190517005447.27171-1-niklas.soderlund@ragnatech.se> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v4 08/11] libcamera: media_device: Add functions to lock device for other processes X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 17 May 2019 00:55:15 -0000 Add lock() and unlock() which are backed by lockf() and allow an instance of libcamera to claim exclusive access to a media device. These functions are the base of allowing multiple user of libcamera to coexist in the system without stepping on each other's toes. Signed-off-by: Niklas Söderlund Reviewed-by: Laurent Pinchart --- src/libcamera/include/media_device.h | 4 ++ src/libcamera/media_device.cpp | 59 +++++++++++++++++++++++++++- 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/src/libcamera/include/media_device.h b/src/libcamera/include/media_device.h index e513a2fee1b91a1b..7b88e2875d59dfc3 100644 --- a/src/libcamera/include/media_device.h +++ b/src/libcamera/include/media_device.h @@ -30,6 +30,9 @@ public: void release(); bool busy() const { return acquired_; } + bool lock(); + void unlock(); + int populate(); bool valid() const { return valid_; } @@ -58,6 +61,7 @@ private: int fd_; bool valid_; bool acquired_; + bool lockOwner_; int open(); void close(); diff --git a/src/libcamera/media_device.cpp b/src/libcamera/media_device.cpp index 62c59e7e8fc0299f..bd99deba098db4ec 100644 --- a/src/libcamera/media_device.cpp +++ b/src/libcamera/media_device.cpp @@ -63,7 +63,8 @@ LOG_DEFINE_CATEGORY(MediaDevice) * populate() before the media graph can be queried. */ MediaDevice::MediaDevice(const std::string &deviceNode) - : deviceNode_(deviceNode), fd_(-1), valid_(false), acquired_(false) + : deviceNode_(deviceNode), fd_(-1), valid_(false), acquired_(false), + lockOwner_(false) { } @@ -118,6 +119,62 @@ void MediaDevice::release() acquired_ = false; } +/** + * \brief Lock the device to prevent it from being used by other instances of + * libcamera + * + * Multiple instances of libcamera might be running on the same system, at the + * same time. To allow the different instances to coexist, system resources in + * the form of media devices must be accessible for enumerating the cameras + * they provide at all times, while still allowing an instance to lock a + * resource while it prepares to actively use a camera from the resource. + * + * This method shall not be called from a pipeline handler implementation + * directly, as the base PipelineHandler implementation handles this on the + * behalf of the specified implementation. + * + * \return True if the device could be locked, false otherwise + * \sa unlock() + */ +bool MediaDevice::lock() +{ + if (fd_ == -1) + return false; + + /* Do not allow nested locking in the same libcamera instance. */ + if (lockOwner_) + return false; + + if (lockf(fd_, F_TLOCK, 0)) + return false; + + lockOwner_ = true; + + return true; +} + +/** + * \brief Unlock the device and free it for use for libcamera instances + * + * This method shall not be called from a pipeline handler implementation + * directly, as the base PipelineHandler implementation handles this on the + * behalf of the specified implementation. + * + * \sa lock() + */ +void MediaDevice::unlock() +{ + if (fd_ == -1) + return; + + if (!lockOwner_) + return; + + lockOwner_ = false; + + lockf(fd_, F_ULOCK, 0); +} + /** * \fn MediaDevice::busy() * \brief Check if a device is in use