| Message ID | 20261130-camera-thread-v1-2-bca905afbeef@ideasonboard.com |
|---|---|
| State | New |
| Headers | show |
| Series |
|
| Related | show |
2026. 11. 30. 9:30 keltezéssel, Daniel Scally írta: > All PipelineHandlers and the Cameras that they create currently live > in the CameraManager Thread. This creates a problem when a Camera is > streaming and a second Camera is started; the event loops that drive > buffers to the ISP video devices then share a thread with ioctl()s > to the driver, and this means the loops stall whilst those ioctl()s > are running. > > To solve the issue move PipelineHandlers to their own thread. > > Signed-off-by: Daniel Scally <dan.scally@ideasonboard.com> > --- > include/libcamera/internal/pipeline_handler.h | 2 ++ > src/libcamera/camera_manager.cpp | 3 ++- > src/libcamera/pipeline_handler.cpp | 8 ++++++++ > 3 files changed, 12 insertions(+), 1 deletion(-) > > diff --git a/include/libcamera/internal/pipeline_handler.h b/include/libcamera/internal/pipeline_handler.h > index 6a018fe428661c179dc35ec6a986dd5ae22e7b92..d9fabd8121ce1c296c4e2a0951aa7ba5d63f0d2b 100644 > --- a/include/libcamera/internal/pipeline_handler.h > +++ b/include/libcamera/internal/pipeline_handler.h > @@ -30,8 +30,10 @@ class FrameBuffer; > class MediaDevice; > class PipelineHandler; > class Request; > +class Thread; > > class PipelineHandler : public std::enable_shared_from_this<PipelineHandler>, > + public Thread, > public Object > { > public: > diff --git a/src/libcamera/camera_manager.cpp b/src/libcamera/camera_manager.cpp > index 0f2189217c86e0ecc8673af0f5c7bf191eb2395d..42c2892168031c25997710785dcaab6e57c14fbc 100644 > --- a/src/libcamera/camera_manager.cpp > +++ b/src/libcamera/camera_manager.cpp > @@ -158,7 +158,8 @@ void CameraManager::Private::pipelineFactoryMatch(const PipelineHandlerFactoryBa > /* Provide as many matching pipelines as possible. */ > while (1) { > std::shared_ptr<PipelineHandler> pipe = factory->create(o); > - if (!pipe->match(enumerator_.get())) > + if (!pipe->invokeMethod(&PipelineHandler::match, > + ConnectionTypeBlocking, enumerator_.get())) > break; > > LOG(Camera, Debug) > diff --git a/src/libcamera/pipeline_handler.cpp b/src/libcamera/pipeline_handler.cpp > index a803f6208c324efc8fb4f244b28d301d0c1f70c4..43aa1a62de013975215bf4ac342a22b99f735fcc 100644 > --- a/src/libcamera/pipeline_handler.cpp > +++ b/src/libcamera/pipeline_handler.cpp > @@ -13,6 +13,8 @@ > > #include <libcamera/base/log.h> > #include <libcamera/base/mutex.h> > +#include <libcamera/base/object.h> > +#include <libcamera/base/thread.h> > #include <libcamera/base/utils.h> > > #include <libcamera/camera.h> > @@ -74,12 +76,18 @@ PipelineHandler::PipelineHandler(CameraManager *manager, > : manager_(manager), maxQueuedRequestsDevice_(maxQueuedRequestsDevice), > useCount_(0) Thread("ph:" + name()) or something like that could be nice. > { > + moveToThread(this); > + Thread::start(); > } > > PipelineHandler::~PipelineHandler() > { > for (std::shared_ptr<MediaDevice> &media : mediaDevices_) > media->release(); One example that just came to mind: if I'm not mistaken, this `release()` can be called on the same object at the same time as another pipeline handler calling `busy()` in `DeviceEnumerator::search()`, that can easily be a data race on `MediaDevice::acquired_`. I don't want to be overly pessimistic, but given that the whole of libcamera has so far been written with the single camera manager thread in mind, where all pipeline handlers run, I cannot help but think that there are other issues like this or more serious. :( But in any case, I think running buffer allocation ioctls and stream processing are probably best not run in the same thread. Some kind of async worker thread approach could also be entertained, but I suppose that might even be more complex. > + > + exit(); > + wait(); > + removeMessages(this); > } > > /** >
Quoting Barnabás Pőcze (2026-11-30 11:08:06) > 2026. 11. 30. 9:30 keltezéssel, Daniel Scally írta: > > All PipelineHandlers and the Cameras that they create currently live > > in the CameraManager Thread. This creates a problem when a Camera is > > streaming and a second Camera is started; the event loops that drive > > buffers to the ISP video devices then share a thread with ioctl()s > > to the driver, and this means the loops stall whilst those ioctl()s > > are running. > > > > To solve the issue move PipelineHandlers to their own thread. > > > > Signed-off-by: Daniel Scally <dan.scally@ideasonboard.com> > > --- > > include/libcamera/internal/pipeline_handler.h | 2 ++ > > src/libcamera/camera_manager.cpp | 3 ++- > > src/libcamera/pipeline_handler.cpp | 8 ++++++++ > > 3 files changed, 12 insertions(+), 1 deletion(-) > > > > diff --git a/include/libcamera/internal/pipeline_handler.h b/include/libcamera/internal/pipeline_handler.h > > index 6a018fe428661c179dc35ec6a986dd5ae22e7b92..d9fabd8121ce1c296c4e2a0951aa7ba5d63f0d2b 100644 > > --- a/include/libcamera/internal/pipeline_handler.h > > +++ b/include/libcamera/internal/pipeline_handler.h > > @@ -30,8 +30,10 @@ class FrameBuffer; > > class MediaDevice; > > class PipelineHandler; > > class Request; > > +class Thread; > > > > class PipelineHandler : public std::enable_shared_from_this<PipelineHandler>, > > + public Thread, > > public Object > > { > > public: > > diff --git a/src/libcamera/camera_manager.cpp b/src/libcamera/camera_manager.cpp > > index 0f2189217c86e0ecc8673af0f5c7bf191eb2395d..42c2892168031c25997710785dcaab6e57c14fbc 100644 > > --- a/src/libcamera/camera_manager.cpp > > +++ b/src/libcamera/camera_manager.cpp > > @@ -158,7 +158,8 @@ void CameraManager::Private::pipelineFactoryMatch(const PipelineHandlerFactoryBa > > /* Provide as many matching pipelines as possible. */ > > while (1) { > > std::shared_ptr<PipelineHandler> pipe = factory->create(o); > > - if (!pipe->match(enumerator_.get())) > > + if (!pipe->invokeMethod(&PipelineHandler::match, > > + ConnectionTypeBlocking, enumerator_.get())) > > break; > > > > LOG(Camera, Debug) > > diff --git a/src/libcamera/pipeline_handler.cpp b/src/libcamera/pipeline_handler.cpp > > index a803f6208c324efc8fb4f244b28d301d0c1f70c4..43aa1a62de013975215bf4ac342a22b99f735fcc 100644 > > --- a/src/libcamera/pipeline_handler.cpp > > +++ b/src/libcamera/pipeline_handler.cpp > > @@ -13,6 +13,8 @@ > > > > #include <libcamera/base/log.h> > > #include <libcamera/base/mutex.h> > > +#include <libcamera/base/object.h> > > +#include <libcamera/base/thread.h> > > #include <libcamera/base/utils.h> > > > > #include <libcamera/camera.h> > > @@ -74,12 +76,18 @@ PipelineHandler::PipelineHandler(CameraManager *manager, > > : manager_(manager), maxQueuedRequestsDevice_(maxQueuedRequestsDevice), > > useCount_(0) > > Thread("ph:" + name()) > > or something like that could be nice. > > > { > > + moveToThread(this); > > + Thread::start(); > > } > > > > PipelineHandler::~PipelineHandler() > > { > > for (std::shared_ptr<MediaDevice> &media : mediaDevices_) > > media->release(); > > One example that just came to mind: if I'm not mistaken, this `release()` > can be called on the same object at the same time as another pipeline handler > calling `busy()` in `DeviceEnumerator::search()`, that can easily be a data > race on `MediaDevice::acquired_`. > > I don't want to be overly pessimistic, but given that the whole of libcamera has so far I'm afraid the unit tests share the pessimism: - https://gitlab.freedesktop.org/camera/libcamera-testing/-/jobs/117657067#L344 So this might need a bit more digging. -- Kieran > been written with the single camera manager thread in mind, where all pipeline handlers run, > I cannot help but think that there are other issues like this or more serious. :( > > But in any case, I think running buffer allocation ioctls and stream processing are > probably best not run in the same thread. Some kind of async worker thread approach > could also be entertained, but I suppose that might even be more complex. > > > > + > > + exit(); > > + wait(); > > + removeMessages(this); > > } > > > > /** > > >
diff --git a/include/libcamera/internal/pipeline_handler.h b/include/libcamera/internal/pipeline_handler.h index 6a018fe428661c179dc35ec6a986dd5ae22e7b92..d9fabd8121ce1c296c4e2a0951aa7ba5d63f0d2b 100644 --- a/include/libcamera/internal/pipeline_handler.h +++ b/include/libcamera/internal/pipeline_handler.h @@ -30,8 +30,10 @@ class FrameBuffer; class MediaDevice; class PipelineHandler; class Request; +class Thread; class PipelineHandler : public std::enable_shared_from_this<PipelineHandler>, + public Thread, public Object { public: diff --git a/src/libcamera/camera_manager.cpp b/src/libcamera/camera_manager.cpp index 0f2189217c86e0ecc8673af0f5c7bf191eb2395d..42c2892168031c25997710785dcaab6e57c14fbc 100644 --- a/src/libcamera/camera_manager.cpp +++ b/src/libcamera/camera_manager.cpp @@ -158,7 +158,8 @@ void CameraManager::Private::pipelineFactoryMatch(const PipelineHandlerFactoryBa /* Provide as many matching pipelines as possible. */ while (1) { std::shared_ptr<PipelineHandler> pipe = factory->create(o); - if (!pipe->match(enumerator_.get())) + if (!pipe->invokeMethod(&PipelineHandler::match, + ConnectionTypeBlocking, enumerator_.get())) break; LOG(Camera, Debug) diff --git a/src/libcamera/pipeline_handler.cpp b/src/libcamera/pipeline_handler.cpp index a803f6208c324efc8fb4f244b28d301d0c1f70c4..43aa1a62de013975215bf4ac342a22b99f735fcc 100644 --- a/src/libcamera/pipeline_handler.cpp +++ b/src/libcamera/pipeline_handler.cpp @@ -13,6 +13,8 @@ #include <libcamera/base/log.h> #include <libcamera/base/mutex.h> +#include <libcamera/base/object.h> +#include <libcamera/base/thread.h> #include <libcamera/base/utils.h> #include <libcamera/camera.h> @@ -74,12 +76,18 @@ PipelineHandler::PipelineHandler(CameraManager *manager, : manager_(manager), maxQueuedRequestsDevice_(maxQueuedRequestsDevice), useCount_(0) { + moveToThread(this); + Thread::start(); } PipelineHandler::~PipelineHandler() { for (std::shared_ptr<MediaDevice> &media : mediaDevices_) media->release(); + + exit(); + wait(); + removeMessages(this); } /**
All PipelineHandlers and the Cameras that they create currently live in the CameraManager Thread. This creates a problem when a Camera is streaming and a second Camera is started; the event loops that drive buffers to the ISP video devices then share a thread with ioctl()s to the driver, and this means the loops stall whilst those ioctl()s are running. To solve the issue move PipelineHandlers to their own thread. Signed-off-by: Daniel Scally <dan.scally@ideasonboard.com> --- include/libcamera/internal/pipeline_handler.h | 2 ++ src/libcamera/camera_manager.cpp | 3 ++- src/libcamera/pipeline_handler.cpp | 8 ++++++++ 3 files changed, 12 insertions(+), 1 deletion(-)