24using namespace std::chrono_literals;
42 TRACETOOLS_TRACEPOINT(
44 names_.orthogonal.c_str(), names_.behavior.c_str());
48 TRACETOOLS_TRACEPOINT(
50 names_.behavior.c_str());
57 TRACETOOLS_TRACEPOINT(
59 names_.behavior.c_str());
63 TRACETOOLS_TRACEPOINT(
65 names_.behavior.c_str());
76 const rclcpp::Logger & logger,
const std::string & name,
const char * thread,
const char * what)
81 logger,
"[" << name <<
"] " << thread <<
" thread ended by exception: " << what);
86 logger,
"[" << name <<
"] " << thread <<
" thread ended by exception at shutdown: " << what);
92 RCLCPP_INFO_STREAM(
getLogger(),
"[" <<
getName() <<
"] asynchronous onEntry thread started");
102 AsyncTraceScope trace(
true, names);
105 catch (
const std::exception & e)
118 RCLCPP_INFO_STREAM(
getLogger(),
"[" <<
getName() <<
"] asynchronous onEntry thread finished");
124 std::optional<std::future<int>> & threadfut,
bool requestFinish)
126 if (!threadfut || !threadfut->valid())
130 "[" <<
getName() <<
"] onEntry thread not created or already joined. Skipping wait.");
141 if (threadfut->wait_for(std::chrono::seconds(0)) == std::future_status::ready)
147 if (requestFinish && !rclcpp::ok())
152 auto status = threadfut->wait_for(std::chrono::milliseconds(20));
153 if (status == std::future_status::ready)
162 <<
"] fut valid but waiting for asynchronous onEntry thread to finish: state: "
165 rclcpp::sleep_for(100ms);
166 RCLCPP_WARN_THROTTLE(
168 "[%s] waiting for finishing client behavior, before leaving the state. Is the client "
169 "behavior stuck? requesting force finish",
175 catch (
const std::exception & e)
192 RCLCPP_INFO_STREAM(
getLogger(),
"[" <<
getName() <<
"] onExit - join async onEntry thread");
196 getLogger(),
"[" <<
getName() <<
"] onExit - Creating asynchronous onExit thread");
203 AsyncTraceScope trace(
false, names);
206 RCLCPP_INFO_STREAM(
getLogger(),
"[" <<
getName() <<
"] asynchronous onExit done.");
215 <<
"] Destroying client behavior- Waiting finishing of asynchronous onExit "
224 catch (
const std::exception & e)
238 <<
"] Destroying client behavior- onExit thread finished. Proccedding "
249 getLogger(),
"[" <<
getName() <<
"] destroyed with a live onEntry thread; joining it");
255 getLogger(),
"[" <<
getName() <<
"] destroyed with a live onExit thread; joining it");
260 catch (
const std::exception & e)
262 RCLCPP_ERROR_STREAM(
getLogger(),
"[" <<
getName() <<
"] onExit thread threw: " << e.what());
267 getLogger(),
"[" <<
getName() <<
"] onExit thread threw a non-standard exception");
278 RCLCPP_FATAL_STREAM_THROTTLE(
280 "[" <<
getName() <<
"] " <<
static_cast<const void *
>(
this) <<
" requestForceFinish");
std::string getName() const
virtual rclcpp::Logger getLogger() const
void postInitializationFailureEvent(const std::string &what)
virtual rclcpp::Node::SharedPtr getNode() const
std::string getTraceStateName() const
std::string getTraceOrthogonalName() const
std::function< void()> postSuccessEventFn_
~SmaccAsyncClientBehavior() override
std::function< void()> postFinishEventFn_
void executeOnEntry() override
bool isShutdownRequested()
onEntry is executed in a new thread. However the current state cannot be left until the onEntry threa...
std::optional< std::future< int > > onExitThread_
std::optional< std::future< int > > onEntryThread_
void waitFutureIfNotFinished(std::optional< std::future< int > > &threadfut, bool requestFinish)
std::atomic< bool > isShutdownRequested_
void requestForceFinish()
void executeOnExit() override
std::function< void()> postFailureEventFn_
void waitOnEntryThread(bool requestFinish)
static void logThreadException(const rclcpp::Logger &logger, const std::string &name, const char *thread, const char *what)
const AsyncTraceNames & names_
AsyncTraceScope(bool entry, const AsyncTraceNames &names)
smacc2_client_behavior_async_on_entry_start
smacc2_client_behavior_async_on_exit_start
smacc2_client_behavior_async_on_entry_end
smacc2_client_behavior_async_on_exit_end