summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaurent Pinchart <laurent.pinchart@ideasonboard.com>2021-08-29 21:10:57 +0300
committerLaurent Pinchart <laurent.pinchart@ideasonboard.com>2021-11-19 22:38:44 +0200
commitf6a5d675b1cfd1ee9cb9358526e1f84b821b9e07 (patch)
tree73b0c87750d811008f1a919c74fd342685438dc5
parentd9ae18088a62afc9cebcaa35809fa2457c37bac8 (diff)
libcamera: pipeline_handler: Make lock() and unlock() thread-safe
The PipelineHandler lock() and unlock() functions are documented as thread-safe, but they're not. Fix them using a mutex. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
-rw-r--r--include/libcamera/internal/pipeline_handler.h4
-rw-r--r--src/libcamera/pipeline_handler.cpp10
2 files changed, 14 insertions, 0 deletions
diff --git a/include/libcamera/internal/pipeline_handler.h b/include/libcamera/internal/pipeline_handler.h
index fc6d476b..b473eb70 100644
--- a/include/libcamera/internal/pipeline_handler.h
+++ b/include/libcamera/internal/pipeline_handler.h
@@ -14,6 +14,7 @@
#include <vector>
#include <libcamera/base/object.h>
+#include <libcamera/base/thread.h>
#include <libcamera/controls.h>
#include <libcamera/stream.h>
@@ -73,6 +74,8 @@ protected:
CameraManager *manager_;
private:
+ void unlockLocked();
+
void mediaDeviceDisconnected(MediaDevice *media);
virtual void disconnect();
@@ -81,6 +84,7 @@ private:
const char *name_;
+ Mutex lock_;
bool lockOwner_;
friend class PipelineHandlerFactory;
diff --git a/src/libcamera/pipeline_handler.cpp b/src/libcamera/pipeline_handler.cpp
index 3d61311c..2d2bb940 100644
--- a/src/libcamera/pipeline_handler.cpp
+++ b/src/libcamera/pipeline_handler.cpp
@@ -10,6 +10,7 @@
#include <sys/sysmacros.h>
#include <libcamera/base/log.h>
+#include <libcamera/base/thread.h>
#include <libcamera/base/utils.h>
#include <libcamera/camera.h>
@@ -150,6 +151,8 @@ MediaDevice *PipelineHandler::acquireMediaDevice(DeviceEnumerator *enumerator,
*/
bool PipelineHandler::lock()
{
+ MutexLocker locker(lock_);
+
/* Do not allow nested locking in the same libcamera instance. */
if (lockOwner_)
return false;
@@ -178,6 +181,13 @@ bool PipelineHandler::lock()
*/
void PipelineHandler::unlock()
{
+ MutexLocker locker(lock_);
+
+ unlockLocked();
+}
+
+void PipelineHandler::unlockLocked()
+{
if (lockOwner_)
return;