RSC  0.16.0
Future.h
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is a part of the the RSC project.
4  *
5  * Copyright (C) 2011 by Johannes Wienke <jwienke at techfak dot uni-bielefeld dot de>
6  *
7  * This file may be licensed under the terms of the
8  * GNU Lesser General Public License Version 3 (the ``LGPL''),
9  * or (at your option) any later version.
10  *
11  * Software distributed under the License is distributed
12  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
13  * express or implied. See the LGPL for the specific language
14  * governing rights and limitations.
15  *
16  * You should have received a copy of the LGPL along with this
17  * program. If not, go to http://www.gnu.org/licenses/lgpl.html
18  * or write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  * The development of this software was supported by:
22  * CoR-Lab, Research Institute for Cognition and Robotics
23  * Bielefeld University
24  *
25  * ============================================================ */
26 
27 #pragma once
28 
29 #include <stdexcept>
30 
31 #include <boost/shared_ptr.hpp>
32 #include <boost/format.hpp>
33 #include <boost/thread/mutex.hpp>
34 #include <boost/thread/condition.hpp>
35 
36 #include "rsc/rscexports.h"
37 
38 namespace rsc {
39 namespace threading {
40 
49 class RSC_EXPORT FutureException: public std::runtime_error {
50 public:
51  explicit FutureException(const std::string& message);
52 };
53 
61 public:
62 
68  explicit FutureTaskExecutionException(const std::string& msg);
69 
70 };
71 
79 class RSC_EXPORT FutureTimeoutException: public FutureException {
80 public:
87  explicit FutureTimeoutException(const std::string& message);
88 };
89 
98 template<class R>
99 class Future {
100 protected:
101  typedef boost::mutex MutexType;
102  typedef boost::condition ConditionType;
103 private:
106  bool taskError;
107  std::string errorMsg;
108  ConditionType condition;
109  MutexType mutex;
110 public:
111 
116  Future() :
117  taskFinished(false), taskError(false) {
118  }
119 
131  R get() {
132  return get(0);
133  }
134 
153  R get(double timeout) {
154  MutexType::scoped_lock lock(mutex);
155  while (!taskFinished) {
156  if (timeout <= 0) {
157  condition.wait(lock);
158  } else {
159 #if BOOST_VERSION >= 105000
160  if (!condition.timed_wait(lock, boost::posix_time::microseconds(long(timeout) * 1000000))) {
161 #else
162  boost::xtime xt;
163  boost::xtime_get(&xt, boost::TIME_UTC);
164  xt.sec += timeout;
165  if (!condition.timed_wait(lock, xt)) {
166 #endif
168  boost::str(
169  boost::format(
170  "Timeout while waiting for result. Waited %1% seconds.")
171  % timeout));
172  }
173  }
174  }
175 
176  if (taskError) {
177  throw FutureTaskExecutionException(errorMsg);
178  }
179 
180  return result;
181  }
182 
190  bool isDone() {
191  MutexType::scoped_lock lock(mutex);
192  return taskFinished;
193  }
194 
200  void set(R data) {
201  result = data;
202  {
203  MutexType::scoped_lock lock(mutex);
204  taskFinished = true;
205  condition.notify_all();
206  }
207  }
208 
214  void setError(const std::string& message) {
215  errorMsg = message;
216  {
217  MutexType::scoped_lock lock(mutex);
218  taskFinished = true;
219  taskError = true;
220  }
221  condition.notify_all();
222  }
223 protected:
224  MutexType& getMutex() {
225  return this->mutex;
226  }
227 
228  ConditionType& getCondition() {
229  return this->condition;
230  }
231 };
232 
233 }
234 }
Thrown when the execution of a Future&#39;s task does not complete within the specified amount of time...
Definition: Future.h:79
bool isDone()
Tells whether the computation of the underlying process finished and provided a result or generated a...
Definition: Future.h:190
std::string errorMsg
Definition: Future.h:107
boost::condition ConditionType
Definition: Future.h:102
Thrown when the result of the computation represented by a future is not available for some reason...
Definition: Future.h:49
boost::mutex MutexType
Definition: Future.h:101
ConditionType & getCondition()
Definition: Future.h:228
Class providing access to the result of a process that is asynchronously running. ...
Definition: Future.h:99
MutexType & getMutex()
Definition: Future.h:224
Exception that is thrown if the result of a Future is not available because the underlying process ge...
Definition: Future.h:60
void setError(const std::string &message)
Indicate an error while processing.
Definition: Future.h:214
Future()
Create a new Future object that does not have a result and is thus suitable for representing an in-pr...
Definition: Future.h:116
ConditionType condition
Definition: Future.h:108