Robot Raconteur Core C++ Library
Loading...
Searching...
No Matches
AsyncUtils.h
Go to the documentation of this file.
1
23
24#include <boost/bind/placeholders.hpp>
25#include <boost/scope_exit.hpp>
26#include <boost/system/error_code.hpp>
27#include <boost/bind/bind.hpp>
28
34
35#pragma once
36
37namespace RobotRaconteur
38{
39class ROBOTRACONTEUR_CORE_API RobotRaconteurNode;
40
41namespace detail
42{
43template <typename T>
44class sync_async_handler : private boost::noncopyable
45{
46 public:
47 RR_SHARED_PTR<AutoResetEvent> ev;
48 RR_SHARED_PTR<RobotRaconteurException> err;
49 RR_SHARED_PTR<T> data;
50 boost::mutex data_lock;
51
52 sync_async_handler() { ev = RR_MAKE_SHARED<AutoResetEvent>(); }
53
54 sync_async_handler(const RR_SHARED_PTR<RobotRaconteurException>& err)
55 {
56 ev = RR_MAKE_SHARED<AutoResetEvent>();
57 this->err = err;
58 }
59
60 void operator()() { ev->Set(); }
61
62 void operator()(const RR_SHARED_PTR<RobotRaconteurException>& err)
63 {
64 boost::mutex::scoped_lock lock(data_lock);
65 this->err = err;
66 ev->Set();
67 }
68
69 void operator()(const RR_SHARED_PTR<T>& data, const RR_SHARED_PTR<RobotRaconteurException>& err)
70 {
71 boost::mutex::scoped_lock lock(data_lock);
72 this->err = err;
73 this->data = data;
74 ev->Set();
75 }
76
77 RR_SHARED_PTR<T> end()
78 {
79 ev->WaitOne();
80
81 boost::mutex::scoped_lock lock(data_lock);
82 if (err)
83 {
84 RobotRaconteurExceptionUtil::DownCastAndThrowException(err);
85 }
86
87 if (!data)
88 throw InternalErrorException("Internal async error");
89
90 return data;
91 }
92
93 void end_void()
94 {
95 ev->WaitOne();
96
97 boost::mutex::scoped_lock lock(data_lock);
98 if (err)
99 {
100 RobotRaconteurExceptionUtil::DownCastAndThrowException(err);
101 }
102 }
103
104 bool try_end(RR_SHARED_PTR<T>& res, RR_SHARED_PTR<RobotRaconteurException>& err_out)
105 {
106 ev->WaitOne();
107
108 boost::mutex::scoped_lock lock(data_lock);
109 if (err)
110 {
111 err_out = err;
112 return false;
113 }
114
115 if (!data)
116 {
117 err_out = RR_MAKE_SHARED<InternalErrorException>("Internal async error");
118 return false;
119 }
120
121 res = data;
122 return true;
123 }
124
125 bool try_end_void(RR_SHARED_PTR<RobotRaconteurException>& err_out)
126 {
127 ev->WaitOne();
128
129 boost::mutex::scoped_lock lock(data_lock);
130 if (err)
131 {
132 err_out = err;
133 return false;
134 }
135
136 return true;
137 }
138};
139
140RR_SHARED_PTR<Timer> async_timeout_wrapper_CreateTimer(const RR_SHARED_PTR<RobotRaconteurNode>& node,
141 const boost::posix_time::time_duration& period,
142 RR_MOVE_ARG(boost::function<void(const TimerEvent&)>) handler,
143 bool oneshot);
144
145template <typename T>
146void async_timeout_wrapper_closer(const RR_SHARED_PTR<T>& d)
147{
148 try
149 {
150 d->Close();
151 }
152 catch (std::exception&)
153 {}
154}
155
156template <typename T, typename T2>
157void async_timeout_wrapper_closer(const RR_SHARED_PTR<T>& d)
158{
159 try
160 {
161 RR_SHARED_PTR<T2> t2 = RR_DYNAMIC_POINTER_CAST<T2>(d);
162 if (!t2)
163 return;
164 t2->Close();
165 }
166 catch (std::exception&)
167 {}
168}
169
170template <typename T>
171class async_timeout_wrapper : public RR_ENABLE_SHARED_FROM_THIS<async_timeout_wrapper<T> >, private boost::noncopyable
172{
173 private:
174 boost::function<void(const RR_SHARED_PTR<T>&, const RR_SHARED_PTR<RobotRaconteurException>&)> handler_;
175 RR_SHARED_PTR<Timer> timeout_timer_;
176 boost::mutex handled_lock;
177 bool handled;
178 RR_SHARED_PTR<RobotRaconteurException> timeout_exception_;
179 boost::function<void(const RR_SHARED_PTR<T>&)> deleter_;
180 RR_WEAK_PTR<RobotRaconteurNode> node;
181
182 public:
183 async_timeout_wrapper(
184 const RR_SHARED_PTR<RobotRaconteurNode>& node,
185 boost::function<void(const RR_SHARED_PTR<T>&, const RR_SHARED_PTR<RobotRaconteurException>&)> handler,
186 RR_MOVE_ARG(boost::function<void(const RR_SHARED_PTR<T>&)>) deleter = 0)
187 : handler_(handler), handled(false), node(node)
188 {
189 deleter_.swap(deleter);
190 }
191
192 void start_timer(int32_t timeout, const RR_SHARED_PTR<RobotRaconteurException>& timeout_exception =
193 RR_MAKE_SHARED<ConnectionException>("Timeout during operation"));
194
195 void operator()(const RR_SHARED_PTR<T>& data, const RR_SHARED_PTR<RobotRaconteurException>& err)
196 {
197 {
198 boost::mutex::scoped_lock lock(handled_lock);
199 if (handled)
200 {
201 if (data && deleter_)
202 deleter_(data);
203 return;
204 }
205 handled = true;
206
207 if (timeout_timer_)
208 timeout_timer_->TryStop();
209
210 timeout_timer_.reset();
211 }
212
213 handler_(data, err);
214 }
215
216 void handle_error(const RR_SHARED_PTR<RobotRaconteurException>& err)
217 {
218 {
219 boost::mutex::scoped_lock lock(handled_lock);
220 if (handled)
221 return;
222 handled = true;
223
224 {
225
226 if (timeout_timer_)
227 timeout_timer_->TryStop();
228
229 timeout_timer_.reset();
230 }
231 }
232
233 handler_(RR_SHARED_PTR<T>(), err);
234 }
235
236 void handle_error(const boost::system::error_code& err)
237 {
238 if (err.value() == boost::system::errc::timed_out)
239 handle_error(timeout_exception_);
240 else
241 handle_error(RR_MAKE_SHARED<ConnectionException>(err.message()));
242 }
243
244 private:
245 void timeout_handler(const TimerEvent& /*e*/)
246 {
247 {
248 boost::mutex::scoped_lock lock(handled_lock);
249 if (handled)
250 return;
251 handled = true;
252
253 timeout_timer_.reset();
254 }
255
256 handler_(RR_SHARED_PTR<T>(), timeout_exception_);
257 }
258};
259
260template <typename T>
261void async_timeout_wrapper<T>::start_timer(int32_t timeout,
262 const RR_SHARED_PTR<RobotRaconteurException>& timeout_exception)
263{
264 RR_SHARED_PTR<RobotRaconteurNode> n = node.lock();
265 if (!n)
266 throw InvalidOperationException("Node has been released");
267
268 boost::mutex::scoped_lock lock(handled_lock);
269
270 if (handled)
271 return;
272
273 if (timeout != RR_TIMEOUT_INFINITE)
274 {
275 timeout_exception_ = RR_MOVE(timeout_exception);
276 timeout_timer_ =
277 async_timeout_wrapper_CreateTimer(n, boost::posix_time::milliseconds(timeout),
278 boost::bind(&async_timeout_wrapper<T>::timeout_handler,
279 this->shared_from_this(), RR_BOOST_PLACEHOLDERS(_1)),
280 true);
281 timeout_timer_->Start();
282 }
283}
284
285class ROBOTRACONTEUR_CORE_API async_signal_semaphore : private boost::noncopyable
286{
287 protected:
288 boost::mutex this_lock;
289 boost::condition_variable next_wait;
290 boost::initialized<bool> running;
291 boost::initialized<bool> next;
292 boost::initialized<uint64_t> next_id;
293
294 public:
295 template <typename F>
296 bool try_fire_next(BOOST_ASIO_MOVE_ARG(F) h)
297 {
298 boost::mutex::scoped_lock lock(this_lock);
299
300 if (running || next)
301 {
302 uint64_t my_id = ++next_id.data();
303
304 if (next.data())
305 {
306 next_wait.notify_all();
307 }
308 else
309 {
310 next.data() = true;
311 }
312
313 while (running)
314 {
315 next_wait.wait(lock);
316 if (my_id != next_id)
317 return false;
318 }
319
320 next.data() = false;
321 }
322
323 running.data() = true;
324
325 BOOST_SCOPE_EXIT_TPL(this_)
326 {
327 boost::mutex::scoped_lock lock2(this_->this_lock);
328 this_->running.data() = false;
329 this_->next_wait.notify_all();
330 }
331 BOOST_SCOPE_EXIT_END;
332
333 lock.unlock();
334
335 h();
336
337 return true;
338 }
339};
340
341class ROBOTRACONTEUR_CORE_API async_signal_pool_semaphore
342 : public RR_ENABLE_SHARED_FROM_THIS<async_signal_pool_semaphore>,
343 private boost::noncopyable
344{
345 protected:
346 boost::mutex this_lock;
347 boost::initialized<bool> running;
348 boost::function<void()> next;
349 RR_WEAK_PTR<RobotRaconteurNode> node;
350
351 public:
352 async_signal_pool_semaphore(const RR_SHARED_PTR<RobotRaconteurNode>& node) : node(node) {}
353
354 template <typename F>
355 void try_fire_next(BOOST_ASIO_MOVE_ARG(F) h)
356 {
357 boost::mutex::scoped_lock lock(this_lock);
358
359 if (!running)
360 {
361 RR_SHARED_PTR<RobotRaconteurNode> node1 = this->node.lock();
362 if (!node1)
363 return;
364 do_post(node1, h);
365 }
366 else
367 {
368 next = h;
369 }
370 }
371
372 protected:
373 void do_fire_next(const boost::function<void()>& h)
374 {
375 try
376 {
377 h();
378 }
379 catch (std::exception& exp)
380 {
381 handle_exception(&exp);
382 }
383
384 BOOST_SCOPE_EXIT(this_)
385 {
386 boost::mutex::scoped_lock lock2(this_->this_lock);
387
388 boost::function<void()> h2;
389 h2.swap(this_->next);
390 this_->next.clear();
391 this_->running.data() = false;
392 if (!h2)
393 return;
394 RR_SHARED_PTR<RobotRaconteurNode> node = this_->node.lock();
395 if (!node)
396 return;
397 try
398 {
399 this_->do_post(node,
400 boost::bind(&async_signal_pool_semaphore::do_fire_next, this_->shared_from_this(), h2));
401 }
402 catch (std::exception&)
403 {}
404 this_->running.data() = true;
405 }
406 BOOST_SCOPE_EXIT_END;
407 }
408
409 void do_post(const RR_SHARED_PTR<RobotRaconteurNode>& node1, RR_MOVE_ARG(boost::function<void()>) h);
410
411 void handle_exception(std::exception* exp);
412};
413
414ROBOTRACONTEUR_CORE_API void InvokeHandler_HandleException(RR_WEAK_PTR<RobotRaconteurNode> node, std::exception& exp);
415
416ROBOTRACONTEUR_CORE_API void InvokeHandler_DoPost(RR_WEAK_PTR<RobotRaconteurNode> node,
417 const boost::function<void()>& h, bool shutdown_op = false,
418 bool throw_on_released = true);
419
420ROBOTRACONTEUR_CORE_API void InvokeHandler(RR_WEAK_PTR<RobotRaconteurNode> node,
421 const boost::function<void()>& handler);
422
423template <typename T>
424void InvokeHandler(RR_WEAK_PTR<RobotRaconteurNode> node, const typename boost::function<void(const T&)>& handler,
425 const T& value)
426{
427 try
428 {
429 handler(value);
430 }
431 catch (std::exception& exp)
432 {
433 InvokeHandler_HandleException(RR_MOVE(node), exp);
434 }
435}
436
437ROBOTRACONTEUR_CORE_API void InvokeHandler(
438 RR_WEAK_PTR<RobotRaconteurNode> node,
439 const boost::function<void(const RR_SHARED_PTR<RobotRaconteurException>&)>& handler);
440
441ROBOTRACONTEUR_CORE_API void InvokeHandlerWithException(
442 RR_WEAK_PTR<RobotRaconteurNode> node,
443 const boost::function<void(const RR_SHARED_PTR<RobotRaconteurException>&)>& handler,
444 const RR_SHARED_PTR<RobotRaconteurException>& exp);
445
446ROBOTRACONTEUR_CORE_API void InvokeHandlerWithException(
447 RR_WEAK_PTR<RobotRaconteurNode> node,
448 const boost::function<void(const RR_SHARED_PTR<RobotRaconteurException>&)>& handler, std::exception& exp,
449 MessageErrorType default_err = MessageErrorType_UnknownError);
450
451template <typename T>
452void InvokeHandler(
453 RR_WEAK_PTR<RobotRaconteurNode> node,
454 const typename boost::function<void(const T&, const RR_SHARED_PTR<RobotRaconteurException>&)>& handler,
455 const T& value)
456{
457 try
458 {
459 handler(value, RR_SHARED_PTR<RobotRaconteurException>());
460 }
461 catch (std::exception& exp)
462 {
463 InvokeHandler_HandleException(RR_MOVE(node), exp);
464 }
465}
466
467template <typename T>
468void InvokeHandlerWithException(
469 RR_WEAK_PTR<RobotRaconteurNode> node,
470 const typename boost::function<void(T, const RR_SHARED_PTR<RobotRaconteurException>&)>& handler,
471 const RR_SHARED_PTR<RobotRaconteurException>& exp)
472{
473 typename boost::initialized<typename boost::remove_reference<T>::type> default_value;
474 try
475 {
476 handler(default_value, exp);
477 }
478 catch (std::exception& exp)
479 {
480 InvokeHandler_HandleException(RR_MOVE(node), exp);
481 }
482}
483
484template <typename T>
485void InvokeHandlerWithException(
486 RR_WEAK_PTR<RobotRaconteurNode> node,
487 const typename boost::function<void(T, const RR_SHARED_PTR<RobotRaconteurException>&)>& handler,
488 std::exception& exp, MessageErrorType default_err = MessageErrorType_UnknownError)
489{
490 typename boost::initialized<typename boost::remove_const<typename boost::remove_reference<T>::type>::type>
491 default_value;
492 try
493 {
494 RR_SHARED_PTR<RobotRaconteurException> err =
495 RobotRaconteurExceptionUtil::ExceptionToSharedPtr(exp, default_err);
496 handler(default_value, err);
497 }
498 catch (std::exception& exp)
499 {
500 InvokeHandler_HandleException(RR_MOVE(node), exp);
501 }
502}
503
504ROBOTRACONTEUR_CORE_API void PostHandler(RR_WEAK_PTR<RobotRaconteurNode> node, const boost::function<void()>& handler,
505 bool shutdown_op = false, bool throw_on_released = true);
506
507template <typename T>
508void PostHandler(RR_WEAK_PTR<RobotRaconteurNode> node, const typename boost::function<void(const T&)>& handler,
509 const T& value, bool shutdown_op = false, bool throw_on_released = true)
510{
511 boost::function<void()> h = boost::bind(handler, value);
512 InvokeHandler_DoPost(RR_MOVE(node), h, shutdown_op, throw_on_released);
513}
514
515ROBOTRACONTEUR_CORE_API void PostHandler(
516 RR_WEAK_PTR<RobotRaconteurNode> node,
517 const boost::function<void(const RR_SHARED_PTR<RobotRaconteurException>&)>& handler, bool shutdown_op = false,
518 bool throw_on_released = true);
519
520ROBOTRACONTEUR_CORE_API void PostHandlerWithException(
521 RR_WEAK_PTR<RobotRaconteurNode> node,
522 const boost::function<void(const RR_SHARED_PTR<RobotRaconteurException>&)>& handler,
523 const RR_SHARED_PTR<RobotRaconteurException>& exp, bool shutdown_op = false, bool throw_on_released = true);
524
525ROBOTRACONTEUR_CORE_API void PostHandlerWithException(
526 RR_WEAK_PTR<RobotRaconteurNode> node,
527 const boost::function<void(const RR_SHARED_PTR<RobotRaconteurException>&)>& handler, std::exception& exp,
528 MessageErrorType default_err = MessageErrorType_UnknownError, bool shutdown_op = false,
529 bool throw_on_released = true);
530
531template <typename T>
532void PostHandler(RR_WEAK_PTR<RobotRaconteurNode> node,
533 const typename boost::function<void(const T&, const RR_SHARED_PTR<RobotRaconteurException>&)>& handler,
534 const T& value, bool shutdown_op = false, bool throw_on_released = true)
535{
536 boost::function<void()> h = boost::bind(handler, value, RR_SHARED_PTR<RobotRaconteurException>());
537 InvokeHandler_DoPost(RR_MOVE(node), h, shutdown_op, throw_on_released);
538}
539
540template <typename T>
541void PostHandlerWithException(
542 RR_WEAK_PTR<RobotRaconteurNode> node,
543 const typename boost::function<void(T, const RR_SHARED_PTR<RobotRaconteurException>&)>& handler,
544 const RR_SHARED_PTR<RobotRaconteurException>& exp, bool shutdown_op = false, bool throw_on_released = true)
545{
546 typename boost::initialized<typename boost::remove_reference<T>::type> default_value;
547 boost::function<void()> h = boost::bind(handler, default_value, exp);
548 InvokeHandler_DoPost(RR_MOVE(node), h, shutdown_op, throw_on_released);
549}
550
551template <typename T>
552void PostHandlerWithException(
553 RR_WEAK_PTR<RobotRaconteurNode> node,
554 const typename boost::function<void(T, const RR_SHARED_PTR<RobotRaconteurException>&)>& handler,
555 std::exception& exp, MessageErrorType default_err = MessageErrorType_UnknownError, bool shutdown_op = false,
556 bool throw_on_released = true)
557{
558 typename boost::initialized<typename boost::remove_reference<T>::type> default_value;
559 RR_SHARED_PTR<RobotRaconteurException> err = RobotRaconteurExceptionUtil::ExceptionToSharedPtr(exp, default_err);
560 boost::function<void()> h = boost::bind(handler, default_value, err);
561 InvokeHandler_DoPost(RR_MOVE(node), h, shutdown_op, throw_on_released);
562}
563
564} // namespace detail
565} // namespace RobotRaconteur
#define RR_TIMEOUT_INFINITE
Disable timeout for asynchronous operations.
Definition RobotRaconteurConstants.h:566
InternalErrorException(const std::string &message, std::string sub_name="", const boost::intrusive_ptr< RRValue > &param_=boost::intrusive_ptr< RRValue >())
Construct an InternalErrorException.
InvalidOperationException(const std::string &message, std::string sub_name="", const boost::intrusive_ptr< RRValue > &param_=boost::intrusive_ptr< RRValue >())
Construct an InvalidOperationException.
The central node implementation.
Definition RobotRaconteurNode.h:132