8 typedef _awaker<event_impl> event_awaker;
9 typedef std::shared_ptr<event_awaker> event_awaker_ptr;
11 struct event_impl :
public std::enable_shared_from_this<event_impl>
15 typedef std::recursive_mutex lock_type;
17 std::list<event_awaker_ptr> _awakes;
21 event_impl(intptr_t initial_counter_);
27 bool wait_(
const event_awaker_ptr& awaker);
29 template<
class callee_t,
class dummy_t = std::enable_if<!std::is_same<std::remove_cv_t<callee_t>, event_awaker_ptr>::value>>
30 decltype(
auto) wait(callee_t&& awaker, dummy_t* dummy_ =
nullptr)
33 return wait_(std::make_shared<event_awaker>(std::forward<callee_t>(awaker)));
36 event_impl(
const event_impl&) =
delete;
37 event_impl(event_impl&&) =
delete;
38 event_impl& operator = (
const event_impl&) =
delete;
39 event_impl& operator = (event_impl&&) =
delete;
51 typedef std::shared_ptr<detail::event_impl> event_impl_ptr;
52 typedef std::weak_ptr<detail::event_impl> event_impl_wptr;
53 typedef std::chrono::system_clock clock_type;
55 event_impl_ptr _event;
58 event_t(intptr_t initial_counter_ = 0);
73 template<
class _Rep,
class _Period>
75 wait_for(
const std::chrono::duration<_Rep, _Period>& dt)
const
77 return wait_for_(std::chrono::duration_cast<clock_type::duration>(dt));
79 template<
class _Clock,
class _Duration>
81 wait_until(
const std::chrono::time_point<_Clock, _Duration>& tp)
const
83 return wait_until_(std::chrono::time_point_cast<clock_type::duration>(tp));
91 static future_t<intptr_t>
92 wait_any(_Iter begin_, _Iter end_)
94 return wait_any_(make_event_vector(begin_, end_));
97 static future_t<intptr_t>
98 wait_any(
const _Cont& cnt_)
100 return wait_any_(make_event_vector(std::begin(cnt_), std::end(cnt_)));
103 template<
class _Rep,
class _Period,
class _Iter>
104 static future_t<intptr_t>
105 wait_any_for(
const std::chrono::duration<_Rep, _Period>& dt, _Iter begin_, _Iter end_)
107 return wait_any_for_(std::chrono::duration_cast<clock_type::duration>(dt), make_event_vector(begin_, end_));
109 template<
class _Rep,
class _Period,
class _Cont>
110 static future_t<intptr_t>
111 wait_any_for(
const std::chrono::duration<_Rep, _Period>& dt,
const _Cont& cnt_)
113 return wait_any_for_(std::chrono::duration_cast<clock_type::duration>(dt), make_event_vector(std::begin(cnt_), std::end(cnt_)));
116 template<
class _Clock,
class _Duration,
class _Iter>
117 static future_t<intptr_t>
118 wait_any_until(
const std::chrono::time_point<_Clock, _Duration>& tp, _Iter begin_, _Iter end_)
120 return wait_any_until_(std::chrono::time_point_cast<clock_type::duration>(tp), make_event_vector(begin_, end_));
122 template<
class _Clock,
class _Duration,
class _Cont>
123 static future_t<intptr_t>
124 wait_any_until(
const std::chrono::time_point<_Clock, _Duration>& tp,
const _Cont& cnt_)
126 return wait_any_until_(std::chrono::time_point_cast<clock_type::duration>(tp), make_event_vector(std::begin(cnt_), std::end(cnt_)));
133 template<
class _Iter>
134 static future_t<bool>
135 wait_all(_Iter begin_, _Iter end_)
137 return wait_all_(make_event_vector(begin_, end_));
139 template<
class _Cont>
140 static future_t<bool>
141 wait_all(
const _Cont& cnt_)
143 return wait_all(std::begin(cnt_), std::end(cnt_));
146 template<
class _Rep,
class _Period,
class _Iter>
147 static future_t<bool>
148 wait_all_for(
const std::chrono::duration<_Rep, _Period>& dt, _Iter begin_, _Iter end_)
150 return wait_all_for_(std::chrono::duration_cast<clock_type::duration>(dt), make_event_vector(begin_, end_));
152 template<
class _Rep,
class _Period,
class _Cont>
153 static future_t<bool>
154 wait_all_for(
const std::chrono::duration<_Rep, _Period>& dt,
const _Cont& cnt_)
156 return wait_all_for_(std::chrono::duration_cast<clock_type::duration>(dt), make_event_vector(std::begin(cnt_), std::end(cnt_)));
159 template<
class _Clock,
class _Duration,
class _Iter>
160 static future_t<bool>
161 wait_all_until(
const std::chrono::time_point<_Clock, _Duration>& tp, _Iter begin_, _Iter end_)
163 return wait_all_until_(std::chrono::time_point_cast<clock_type::duration>(tp), make_event_vector(begin_, end_));
165 template<
class _Clock,
class _Duration,
class _Cont>
166 static future_t<bool>
167 wait_all_until(
const std::chrono::time_point<_Clock, _Duration>& tp,
const _Cont& cnt_)
169 return wait_all_until_(std::chrono::time_point_cast<clock_type::duration>(tp), make_event_vector(std::begin(cnt_), std::end(cnt_)));
174 event_t(
const event_t&) =
default;
175 event_t(event_t&&) =
default;
176 event_t& operator = (
const event_t&) =
default;
177 event_t& operator = (event_t&&) =
default;
180 template<
class _Iter>
181 static std::vector<event_impl_ptr> make_event_vector(_Iter begin_, _Iter end_)
183 std::vector<event_impl_ptr> evts;
184 evts.reserve(std::distance(begin_, end_));
185 for (
auto i = begin_; i != end_; ++i)
186 evts.push_back((*i)._event);
191 inline future_t<bool> wait_for_(
const clock_type::duration& dt)
const
193 return wait_until_(clock_type::now() + dt);
195 future_t<bool> wait_until_(
const clock_type::time_point& tp)
const;
198 static future_t<intptr_t> wait_any_(std::vector<event_impl_ptr>&& evts);
199 inline static future_t<intptr_t> wait_any_for_(
const clock_type::duration& dt, std::vector<event_impl_ptr>&& evts)
201 return wait_any_until_(clock_type::now() + dt, std::forward<std::vector<event_impl_ptr>>(evts));
203 static future_t<intptr_t> wait_any_until_(
const clock_type::time_point& tp, std::vector<event_impl_ptr>&& evts);
206 static future_t<bool> wait_all_(std::vector<event_impl_ptr>&& evts);
207 inline static future_t<bool> wait_all_for_(
const clock_type::duration& dt, std::vector<event_impl_ptr>&& evts)
209 return wait_all_until_(clock_type::now() + dt, std::forward<std::vector<event_impl_ptr>>(evts));
211 static future_t<bool> wait_all_until_(
const clock_type::time_point& tp, std::vector<event_impl_ptr>&& evts);