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 | |
8 | #ifndef BOOST_THREAD_FUTURES_WAIT_FOR_ANY_HPP |
9 | #define BOOST_THREAD_FUTURES_WAIT_FOR_ANY_HPP |
10 | |
11 | #include <boost/thread/detail/config.hpp> |
12 | |
13 | #include <boost/thread/detail/move.hpp> |
14 | #include <boost/thread/futures/is_future_type.hpp> |
15 | #include <boost/thread/lock_algorithms.hpp> |
16 | #include <boost/thread/mutex.hpp> |
17 | |
18 | #include <boost/core/enable_if.hpp> |
19 | #include <boost/next_prior.hpp> |
20 | #include <boost/scoped_array.hpp> |
21 | |
22 | #include <iterator> |
23 | #include <vector> |
24 | |
25 | namespace boost |
26 | { |
27 | namespace detail |
28 | { |
29 | template <class Future> |
30 | class waiter_for_any_in_seq |
31 | { |
32 | struct registered_waiter; |
33 | typedef std::vector<int>::size_type count_type; |
34 | |
35 | struct registered_waiter |
36 | { |
37 | typedef Future future_type; |
38 | future_type* future_; |
39 | typedef typename Future::notify_when_ready_handle notify_when_ready_handle; |
40 | notify_when_ready_handle handle; |
41 | count_type index; |
42 | |
43 | registered_waiter(future_type & a_future, |
44 | notify_when_ready_handle handle_, count_type index_) : |
45 | future_(&a_future), handle(handle_), index(index_) |
46 | { |
47 | } |
48 | }; |
49 | |
50 | struct all_futures_lock |
51 | { |
52 | #ifdef _MANAGED |
53 | typedef std::ptrdiff_t count_type_portable; |
54 | #else |
55 | typedef count_type count_type_portable; |
56 | #endif |
57 | count_type_portable count; |
58 | boost::scoped_array<boost::unique_lock<boost::mutex> > locks; |
59 | |
60 | all_futures_lock(std::vector<registered_waiter>& waiters) : |
61 | count(waiters.size()), locks(new boost::unique_lock<boost::mutex>[count]) |
62 | { |
63 | for (count_type_portable i = 0; i < count; ++i) |
64 | { |
65 | locks[i] = BOOST_THREAD_MAKE_RV_REF(boost::unique_lock<boost::mutex>(waiters[i].future_->mutex())); |
66 | } |
67 | } |
68 | |
69 | void lock() |
70 | { |
71 | boost::lock(locks.get(), locks.get() + count); |
72 | } |
73 | |
74 | void unlock() |
75 | { |
76 | for (count_type_portable i = 0; i < count; ++i) |
77 | { |
78 | locks[i].unlock(); |
79 | } |
80 | } |
81 | }; |
82 | |
83 | boost::condition_variable_any cv; |
84 | std::vector<registered_waiter> waiters_; |
85 | count_type future_count; |
86 | |
87 | public: |
88 | waiter_for_any_in_seq() : |
89 | future_count(0) |
90 | { |
91 | } |
92 | |
93 | template <typename F> |
94 | void add(F& f) |
95 | { |
96 | if (f.valid()) |
97 | { |
98 | registered_waiter waiter(f, f.notify_when_ready(cv), future_count); |
99 | try |
100 | { |
101 | waiters_.push_back(waiter); |
102 | } |
103 | catch (...) |
104 | { |
105 | f.future_->unnotify_when_ready(waiter.handle); |
106 | throw; |
107 | } |
108 | ++future_count; |
109 | } |
110 | } |
111 | |
112 | #ifndef BOOST_NO_CXX11_VARIADIC_TEMPLATES |
113 | template <typename F1, typename ... Fs> |
114 | void add(F1& f1, Fs&... fs) |
115 | { |
116 | add(f1); |
117 | add(fs...); |
118 | } |
119 | #endif |
120 | |
121 | count_type wait() |
122 | { |
123 | all_futures_lock lk(waiters_); |
124 | for (;;) |
125 | { |
126 | for (count_type i = 0; i < waiters_.size(); ++i) |
127 | { |
128 | if (waiters_[i].future_->is_ready(lk.locks[i])) |
129 | { |
130 | return waiters_[i].index; |
131 | } |
132 | } |
133 | cv.wait(lk); |
134 | } |
135 | } |
136 | |
137 | ~waiter_for_any_in_seq() |
138 | { |
139 | for (count_type i = 0; i < waiters_.size(); ++i) |
140 | { |
141 | waiters_[i].future_->unnotify_when_ready(waiters_[i].handle); |
142 | } |
143 | } |
144 | }; |
145 | } |
146 | |
147 | template <typename Iterator> |
148 | typename boost::disable_if<is_future_type<Iterator> , Iterator>::type wait_for_any(Iterator begin, Iterator end) |
149 | { |
150 | if (begin == end) return end; |
151 | |
152 | detail::waiter_for_any_in_seq<typename std::iterator_traits<Iterator>::value_type> waiter; |
153 | for (Iterator current = begin; current != end; ++current) |
154 | { |
155 | waiter.add(*current); |
156 | } |
157 | return boost::next(begin, waiter.wait()); |
158 | } |
159 | } |
160 | |
161 | #endif // header |
162 | |