SMACC2
smacc_asynchronous_client_behavior.hpp
Go to the documentation of this file.
1// Copyright 2021 RobosoftAI 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 <condition_variable>
23#include <future>
24#include <mutex>
27#include <thread>
28
29namespace smacc2
30{
31template <typename AsyncCB, typename Orthogonal>
32struct EvCbFinished : sc::event<EvCbFinished<AsyncCB, Orthogonal>>
33{
34};
35
36template <typename AsyncCB, typename Orthogonal>
37struct EvCbSuccess : sc::event<EvCbSuccess<AsyncCB, Orthogonal>>
38{
39};
40
41template <typename AsyncCB, typename Orthogonal>
42struct EvCbFailure : sc::event<EvCbFailure<AsyncCB, Orthogonal>>
43{
44};
45
46// INTRODUCTION: All of them conceptually start in parallel when the state starts. No behavior should block the creation of other behaviors,
47// Asnchronous client behaviors are used when the onEntry or onExit function execution is slow
48// CONCEPT: this funcionality is related with the orthogonality of SmaccState machines.
49// Alternative for long duration behaviors: using default-synchromous SmaccClientBehaviors with the update method
50// ASYNCHRONOUS STATE MACHINES DESIGN NOTES: Asynchronous behaviors can safely post events and use its local methods,
51// but the interaction with other components or elements of
52// the state machine is not by-default thread safe and must be manually implemented. For example, if some element of the architecture
53// (components, states, clients) need to access to this behavior client information it is needed to implement a mutex for the internal
54// state of this behavior. Other example: if this behavior access to some component located in other thread, it is also may be needed
55// to some mutex for that component
57{
58public:
59 template <typename TOrthogonal, typename TSourceObject>
61
63
64 template <typename TCallback, typename T>
65 boost::signals2::connection onSuccess(TCallback callback, T * object);
66
67 template <typename TCallback, typename T>
68 boost::signals2::connection onFinished(TCallback callback, T * object);
69
70 template <typename TCallback, typename T>
71 boost::signals2::connection onFailure(TCallback callback, T * object);
72
73protected:
74 void postSuccessEvent();
75 void postFailureEvent();
76
77 virtual void dispose() override;
78
79 inline bool isShutdownRequested() { return isShutdownRequested_; }
80
81private:
82 void waitFutureIfNotFinished(std::future<int> & threadfut);
83 std::future<int> onEntryThread_;
84 std::future<int> onExitThread_;
85
86 std::function<void()> postFinishEventFn_;
87 std::function<void()> postSuccessEventFn_;
88 std::function<void()> postFailureEventFn_;
89
93
94 // executes onExit in a new thread
95 void executeOnEntry() override;
96
97 // executes onExit in a new thread, waits first onEntry thread if it is still running
98 void executeOnExit() override;
99
101};
102} // namespace smacc2
103
void waitFutureIfNotFinished(std::future< int > &threadfut)
boost::signals2::connection onFinished(TCallback callback, T *object)
boost::signals2::connection onSuccess(TCallback callback, T *object)
boost::signals2::connection onFailure(TCallback callback, T *object)
void callback(const image_tools::ROSCvMatContainer &img)