libcamera: camera_manager: Do not emit signals while holding lock
Both `CameraManager::Private::{add,remove}Camera()` emit the `camera{Added,Removed}` signals, respectively, while holding the lock protecting the list of cameras. This is problematic because if a callback tries to call `cameras()`, then the same (non-recursive) lock would be locked again. Furthermore, there is no real need to hold the lock while user code is running, so release the lock as soon as possible. Signed-off-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com> Reviewed-by: Harvey Yang <chenghaoyang@chromium.org> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
This commit is contained in:
parent
b3272f7827
commit
d505bd5360
1 changed files with 23 additions and 21 deletions
|
@ -202,6 +202,7 @@ void CameraManager::Private::addCamera(std::shared_ptr<Camera> camera)
|
|||
{
|
||||
ASSERT(Thread::current() == this);
|
||||
|
||||
{
|
||||
MutexLocker locker(mutex_);
|
||||
|
||||
for (const std::shared_ptr<Camera> &c : cameras_) {
|
||||
|
@ -213,13 +214,12 @@ void CameraManager::Private::addCamera(std::shared_ptr<Camera> camera)
|
|||
}
|
||||
}
|
||||
|
||||
cameras_.push_back(std::move(camera));
|
||||
|
||||
unsigned int index = cameras_.size() - 1;
|
||||
cameras_.push_back(camera);
|
||||
}
|
||||
|
||||
/* Report the addition to the public signal */
|
||||
CameraManager *const o = LIBCAMERA_O_PTR();
|
||||
o->cameraAdded.emit(cameras_[index]);
|
||||
o->cameraAdded.emit(camera);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -236,6 +236,7 @@ void CameraManager::Private::removeCamera(std::shared_ptr<Camera> camera)
|
|||
{
|
||||
ASSERT(Thread::current() == this);
|
||||
|
||||
{
|
||||
MutexLocker locker(mutex_);
|
||||
|
||||
auto iter = std::find_if(cameras_.begin(), cameras_.end(),
|
||||
|
@ -245,11 +246,12 @@ void CameraManager::Private::removeCamera(std::shared_ptr<Camera> camera)
|
|||
if (iter == cameras_.end())
|
||||
return;
|
||||
|
||||
cameras_.erase(iter);
|
||||
}
|
||||
|
||||
LOG(Camera, Debug)
|
||||
<< "Unregistering camera '" << camera->id() << "'";
|
||||
|
||||
cameras_.erase(iter);
|
||||
|
||||
/* Report the removal to the public signal */
|
||||
CameraManager *const o = LIBCAMERA_O_PTR();
|
||||
o->cameraRemoved.emit(camera);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue