From patchwork Sun Apr 14 01:35:04 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: 978 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 1474E60DD8 for ; Sun, 14 Apr 2019 03:35:20 +0200 (CEST) X-Halon-ID: 8ff64ebe-5e55-11e9-8e2c-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 8ff64ebe-5e55-11e9-8e2c-005056917f90; Sun, 14 Apr 2019 03:35:19 +0200 (CEST) From: =?utf-8?q?Niklas_S=C3=B6derlund?= To: libcamera-devel@lists.libcamera.org Date: Sun, 14 Apr 2019 03:35:04 +0200 Message-Id: <20190414013506.10515-10-niklas.soderlund@ragnatech.se> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190414013506.10515-1-niklas.soderlund@ragnatech.se> References: <20190414013506.10515-1-niklas.soderlund@ragnatech.se> MIME-Version: 1.0 Subject: [libcamera-devel] [RFC 09/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: Sun, 14 Apr 2019 01:35:21 -0000 Add lock() and unlock() which backed by lockf() allows an instance of libcamera to claim exclusive access to a media device. These functions is the base to allow multiple users of libcamera to coexist in the system without stepping on each others toes. Signed-off-by: Niklas Söderlund --- src/libcamera/include/media_device.h | 4 ++++ src/libcamera/media_device.cpp | 29 +++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/libcamera/include/media_device.h b/src/libcamera/include/media_device.h index 5d28a7bb8214ef4a..e2956549fa201ea3 100644 --- a/src/libcamera/include/media_device.h +++ b/src/libcamera/include/media_device.h @@ -64,6 +64,10 @@ private: bool acquire(); void release(); + bool lockOwner_; + bool lock(); + void unlock(); + std::map objects_; MediaObject *object(unsigned int id); bool addObject(MediaObject *object); diff --git a/src/libcamera/media_device.cpp b/src/libcamera/media_device.cpp index 46931f52688f6c82..5cea4e63715125c8 100644 --- a/src/libcamera/media_device.cpp +++ b/src/libcamera/media_device.cpp @@ -62,7 +62,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) { } @@ -421,6 +422,32 @@ void MediaDevice::release() acquired_ = false; } +bool MediaDevice::lock() +{ + if (fd_ == -1) + return false; + + if (lockOwner_) + return true; + + if (lockf(fd_, F_TLOCK, 0)) + return false; + + lockOwner_ = true; + + return true; +} + +void MediaDevice::unlock() +{ + if (fd_ == -1) + return; + + lockOwner_ = false; + + lockf(fd_, F_ULOCK, 0); +} + /** * \var MediaDevice::objects_ * \brief Global map of media objects (entities, pads, links) keyed by their