ArmarXObjectScheduler.h
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package ArmarXCore::core
19 * @author Kai Welke (kai dot welke at kit dot edu)
20 * @date 2012
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24
25#pragma once
26#include <condition_variable>
27#include <mutex>
28#include <string> // for string
29
30#include <Ice/Handle.h> // for Handle
31#include <Ice/ObjectAdapterF.h> // for ObjectAdapterPtr
32#include <Ice/ObjectF.h> // for upCast
33#include <IceUtil/Handle.h> // for Handle
34#include <IceUtil/Shared.h> // for Shared
35
36#include "ArmarXCore/core/IceManager.h" // for IceManager
37#include "ArmarXCore/core/ManagedIceObject.h" // for ManagedIceObject
38#include <ArmarXCore/core/logging/Logging.h> // for Logging
39#include <ArmarXCore/interface/core/ManagedIceObjectDefinitions.h>
40
41namespace armarx
42{
43 /**
44 * forward declarations
45 */
46 class ArmarXManager;
47
48 using ArmarXManagerPtr = IceUtil::Handle<ArmarXManager>;
49 using IceManagerPtr = IceUtil::Handle<IceManager>;
50 using ManagedIceObjectPtr = IceInternal::Handle<ManagedIceObject>;
51
52 /**
53 * ArmarXObjectScheduler shared pointer for convenience
54 */
56 template <class T>
57 class RunningTask;
58
59 using ArmarXObjectSchedulerPtr = IceUtil::Handle<ArmarXObjectScheduler>;
60
61 /**
62 * @class ArmarXObjectScheduler
63 * @brief Takes care of the lifecycle management of ManagedIceObjects.
64 * @ingroup DistributedProcessingSub
65 *
66 * The ArmarXObjectScheduler schedules the lifecycle of a ManagedIceObject in a single thread.
67 * According to the current state, the ManagedIceObject framework hooks are called.
68 * It provides all necessary functionality for a clean initialisation of ManagedIceObjects
69 * as well as dependency-resolution, dependency-monitoring, and exception handling.
70 * ArmarXObjectSchedulers are created by the ArmarXManager.
71
72 * @image html Lifecycle-UML.svg Lifecyle of a ManagedIceObject managed by the ArmarXObjectScheduler
73 */
74 class ArmarXObjectScheduler : public IceUtil::Shared, virtual public Logging
75 {
76 public:
77 /**
78 * Constructs an ArmarXObjectScheduler
79 *
80 * @param armarXManager pointer to the armarXManager
81 * @param iceManager pointer to the iceManager
82 * @param object object to schedule
83 */
84 ArmarXObjectScheduler(const ArmarXManagerPtr& armarXManager,
85 const IceManagerPtr& iceManager,
86 const armarx::ManagedIceObjectPtr& object,
87 Ice::ObjectAdapterPtr objectAdapterToAddTo,
88 bool startSchedulingObject = true);
89 ~ArmarXObjectScheduler() override;
90
91
92 /**
93 * waits until all depenencies are resolved.
94 * @param timeoutMs If set to -1, it waits indefinitely. Otherwise throws
95 * exception after waiting the specifed time (given in milliseconds).
96 */
97 void waitForDependencies(int timeoutMs = -1);
99 bool dependsOn(const std::string& objectName);
100 void disconnected(bool reconnect);
101
102 /**
103 * Waits until scheduler has been terminated. After termination the lifecycle of the ManagedIceObject
104 * has ceased and the thread has joined.
105 */
106 void waitForTermination();
107
108
109 /**
110 * @brief waitForObjectStart waits (thread sleeps) until the object
111 * reached a specific state.
112 * @param stateToWaitFor State for which the calling thread should wait
113 * for.
114 * @param timeoutMs Timeout in miliseconds until this function stops
115 * waiting and returns false. Set to -1 for infinite waiting.
116 * @return Whether the object reached the state before timeout or not.
117 * @see waitForObjectStateMinimum()
118 */
119 bool waitForObjectState(ManagedIceObjectState stateToWaitFor,
120 const long timeoutMs = -1) const;
121
122 /**
123 * @brief waitForObjectStart waits (thread sleeps) until the object
124 * reached a specific state (or higher/later).
125 * @param minimumStateToWaitFor Minimum State for which the calling thread should wait
126 * for.
127 * @param timeoutMs Timeout in miliseconds until this function stops
128 * waiting and returns false. Set to -1 for infinite waiting.
129 * @return Whether the object reached the minimum state before timeout
130 * or not.
131 * @see waitForObjectState()
132 */
133 bool waitForObjectStateMinimum(ManagedIceObjectState minimumStateToWaitFor,
134 const long timeoutMs = -1) const;
135
136 /**
137 * Terminates the ManagedIceObject.
138 */
139 void terminate();
140
141 /**
142 * Check whether the Scheduler is terminated
143 *
144 * @return terminated state
145 */
146 bool isTerminated() const;
147
148 bool isTerminationRequested() const;
149
150 /**
151 * Retrieve pointer to scheduled ManagedIceObject
152 *
153 * @return ManagedIceObjectPtr
154 */
156 ManagedIceObjectState getObjectState() const;
157
158
159 bool checkDependenciesStatus() const;
160 void startScheduling();
161
162 protected:
164
165 private:
166 void
167 setInteruptConditionVariable(std::shared_ptr<std::condition_variable> interruptCondition,
168 std::shared_ptr<bool> interruptConditionVariable);
169 // main scheduling thread functions
170 void scheduleObject();
171
172 void waitForInterrupt();
173
174
175 // scheduling thread
177
178
179 // phases of the object lifecycle
180 void initObject();
181 void startObject();
182 void disconnectObject();
183 void exitObject();
184
185 // private fields
186 ArmarXManagerPtr armarXManager;
187 IceManagerPtr iceManager;
188 ManagedIceObjectPtr managedObject;
189
190 // termination handling
191 // boost::mutex terminateMutex;
192 // boost::condition_variable terminateCondition;
193 // bool terminateConditionVariable;
194 std::mutex interruptMutex;
195 std::shared_ptr<std::condition_variable> interruptCondition;
196 std::shared_ptr<bool> interruptConditionVariable;
197 std::mutex dependencyWaitMutex;
198 std::condition_variable dependencyWaitCondition;
199 bool dependencyWaitConditionVariable;
200 bool terminateRequested;
201 bool objectedInitialized;
202 bool tryReconnect;
203 Ice::ObjectAdapterPtr objectAdapterToAddTo;
204
206 };
207} // namespace armarx
Main class of an ArmarX process.
Takes care of the lifecycle management of ManagedIceObjects.
ManagedIceObjectState getObjectState() const
bool dependsOn(const std::string &objectName)
void waitForDependencies(int timeoutMs=-1)
waits until all depenencies are resolved.
void terminate()
Terminates the ManagedIceObject.
ArmarXObjectScheduler(const ArmarXManagerPtr &armarXManager, const IceManagerPtr &iceManager, const armarx::ManagedIceObjectPtr &object, Ice::ObjectAdapterPtr objectAdapterToAddTo, bool startSchedulingObject=true)
Constructs an ArmarXObjectScheduler.
const armarx::ManagedIceObjectPtr & getObject() const
Retrieve pointer to scheduled ManagedIceObject.
bool isTerminated() const
Check whether the Scheduler is terminated.
bool waitForObjectState(ManagedIceObjectState stateToWaitFor, const long timeoutMs=-1) const
waitForObjectStart waits (thread sleeps) until the object reached a specific state.
void waitForTermination()
Waits until scheduler has been terminated.
bool waitForObjectStateMinimum(ManagedIceObjectState minimumStateToWaitFor, const long timeoutMs=-1) const
waitForObjectStart waits (thread sleeps) until the object reached a specific state (or higher/later).
::IceInternal::Handle<::Ice::ObjectAdapter > ObjectAdapterPtr
Definition IceManager.h:52
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceUtil::Handle< ArmarXManager > ArmarXManagerPtr
IceUtil::Handle< ArmarXObjectScheduler > ArmarXObjectSchedulerPtr
Definition ArmarXFwd.h:33
IceUtil::Handle< IceManager > IceManagerPtr
IceManager smart pointer.
Definition ArmarXFwd.h:39
IceInternal::Handle< ManagedIceObject > ManagedIceObjectPtr
Definition ArmarXFwd.h:42