[1/2] libcamera: camera_manager: Allow other threads to create cameras
diff mbox series

Message ID 20261130-camera-thread-v1-1-bca905afbeef@ideasonboard.com
State New
Headers show
Series
  • Move PipelineHandlers to their own thread
Related show

Commit Message

Daniel Scally Nov. 30, 2026, 8:30 a.m. UTC
addCamera() and removeCamera() currently restrict the caller to the
CameraManager thread. I can't see a compelling reason to keep that
restriction; the addition / removal of the new Camera to the vector
of Cameras is guarded by a mutex, and the notification of the change
is performed by emitting a signal that will be handled by the program
thread anyway. Removing the restriction makes it much easier to move
PipelineHandlers to their own thread - drop it.

Signed-off-by: Daniel Scally <dan.scally@ideasonboard.com>
---
With the next patch, addCamera() will be called from the new thread
for the PipelineHandler instead of the CameraManager's thread in
PipelineHandler::registerCamera() via manager_->d()->addCamera(). It
might be better to use invokeMethod? addCamera() doesn't create any
objects that might then inherit the wrong thread so it didn't seem to
be necessary, but I'm not totally sure so I thought I'd mention it.
---
 src/libcamera/camera_manager.cpp | 8 --------
 1 file changed, 8 deletions(-)

Comments

Barnabás Pőcze Nov. 30, 2026, 10:43 a.m. UTC | #1
2026. 11. 30. 9:30 keltezéssel, Daniel Scally írta:
> addCamera() and removeCamera() currently restrict the caller to the
> CameraManager thread. I can't see a compelling reason to keep that
> restriction; the addition / removal of the new Camera to the vector
> of Cameras is guarded by a mutex, and the notification of the change
> is performed by emitting a signal that will be handled by the program
> thread anyway. Removing the restriction makes it much easier to move

I'm not sure I didn't misunderstand, but it will run in the "current thread",
not the "program thread". Signals to applications are dispatched directly.
So this will change things a bit, in that an applications handler can now be
called from various different threads. Although as far as I can tell, this
shouldn't be a problematic change in and of itself, in any case.


> PipelineHandlers to their own thread - drop it.
> 
> Signed-off-by: Daniel Scally <dan.scally@ideasonboard.com>
> ---
> With the next patch, addCamera() will be called from the new thread
> for the PipelineHandler instead of the CameraManager's thread in
> PipelineHandler::registerCamera() via manager_->d()->addCamera(). It
> might be better to use invokeMethod? addCamera() doesn't create any
> objects that might then inherit the wrong thread so it didn't seem to
> be necessary, but I'm not totally sure so I thought I'd mention it.
> ---
>   src/libcamera/camera_manager.cpp | 8 --------
>   1 file changed, 8 deletions(-)
> 
> diff --git a/src/libcamera/camera_manager.cpp b/src/libcamera/camera_manager.cpp
> index 9c9073053710096f844670e8e1a5b70f2e4372bb..0f2189217c86e0ecc8673af0f5c7bf191eb2395d 100644
> --- a/src/libcamera/camera_manager.cpp
> +++ b/src/libcamera/camera_manager.cpp
> @@ -198,13 +198,9 @@ void CameraManager::Private::cleanup()
>    *
>    * Device numbers from the SystemDevices property are used by the V4L2
>    * compatibility layer to map V4L2 device nodes to Camera instances.
> - *
> - * \context This function shall be called from the CameraManager thread.
>    */
>   void CameraManager::Private::addCamera(std::shared_ptr<Camera> camera)
>   {
> -	ASSERT(Thread::current() == this);
> -
>   	{
>   		MutexLocker locker(mutex_);
>   
> @@ -236,13 +232,9 @@ void CameraManager::Private::addCamera(std::shared_ptr<Camera> camera)
>    * This function is called by pipeline handlers to unregister cameras from the
>    * camera manager. Unregistered cameras won't be reported anymore by the
>    * cameras() and get() calls, but references may still exist in applications.
> - *
> - * \context This function shall be called from the CameraManager thread.
>    */
>   void CameraManager::Private::removeCamera(std::shared_ptr<Camera> camera)
>   {
> -	ASSERT(Thread::current() == this);
> -
>   	{
>   		MutexLocker locker(mutex_);
>   
>

Patch
diff mbox series

diff --git a/src/libcamera/camera_manager.cpp b/src/libcamera/camera_manager.cpp
index 9c9073053710096f844670e8e1a5b70f2e4372bb..0f2189217c86e0ecc8673af0f5c7bf191eb2395d 100644
--- a/src/libcamera/camera_manager.cpp
+++ b/src/libcamera/camera_manager.cpp
@@ -198,13 +198,9 @@  void CameraManager::Private::cleanup()
  *
  * Device numbers from the SystemDevices property are used by the V4L2
  * compatibility layer to map V4L2 device nodes to Camera instances.
- *
- * \context This function shall be called from the CameraManager thread.
  */
 void CameraManager::Private::addCamera(std::shared_ptr<Camera> camera)
 {
-	ASSERT(Thread::current() == this);
-
 	{
 		MutexLocker locker(mutex_);
 
@@ -236,13 +232,9 @@  void CameraManager::Private::addCamera(std::shared_ptr<Camera> camera)
  * This function is called by pipeline handlers to unregister cameras from the
  * camera manager. Unregistered cameras won't be reported anymore by the
  * cameras() and get() calls, but references may still exist in applications.
- *
- * \context This function shall be called from the CameraManager thread.
  */
 void CameraManager::Private::removeCamera(std::shared_ptr<Camera> camera)
 {
-	ASSERT(Thread::current() == this);
-
 	{
 		MutexLocker locker(mutex_);