| Message ID | 20261123-kbingham-atomic-disconnects-v1-2-0b251be0bb3e@ideasonboard.com |
|---|---|
| State | New |
| Headers | show |
| Series |
|
| Related | show |
Quoting Kieran Bingham (2026-11-23 17:47:14) > From: Stefan Klug <stefan.klug@ideasonboard.com> > > If the application does not synchronize calls to Camera::queueRequest() > and Camera::stop(), bad things might happen. Add an assert to detect > that. > > Signed-off-by: Stefan Klug <stefan.klug@ideasonboard.com> > Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com> > --- > include/libcamera/internal/camera.h | 4 +++- > src/libcamera/camera.cpp | 10 +++++++--- > src/libcamera/pipeline_handler.cpp | 11 +++++++++++ > 3 files changed, 21 insertions(+), 4 deletions(-) > > diff --git a/include/libcamera/internal/camera.h b/include/libcamera/internal/camera.h > index 8af39047b4f5..f44b15c7da48 100644 > --- a/include/libcamera/internal/camera.h > +++ b/include/libcamera/internal/camera.h > @@ -45,6 +45,8 @@ public: > > const CameraControlValidator *validator() const { return validator_.get(); } > > + bool isRunningOrStopping() const; > + > private: > enum State { > CameraAvailable, > @@ -55,7 +57,6 @@ private: > }; > > bool isAcquired() const; > - bool isRunning() const; > int isAccessAllowed(State state, bool allowDisconnected = false, > const char *from = __builtin_FUNCTION()) const; > int isAccessAllowed(State low, State high, > @@ -64,6 +65,7 @@ private: > > void disconnect(); > void setState(State state); > + State state() const { return state_.load(std::memory_order_acquire); } > > std::shared_ptr<PipelineHandler> pipe_; > std::string id_; > diff --git a/src/libcamera/camera.cpp b/src/libcamera/camera.cpp > index 2b179745aa4e..b6e2d3e51a46 100644 > --- a/src/libcamera/camera.cpp > +++ b/src/libcamera/camera.cpp > @@ -682,9 +682,13 @@ bool Camera::Private::isAcquired() const > return state_.load(std::memory_order_acquire) != CameraAvailable; > } > > -bool Camera::Private::isRunning() const > +bool Camera::Private::isRunningOrStopping() const > { > - return state_.load(std::memory_order_acquire) == CameraRunning; > + State currentState = state_.load(std::memory_order_acquire); > + if (currentState == CameraRunning || currentState == CameraStopping) > + return true; > + > + return false; > } > > int Camera::Private::isAccessAllowed(State state, bool allowDisconnected, > @@ -1450,7 +1454,7 @@ int Camera::stop() > * \todo Make calling stop() when not in 'Running' part of the state > * machine rather than take this shortcut > */ > - if (!d->isRunning()) > + if (d->state() != Private::CameraRunning) > return 0; > > int ret = d->isAccessAllowed(Private::CameraRunning); > diff --git a/src/libcamera/pipeline_handler.cpp b/src/libcamera/pipeline_handler.cpp > index a803f6208c32..9613abc46dda 100644 > --- a/src/libcamera/pipeline_handler.cpp > +++ b/src/libcamera/pipeline_handler.cpp > @@ -474,6 +474,17 @@ void PipelineHandler::queueRequest(Request *request) > Camera::Private *data = camera->_d(); > data->waitingRequests_.push(request); > > + /* > + * If we are running into this assert, the calling application didn't > + * synchronize calls to Camera::queueRequest() and Camera::stop() and a > + * PipelineHandler::queueRequest() invokation was queued after > + * PipelineHandler::stop(). We can't guard against that in the Camera > + * class as it is lock free. Synchronization happens when the Camera > + * class invokes PipelineHandler::stop() in blocking mode. As such a > + * race leads to difficult to debug issues, assert here. > + */ > + ASSERT(data->isRunningOrStopping()); > + I /think/ this is useful. (hence also picking this from a development tree for this series). Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com> > request->_d()->prepare(300ms); > } > > > -- > 2.52.0 >
diff --git a/include/libcamera/internal/camera.h b/include/libcamera/internal/camera.h index 8af39047b4f5..f44b15c7da48 100644 --- a/include/libcamera/internal/camera.h +++ b/include/libcamera/internal/camera.h @@ -45,6 +45,8 @@ public: const CameraControlValidator *validator() const { return validator_.get(); } + bool isRunningOrStopping() const; + private: enum State { CameraAvailable, @@ -55,7 +57,6 @@ private: }; bool isAcquired() const; - bool isRunning() const; int isAccessAllowed(State state, bool allowDisconnected = false, const char *from = __builtin_FUNCTION()) const; int isAccessAllowed(State low, State high, @@ -64,6 +65,7 @@ private: void disconnect(); void setState(State state); + State state() const { return state_.load(std::memory_order_acquire); } std::shared_ptr<PipelineHandler> pipe_; std::string id_; diff --git a/src/libcamera/camera.cpp b/src/libcamera/camera.cpp index 2b179745aa4e..b6e2d3e51a46 100644 --- a/src/libcamera/camera.cpp +++ b/src/libcamera/camera.cpp @@ -682,9 +682,13 @@ bool Camera::Private::isAcquired() const return state_.load(std::memory_order_acquire) != CameraAvailable; } -bool Camera::Private::isRunning() const +bool Camera::Private::isRunningOrStopping() const { - return state_.load(std::memory_order_acquire) == CameraRunning; + State currentState = state_.load(std::memory_order_acquire); + if (currentState == CameraRunning || currentState == CameraStopping) + return true; + + return false; } int Camera::Private::isAccessAllowed(State state, bool allowDisconnected, @@ -1450,7 +1454,7 @@ int Camera::stop() * \todo Make calling stop() when not in 'Running' part of the state * machine rather than take this shortcut */ - if (!d->isRunning()) + if (d->state() != Private::CameraRunning) return 0; int ret = d->isAccessAllowed(Private::CameraRunning); diff --git a/src/libcamera/pipeline_handler.cpp b/src/libcamera/pipeline_handler.cpp index a803f6208c32..9613abc46dda 100644 --- a/src/libcamera/pipeline_handler.cpp +++ b/src/libcamera/pipeline_handler.cpp @@ -474,6 +474,17 @@ void PipelineHandler::queueRequest(Request *request) Camera::Private *data = camera->_d(); data->waitingRequests_.push(request); + /* + * If we are running into this assert, the calling application didn't + * synchronize calls to Camera::queueRequest() and Camera::stop() and a + * PipelineHandler::queueRequest() invokation was queued after + * PipelineHandler::stop(). We can't guard against that in the Camera + * class as it is lock free. Synchronization happens when the Camera + * class invokes PipelineHandler::stop() in blocking mode. As such a + * race leads to difficult to debug issues, assert here. + */ + ASSERT(data->isRunningOrStopping()); + request->_d()->prepare(300ms); }