SMACC2
Loading...
Searching...
No Matches
smacc_asynchronous_client_behavior.hpp
Go to the documentation of this file.
1// Copyright 2025 Robosoft Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15/*****************************************************************************************************************
16 *
17 * Authors: Pablo Inigo Blasco, Brett Aldrich
18 *
19 ******************************************************************************************************************/
20
21#pragma once
22#include <atomic>
23#include <future>
26
27namespace smacc2
28{
29template <typename AsyncCB, typename Orthogonal>
30struct EvCbFinished : sc::event<EvCbFinished<AsyncCB, Orthogonal>>
31{
32};
33
34template <typename AsyncCB, typename Orthogonal>
35struct EvCbSuccess : sc::event<EvCbSuccess<AsyncCB, Orthogonal>>
36{
37};
38
39template <typename AsyncCB, typename Orthogonal>
40struct EvCbFailure : sc::event<EvCbFailure<AsyncCB, Orthogonal>>
41{
42};
43
44// INTRODUCTION: Conceptually, AsynchronousClientBehaviors start in parallel on state entry.
45// Asnchronous client behaviors are used when the onEntry or onExit function execution could be too much slow and
46// could block the state machine thread.
47// AsynchronousClientBehaviors are related with the concept orthogonality of Smacc State Machines.
48// ASYNCHRONOUS STATE MACHINES DESIGN NOTES: Asynchronous behaviors can safely post events and use its local methods,
49// but the interaction with other components or elements of
50// the state machine is not by-default thread safe and must be manually implemented. For example, if some element of the architecture
51// (components, states, clients) need to access to this behavior client information it is needed to implement a mutex for the internal
52// state of this behavior. Other example: if this behavior access to some component located in other thread, it is also may be needed
53// to some mutex for that component
54// ALTERNATIVE: for long duration behaviors: using default-synchromous SmaccClientBehaviors with the update method
55
57{
58public:
59 template <typename TOrthogonal, typename TSourceObject>
61
64
65 // Not copyable or movable. This turns an accidental copy, such as a missing & in
66 // "auto & cb = *behavior;", into a compile error instead of calls on a detached copy.
71
72 template <typename TCallback, typename T>
73 smacc2::SmaccSignalConnection onSuccess(TCallback callback, T * object);
74
75 template <typename TCallback, typename T>
76 smacc2::SmaccSignalConnection onFinished(TCallback callback, T * object);
77
78 template <typename TCallback, typename T>
79 smacc2::SmaccSignalConnection onFailure(TCallback callback, T * object);
80
81 void requestForceFinish();
82
83 // executes onExit in a new thread
84 void executeOnEntry() override;
85
86 // executes onExit in a new thread, waits first onEntry thread if it is still running
87 void executeOnExit() override;
88
89 // Joins the onEntry thread if it is live, optionally asking it to finish first. Does not
90 // give up at rclcpp shutdown; see waitFutureIfNotFinished.
91 void waitOnEntryThread(bool requestFinish);
92
93protected:
94 void postSuccessEvent();
95 void postFailureEvent();
96
97 void dispose() override;
98
101 // All asyncrhonous client behaviors should implement the ability of interrupting the onEntry thread.
102 // to avoid blocking the state machine.
103
104 bool isShutdownRequested();
105
106private:
107 void waitFutureIfNotFinished(std::optional<std::future<int>> & threadfut, bool requestFinish);
108
109 std::optional<std::future<int>> onEntryThread_;
110 std::optional<std::future<int>> onExitThread_;
111
112 std::function<void()> postFinishEventFn_;
113 std::function<void()> postSuccessEventFn_;
114 std::function<void()> postFailureEventFn_;
115
119
120 // Written by requestForceFinish() on the state machine thread, read by onEntry/onExit on
121 // the behavior's std::async thread. Atomic so the write is visible across threads and the
122 // compiler cannot hoist the read out of a behavior's wait loop.
123 std::atomic<bool> isShutdownRequested_{false};
124};
125} // namespace smacc2
126
smacc2::SmaccSignalConnection onFailure(TCallback callback, T *object)
bool isShutdownRequested()
onEntry is executed in a new thread. However the current state cannot be left until the onEntry threa...
SmaccAsyncClientBehavior & operator=(SmaccAsyncClientBehavior &&)=delete
std::optional< std::future< int > > onExitThread_
std::optional< std::future< int > > onEntryThread_
void waitFutureIfNotFinished(std::optional< std::future< int > > &threadfut, bool requestFinish)
smacc2::SmaccSignalConnection onSuccess(TCallback callback, T *object)
SmaccAsyncClientBehavior(const SmaccAsyncClientBehavior &)=delete
smacc2::SmaccSignalConnection onFinished(TCallback callback, T *object)
SmaccAsyncClientBehavior(SmaccAsyncClientBehavior &&)=delete
SmaccAsyncClientBehavior & operator=(const SmaccAsyncClientBehavior &)=delete
boost::signals2::connection SmaccSignalConnection