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