wait_for_any.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // (C) Copyright 2008-10 Anthony Williams
  2. // (C) Copyright 2011-2015 Vicente J. Botet Escriba
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See
  5. // accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. #ifndef BOOST_THREAD_FUTURES_WAIT_FOR_ANY_HPP
  8. #define BOOST_THREAD_FUTURES_WAIT_FOR_ANY_HPP
  9. #include <boost/thread/detail/config.hpp>
  10. #include <boost/thread/detail/move.hpp>
  11. #include <boost/thread/futures/is_future_type.hpp>
  12. #include <boost/thread/lock_algorithms.hpp>
  13. #include <boost/thread/mutex.hpp>
  14. #include <boost/thread/condition_variable.hpp>
  15. #include <boost/core/enable_if.hpp>
  16. #include <boost/scoped_array.hpp>
  17. #include <iterator>
  18. #include <vector>
  19. namespace boost
  20. {
  21. namespace detail
  22. {
  23. template <class Future>
  24. class waiter_for_any_in_seq
  25. {
  26. struct registered_waiter;
  27. typedef std::vector<int>::size_type count_type;
  28. struct registered_waiter
  29. {
  30. typedef Future future_type;
  31. future_type* future_;
  32. typedef typename Future::notify_when_ready_handle notify_when_ready_handle;
  33. notify_when_ready_handle handle;
  34. count_type index;
  35. registered_waiter(future_type & a_future,
  36. notify_when_ready_handle handle_, count_type index_) :
  37. future_(&a_future), handle(handle_), index(index_)
  38. {
  39. }
  40. };
  41. struct all_futures_lock
  42. {
  43. #ifdef _MANAGED
  44. typedef std::ptrdiff_t count_type_portable;
  45. #else
  46. typedef count_type count_type_portable;
  47. #endif
  48. count_type_portable count;
  49. boost::scoped_array<boost::unique_lock<boost::mutex> > locks;
  50. all_futures_lock(std::vector<registered_waiter>& waiters) :
  51. count(waiters.size()), locks(new boost::unique_lock<boost::mutex>[count])
  52. {
  53. for (count_type_portable i = 0; i < count; ++i)
  54. {
  55. locks[i] = BOOST_THREAD_MAKE_RV_REF(boost::unique_lock<boost::mutex>(waiters[i].future_->mutex()));
  56. }
  57. }
  58. void lock()
  59. {
  60. boost::lock(locks.get(), locks.get() + count);
  61. }
  62. void unlock()
  63. {
  64. for (count_type_portable i = 0; i < count; ++i)
  65. {
  66. locks[i].unlock();
  67. }
  68. }
  69. };
  70. boost::condition_variable_any cv;
  71. std::vector<registered_waiter> waiters_;
  72. count_type future_count;
  73. public:
  74. waiter_for_any_in_seq() :
  75. future_count(0)
  76. {
  77. }
  78. template <typename F>
  79. void add(F& f)
  80. {
  81. if (f.valid())
  82. {
  83. registered_waiter waiter(f, f.notify_when_ready(cv), future_count);
  84. try
  85. {
  86. waiters_.push_back(waiter);
  87. }
  88. catch (...)
  89. {
  90. f.future_->unnotify_when_ready(waiter.handle);
  91. throw;
  92. }
  93. ++future_count;
  94. }
  95. }
  96. #ifndef BOOST_NO_CXX11_VARIADIC_TEMPLATES
  97. template <typename F1, typename ... Fs>
  98. void add(F1& f1, Fs&... fs)
  99. {
  100. add(f1);
  101. add(fs...);
  102. }
  103. #endif
  104. count_type wait()
  105. {
  106. all_futures_lock lk(waiters_);
  107. for (;;)
  108. {
  109. for (count_type i = 0; i < waiters_.size(); ++i)
  110. {
  111. if (waiters_[i].future_->is_ready(lk.locks[i]))
  112. {
  113. return waiters_[i].index;
  114. }
  115. }
  116. cv.wait(lk);
  117. }
  118. }
  119. ~waiter_for_any_in_seq()
  120. {
  121. for (count_type i = 0; i < waiters_.size(); ++i)
  122. {
  123. waiters_[i].future_->unnotify_when_ready(waiters_[i].handle);
  124. }
  125. }
  126. };
  127. }
  128. template <typename Iterator>
  129. typename boost::disable_if<is_future_type<Iterator> , Iterator>::type wait_for_any(Iterator begin, Iterator end)
  130. {
  131. if (begin == end) return end;
  132. detail::waiter_for_any_in_seq<typename std::iterator_traits<Iterator>::value_type> waiter;
  133. for (Iterator current = begin; current != end; ++current)
  134. {
  135. waiter.add(*current);
  136. }
  137. std::advance( begin, waiter.wait() );
  138. return begin;
  139. }
  140. }
  141. #endif // header