ArmarXMultipleObjectsScheduler.cpp
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2011-2017, 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 ArmarX
19 * @author Mirko Waechter( mirko.waechter at kit dot edu)
20 * @date 2017
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
25
26#include <memory>
27
29
30namespace armarx
31{
32
34 {
35 interruptCondition = std::make_shared<std::condition_variable>();
36 interruptConditionVariable = std::make_shared<bool>();
37 *interruptConditionVariable = false;
38 scheduleObjectsTask = new RunningTask<ArmarXMultipleObjectsScheduler>(
39 this, &ArmarXMultipleObjectsScheduler::schedule, "ArmarXMultipleObjectsScheduler");
40 scheduleObjectsTask->start();
41 }
42
44 {
45 scheduleObjectsTask->stop(false);
46 interruptCondition->notify_all();
47 scheduleObjectsTask->waitForFinished();
48 }
49
50 bool
52 {
53 bool result = false;
54 bool found = false;
55 {
56 std::scoped_lock lock(dataMutex);
57 for (size_t j = 0; j < schedulers.size();
58 ++j) // std::find does not work for IceUtil::Handle?!....
59 {
60 for (size_t i = j + 1; i < schedulers.size(); ++i)
61 {
62 if (schedulers.at(j).get() == schedulers.at(i).get())
63 {
64 found = true;
65 break;
66 }
67 }
68 if (found)
69 {
70 break;
71 }
72 }
73 if (!found)
74 {
75 schedulers.push_back(scheduler);
76 scheduler->setInteruptConditionVariable(interruptCondition,
77 interruptConditionVariable);
78 result = true;
79 }
80 }
81 if (!found)
82 {
83 std::scoped_lock lock(interruptMutex);
84 *interruptConditionVariable = true;
85 interruptCondition->notify_all();
86 }
87 return result;
88 }
89
90 void
91 ArmarXMultipleObjectsScheduler::schedule()
92 {
93 while (!scheduleObjectsTask->isStopped())
94 {
95 {
96 // ARMARX_INFO << "Checking states";
97 std::vector<ArmarXObjectSchedulerPtr> tempSchedulers;
98 {
99 std::scoped_lock lock(dataMutex);
100 tempSchedulers = schedulers;
101 }
102 std::vector<ArmarXObjectSchedulerPtr> schedulersToErase;
103 int i = 0;
104 for (ArmarXObjectSchedulerPtr& sched : tempSchedulers)
105 {
106 auto state = sched->getObjectState();
107 // ARMARX_INFO << "Checking " << sched->getObject()->getName() << " state: " << sched->getObject()->getState();
108
109 switch (state)
110 {
111 case eManagedIceObjectCreated:
112 sched->initObject();
113 if (sched->checkDependenciesResolvement())
114 {
115 sched->startObject();
116 }
117
118 break;
119 case eManagedIceObjectInitializing:
120 break;
121 case eManagedIceObjectInitialized:
122 if (sched->isTerminationRequested())
123 {
124 sched->exitObject();
125 schedulersToErase.push_back(sched);
126 }
127 else if (sched->checkDependenciesResolvement())
128 {
129 sched->startObject();
130 }
131 break;
132 case eManagedIceObjectStarting:
133 break;
134 case eManagedIceObjectStarted:
135 if (sched->isTerminationRequested())
136 {
137 sched->disconnectObject();
138 sched->exitObject();
139 schedulersToErase.push_back(sched);
140 }
141 else if (!sched->checkDependenciesStatus())
142 {
143 sched->disconnectObject();
144 }
145 break;
146
147 case eManagedIceObjectExiting:
148 break;
149 case eManagedIceObjectExited:
150 break;
151 default:
152 break;
153 }
154 i++;
155 }
156 std::scoped_lock lock(dataMutex);
157 std::reverse(schedulersToErase.begin(), schedulersToErase.end());
158 for (auto sched : schedulersToErase)
159 {
160
161 // std::remove_if(schedulers.begin(), schedulers.end(), [](const ArmarXObjectSchedulerPtr& lhs, const ArmarXObjectSchedulerPtr& rhs){ return lhs.get() == rhs.get();});// std::remove does not work
162
163 for (auto it = schedulers.begin(); it != schedulers.end(); it++)
164 {
165 if (sched.get() == it->get())
166 {
167 schedulers.erase(it);
168 break;
169 }
170 }
171 }
172 }
173 std::unique_lock lock(interruptMutex);
174
175 *interruptConditionVariable = false;
176
177 while (!*interruptConditionVariable && !scheduleObjectsTask->isStopped())
178 {
179 if (interruptCondition->wait_for(lock, std::chrono::milliseconds(2000)) ==
180 std::cv_status::timeout)
181 {
182 // ARMARX_INFO << "Timed out";
183 break;
184 }
185 }
186 }
187 }
188
189} // namespace armarx
bool addObjectScheduler(const ArmarXObjectSchedulerPtr &scheduler)
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceUtil::Handle< ArmarXObjectScheduler > ArmarXObjectSchedulerPtr
Definition ArmarXFwd.h:33