diff options
author | Laurent Pinchart <laurent.pinchart@ideasonboard.com> | 2021-08-29 21:03:23 +0300 |
---|---|---|
committer | Laurent Pinchart <laurent.pinchart@ideasonboard.com> | 2021-11-19 22:38:44 +0200 |
commit | d9ae18088a62afc9cebcaa35809fa2457c37bac8 (patch) | |
tree | e7e04f7e5a52944ab2a699104ea9214474649971 | |
parent | 86364c7b5e7233b6658d14ca2d49681e30af8ffe (diff) |
libcamera: media_device: Move recursive lock handling to pipeline handler
The MediaDevice lock is meant to prevent concurrent usage of multiple
cameras from the same pipeline handlers. As media devices are acquired
by pipeline handlers, we can't have multiple pipeline handlers trying to
lock the same media device. The recursive locking detection can thus be
moved to the pipeline handler. This simplifies the media device
implementation that now implements true lock semantics, and prepares for
support of concurrent camera usage.
Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
-rw-r--r-- | include/libcamera/internal/media_device.h | 1 | ||||
-rw-r--r-- | include/libcamera/internal/pipeline_handler.h | 2 | ||||
-rw-r--r-- | src/libcamera/media_device.cpp | 14 | ||||
-rw-r--r-- | src/libcamera/pipeline_handler.cpp | 13 |
4 files changed, 15 insertions, 15 deletions
diff --git a/include/libcamera/internal/media_device.h b/include/libcamera/internal/media_device.h index 1f2304c1..e86e5074 100644 --- a/include/libcamera/internal/media_device.h +++ b/include/libcamera/internal/media_device.h @@ -85,7 +85,6 @@ private: int fd_; bool valid_; bool acquired_; - bool lockOwner_; std::map<unsigned int, MediaObject *> objects_; std::vector<MediaEntity *> entities_; diff --git a/include/libcamera/internal/pipeline_handler.h b/include/libcamera/internal/pipeline_handler.h index 41cba44d..fc6d476b 100644 --- a/include/libcamera/internal/pipeline_handler.h +++ b/include/libcamera/internal/pipeline_handler.h @@ -81,6 +81,8 @@ private: const char *name_; + bool lockOwner_; + friend class PipelineHandlerFactory; }; diff --git a/src/libcamera/media_device.cpp b/src/libcamera/media_device.cpp index aa93da75..ced19a10 100644 --- a/src/libcamera/media_device.cpp +++ b/src/libcamera/media_device.cpp @@ -63,8 +63,7 @@ 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), - lockOwner_(false) + : deviceNode_(deviceNode), fd_(-1), valid_(false), acquired_(false) { } @@ -146,15 +145,9 @@ 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; } @@ -172,11 +165,6 @@ void MediaDevice::unlock() if (fd_ == -1) return; - if (!lockOwner_) - return; - - lockOwner_ = false; - lockf(fd_, F_ULOCK, 0); } diff --git a/src/libcamera/pipeline_handler.cpp b/src/libcamera/pipeline_handler.cpp index f69c4f03..3d61311c 100644 --- a/src/libcamera/pipeline_handler.cpp +++ b/src/libcamera/pipeline_handler.cpp @@ -62,7 +62,7 @@ LOG_DEFINE_CATEGORY(Pipeline) * respective factories. */ PipelineHandler::PipelineHandler(CameraManager *manager) - : manager_(manager) + : manager_(manager), lockOwner_(false) { } @@ -150,6 +150,10 @@ MediaDevice *PipelineHandler::acquireMediaDevice(DeviceEnumerator *enumerator, */ bool PipelineHandler::lock() { + /* Do not allow nested locking in the same libcamera instance. */ + if (lockOwner_) + return false; + for (std::shared_ptr<MediaDevice> &media : mediaDevices_) { if (!media->lock()) { unlock(); @@ -157,6 +161,8 @@ bool PipelineHandler::lock() } } + lockOwner_ = true; + return true; } @@ -172,8 +178,13 @@ bool PipelineHandler::lock() */ void PipelineHandler::unlock() { + if (lockOwner_) + return; + for (std::shared_ptr<MediaDevice> &media : mediaDevices_) media->unlock(); + + lockOwner_ = false; } /** |