utils: ipc: proxy: Track IPA with a state machine

Asynchronous tasks can only be submitted while the IPA is running.

Further more, the shutdown sequence can not be tracked with a simple
running flag. We can also be in the state 'Stopping' where we have not
yet completed all events, but we must not commence anything new.

Refactor the running_ boolean into a stateful enum to track this.

Reviewed-by: Paul Elder <paul.elder@ideasonboard.com>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
This commit is contained in:
Kieran Bingham 2021-03-23 13:55:09 +00:00
parent e7d367bf65
commit 70238ceca5
5 changed files with 17 additions and 9 deletions

View file

@ -17,6 +17,12 @@ namespace libcamera {
class IPAModule; class IPAModule;
enum ProxyState {
ProxyStopped,
ProxyStopping,
ProxyRunning,
};
class IPAProxy : public IPAInterface class IPAProxy : public IPAInterface
{ {
public: public:
@ -31,6 +37,7 @@ protected:
std::string resolvePath(const std::string &file) const; std::string resolvePath(const std::string &file) const;
bool valid_; bool valid_;
ProxyState state_;
private: private:
IPAModule *ipam_; IPAModule *ipam_;

View file

@ -37,7 +37,7 @@ LOG_DEFINE_CATEGORY(IPAProxy)
* \param[in] ipam The IPA module * \param[in] ipam The IPA module
*/ */
IPAProxy::IPAProxy(IPAModule *ipam) IPAProxy::IPAProxy(IPAModule *ipam)
: valid_(false), ipam_(ipam) : valid_(false), state_(ProxyStopped), ipam_(ipam)
{ {
} }

View file

@ -45,8 +45,7 @@ namespace {{ns}} {
{%- endif %} {%- endif %}
{{proxy_name}}::{{proxy_name}}(IPAModule *ipam, bool isolate) {{proxy_name}}::{{proxy_name}}(IPAModule *ipam, bool isolate)
: IPAProxy(ipam), running_(false), : IPAProxy(ipam), isolate_(isolate), seq_(0)
isolate_(isolate), seq_(0)
{ {
LOG(IPAProxy, Debug) LOG(IPAProxy, Debug)
<< "initializing {{module_name}} proxy: loading IPA from " << "initializing {{module_name}} proxy: loading IPA from "
@ -155,7 +154,7 @@ void {{proxy_name}}::recvMessage(const IPCMessage &data)
return {{ "_ret" if method|method_return_value != "void" }}; return {{ "_ret" if method|method_return_value != "void" }};
{%- elif method.mojom_name == "start" %} {%- elif method.mojom_name == "start" %}
running_ = true; state_ = ProxyRunning;
thread_.start(); thread_.start();
{{ "return " if method|method_return_value != "void" -}} {{ "return " if method|method_return_value != "void" -}}
@ -173,7 +172,7 @@ void {{proxy_name}}::recvMessage(const IPCMessage &data)
{%- endfor -%} {%- endfor -%}
); );
{% elif method|is_async %} {% elif method|is_async %}
ASSERT(running_); ASSERT(state_ == ProxyRunning);
proxy_.invokeMethod(&ThreadProxy::{{method.mojom_name}}, ConnectionTypeQueued, proxy_.invokeMethod(&ThreadProxy::{{method.mojom_name}}, ConnectionTypeQueued,
{%- for param in method|method_param_names -%} {%- for param in method|method_param_names -%}
{{param}}{{- ", " if not loop.last}} {{param}}{{- ", " if not loop.last}}
@ -226,7 +225,7 @@ void {{proxy_name}}::recvMessage(const IPCMessage &data)
{% for method in interface_event.methods %} {% for method in interface_event.methods %}
{{proxy_funcs.func_sig(proxy_name, method, "Thread")}} {{proxy_funcs.func_sig(proxy_name, method, "Thread")}}
{ {
ASSERT(running_); ASSERT(state_ != ProxyStopped);
{{method.mojom_name}}.emit({{method.parameters|params_comma_sep}}); {{method.mojom_name}}.emit({{method.parameters|params_comma_sep}});
} }

View file

@ -104,7 +104,6 @@ private:
{{interface_name}} *ipa_; {{interface_name}} *ipa_;
}; };
bool running_;
Thread thread_; Thread thread_;
ThreadProxy proxy_; ThreadProxy proxy_;
std::unique_ptr<{{interface_name}}> ipa_; std::unique_ptr<{{interface_name}}> ipa_;

View file

@ -23,9 +23,12 @@
# \brief Generate function body for IPA stop() function for thread # \brief Generate function body for IPA stop() function for thread
#} #}
{%- macro stop_thread_body() -%} {%- macro stop_thread_body() -%}
if (!running_) ASSERT(state_ != ProxyStopping);
if (state_ != ProxyRunning)
return; return;
state_ = ProxyStopping;
proxy_.invokeMethod(&ThreadProxy::stop, ConnectionTypeBlocking); proxy_.invokeMethod(&ThreadProxy::stop, ConnectionTypeBlocking);
thread_.exit(); thread_.exit();
@ -33,7 +36,7 @@
Thread::current()->dispatchMessages(Message::Type::InvokeMessage); Thread::current()->dispatchMessages(Message::Type::InvokeMessage);
running_ = false; state_ = ProxyStopped;
{%- endmacro -%} {%- endmacro -%}