Skip to content

Commit 803cc2e

Browse files
author
tearshark
committed
支持when_all/when_any
1 parent 6c9f476 commit 803cc2e

17 files changed

+363
-94
lines changed

librf/librf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@
2121
#include "src/channel.h"
2222
#include "src/scheduler.h"
2323
#include "src/sleep.h"
24+
#include "src/when.h"

librf/src/channel.h

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -166,47 +166,47 @@ namespace resumef
166166
}
167167

168168
template<class _Ty2>
169-
awaitable_t<bool> write(_Ty2&& val) const
169+
future_t<bool> write(_Ty2&& val) const
170170
{
171-
awaitable_t<bool> awaitable;
171+
promise_t<bool> awaitable;
172172

173173
auto awaker = std::make_shared<channel_write_awaker>(
174174
[st = awaitable._state](channel_impl_type * chan) -> bool
175-
{
176-
st->set_value(chan ? true : false);
177-
return true;
178-
});
175+
{
176+
st->set_value(chan ? true : false);
177+
return true;
178+
});
179179
_chan->write_(std::move(awaker), std::forward<_Ty2>(val));
180180

181-
return awaitable;
181+
return awaitable.get_future();
182182
}
183183

184-
awaitable_t<_Ty> read() const
184+
future_t<_Ty> read() const
185185
{
186-
awaitable_t<_Ty> awaitable;
186+
promise_t<_Ty> awaitable;
187187

188188
auto awaker = std::make_shared<channel_read_awaker>(
189189
[st = awaitable._state](channel_impl_type *, _Ty * val, error_code fe) -> bool
190-
{
191-
if(val)
192-
st->set_value(std::move(*val));
193-
else
194-
st->throw_exception(channel_exception{ fe });
190+
{
191+
if(val)
192+
st->set_value(std::move(*val));
193+
else
194+
st->throw_exception(channel_exception{ fe });
195195

196-
return true;
197-
});
196+
return true;
197+
});
198198
_chan->read_(std::move(awaker));
199199

200-
return awaitable;
200+
return awaitable.get_future();
201201
}
202202

203203
template<class _Ty2>
204-
awaitable_t<bool> operator << (_Ty2&& val) const
204+
future_t<bool> operator << (_Ty2&& val) const
205205
{
206206
return std::move(write(std::forward<_Ty2>(val)));
207207
}
208208

209-
awaitable_t<_Ty> operator co_await () const
209+
future_t<_Ty> operator co_await () const
210210
{
211211
return read();
212212
}

librf/src/event.cpp

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ namespace resumef
6666
{
6767
}
6868

69-
awaitable_t<bool> event_t::wait() const
69+
future_t<bool> event_t::wait() const
7070
{
71-
awaitable_t<bool> awaitable;
71+
promise_t<bool> awaitable;
7272

7373
auto awaker = std::make_shared<detail::event_awaker>(
7474
[st = awaitable._state](detail::event_impl * e) -> bool
@@ -78,12 +78,12 @@ namespace resumef
7878
});
7979
_event->wait_(awaker);
8080

81-
return awaitable;
81+
return awaitable.get_future();
8282
}
8383

84-
awaitable_t<bool> event_t::wait_until_(const clock_type::time_point & tp) const
84+
future_t<bool> event_t::wait_until_(const clock_type::time_point & tp) const
8585
{
86-
awaitable_t<bool> awaitable;
86+
promise_t<bool> awaitable;
8787

8888
auto awaker = std::make_shared<detail::event_awaker>(
8989
[st = awaitable._state](detail::event_impl * e) -> bool
@@ -99,7 +99,7 @@ namespace resumef
9999
awaker->awake(nullptr, 1);
100100
});
101101

102-
return awaitable;
102+
return awaitable.get_future();
103103
}
104104

105105
struct wait_any_awaker
@@ -137,14 +137,14 @@ namespace resumef
137137
}
138138
};
139139

140-
awaitable_t<intptr_t> event_t::wait_any_(std::vector<event_impl_ptr> && evts)
140+
future_t<intptr_t> event_t::wait_any_(std::vector<event_impl_ptr> && evts)
141141
{
142-
awaitable_t<intptr_t> awaitable;
142+
promise_t<intptr_t> awaitable;
143143

144144
if (evts.size() <= 0)
145145
{
146146
awaitable._state->set_value(-1);
147-
return awaitable;
147+
return awaitable.get_future();
148148
}
149149

150150
auto awaker = std::make_shared<detail::event_awaker>(
@@ -174,12 +174,12 @@ namespace resumef
174174
e->wait_(awaker);
175175
}
176176

177-
return awaitable;
177+
return awaitable.get_future();
178178
}
179179

180-
awaitable_t<intptr_t> event_t::wait_any_until_(const clock_type::time_point & tp, std::vector<event_impl_ptr> && evts)
180+
future_t<intptr_t> event_t::wait_any_until_(const clock_type::time_point & tp, std::vector<event_impl_ptr> && evts)
181181
{
182-
awaitable_t<intptr_t> awaitable;
182+
promise_t<intptr_t> awaitable;
183183

184184
auto awaker = std::make_shared<detail::event_awaker>(
185185
[st = awaitable._state, evts](detail::event_impl * e) -> bool
@@ -214,16 +214,16 @@ namespace resumef
214214
awaker->awake(nullptr, 1);
215215
});
216216

217-
return awaitable;
217+
return awaitable.get_future();
218218
}
219219

220-
awaitable_t<bool> event_t::wait_all_(std::vector<event_impl_ptr> && evts)
220+
future_t<bool> event_t::wait_all_(std::vector<event_impl_ptr> && evts)
221221
{
222-
awaitable_t<bool> awaitable;
222+
promise_t<bool> awaitable;
223223
if (evts.size() <= 0)
224224
{
225225
awaitable._state->set_value(false);
226-
return awaitable;
226+
return awaitable.get_future();
227227
}
228228

229229
auto awaker = std::make_shared<detail::event_awaker>(
@@ -239,7 +239,7 @@ namespace resumef
239239
e->wait_(awaker);
240240
}
241241

242-
return awaitable;
242+
return awaitable.get_future();
243243
}
244244

245245

@@ -319,17 +319,17 @@ namespace resumef
319319
//超时后的行为应该表现为:
320320
//要么所有的事件计数减一,要么所有事件计数不动
321321
//则需要超时后,恢复已经等待的事件计数
322-
awaitable_t<bool> event_t::wait_all_until_(const clock_type::time_point & tp, std::vector<event_impl_ptr> && evts)
322+
future_t<bool> event_t::wait_all_until_(const clock_type::time_point & tp, std::vector<event_impl_ptr> && evts)
323323
{
324-
awaitable_t<bool> awaitable;
324+
promise_t<bool> awaitable;
325325
if (evts.size() <= 0)
326326
{
327327
this_scheduler()->timer()->add_handler(tp,
328328
[st = awaitable._state](bool bValue)
329329
{
330330
st->set_value(false);
331331
});
332-
return awaitable;
332+
return awaitable.get_future();
333333
}
334334

335335
auto ctx = std::make_shared<wait_all_ctx>();
@@ -353,6 +353,6 @@ namespace resumef
353353
}
354354

355355

356-
return awaitable;
356+
return awaitable.get_future();
357357
}
358358
}

librf/src/event.h

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,16 @@ namespace resumef
6363

6464

6565

66-
RF_API awaitable_t<bool>
66+
RF_API future_t<bool>
6767
wait() const;
6868
template<class _Rep, class _Period>
69-
awaitable_t<bool>
69+
future_t<bool>
7070
wait_for(const std::chrono::duration<_Rep, _Period> & dt) const
7171
{
7272
return wait_for_(std::chrono::duration_cast<clock_type::duration>(dt));
7373
}
7474
template<class _Clock, class _Duration>
75-
awaitable_t<bool>
75+
future_t<bool>
7676
wait_until(const std::chrono::time_point<_Clock, _Duration> & tp) const
7777
{
7878
return wait_until_(std::chrono::time_point_cast<clock_type::duration>(tp));
@@ -83,39 +83,39 @@ namespace resumef
8383

8484

8585
template<class _Iter>
86-
static awaitable_t<intptr_t>
86+
static future_t<intptr_t>
8787
wait_any(_Iter begin_, _Iter end_)
8888
{
8989
return wait_any_(make_event_vector(begin_, end_));
9090
}
9191
template<class _Cont>
92-
static awaitable_t<intptr_t>
92+
static future_t<intptr_t>
9393
wait_any(const _Cont & cnt_)
9494
{
9595
return wait_any_(make_event_vector(std::begin(cnt_), std::end(cnt_)));
9696
}
9797

9898
template<class _Rep, class _Period, class _Iter>
99-
static awaitable_t<intptr_t>
99+
static future_t<intptr_t>
100100
wait_any_for(const std::chrono::duration<_Rep, _Period> & dt, _Iter begin_, _Iter end_)
101101
{
102102
return wait_any_for_(std::chrono::duration_cast<clock_type::duration>(dt), make_event_vector(begin_, end_));
103103
}
104104
template<class _Rep, class _Period, class _Cont>
105-
static awaitable_t<intptr_t>
105+
static future_t<intptr_t>
106106
wait_any_for(const std::chrono::duration<_Rep, _Period> & dt, const _Cont & cnt_)
107107
{
108108
return wait_any_for_(std::chrono::duration_cast<clock_type::duration>(dt), make_event_vector(std::begin(cnt_), std::end(cnt_)));
109109
}
110110

111111
template<class _Clock, class _Duration, class _Iter>
112-
static awaitable_t<intptr_t>
112+
static future_t<intptr_t>
113113
wait_any_until(const std::chrono::time_point<_Clock, _Duration> & tp, _Iter begin_, _Iter end_)
114114
{
115115
return wait_any_until_(std::chrono::time_point_cast<clock_type::duration>(tp), make_event_vector(begin_, end_));
116116
}
117117
template<class _Clock, class _Duration, class _Cont>
118-
static awaitable_t<intptr_t>
118+
static future_t<intptr_t>
119119
wait_any_until(const std::chrono::time_point<_Clock, _Duration> & tp, const _Cont & cnt_)
120120
{
121121
return wait_any_until_(std::chrono::time_point_cast<clock_type::duration>(tp), make_event_vector(std::begin(cnt_), std::end(cnt_)));
@@ -126,47 +126,46 @@ namespace resumef
126126

127127

128128
template<class _Iter>
129-
static awaitable_t<bool>
129+
static future_t<bool>
130130
wait_all(_Iter begin_, _Iter end_)
131131
{
132132
return wait_all_(make_event_vector(begin_, end_));
133133
}
134134
template<class _Cont>
135-
static awaitable_t<bool>
135+
static future_t<bool>
136136
wait_all(const _Cont & cnt_)
137137
{
138138
return wait_all(std::begin(cnt_), std::end(cnt_));
139139
}
140140

141141
template<class _Rep, class _Period, class _Iter>
142-
static awaitable_t<bool>
142+
static future_t<bool>
143143
wait_all_for(const std::chrono::duration<_Rep, _Period> & dt, _Iter begin_, _Iter end_)
144144
{
145145
return wait_all_for_(std::chrono::duration_cast<clock_type::duration>(dt), make_event_vector(begin_, end_));
146146
}
147147
template<class _Rep, class _Period, class _Cont>
148-
static awaitable_t<bool>
148+
static future_t<bool>
149149
wait_all_for(const std::chrono::duration<_Rep, _Period> & dt, const _Cont & cnt_)
150150
{
151151
return wait_all_for_(std::chrono::duration_cast<clock_type::duration>(dt), make_event_vector(std::begin(cnt_), std::end(cnt_)));
152152
}
153153

154154
template<class _Clock, class _Duration, class _Iter>
155-
static awaitable_t<bool>
155+
static future_t<bool>
156156
wait_all_until(const std::chrono::time_point<_Clock, _Duration> & tp, _Iter begin_, _Iter end_)
157157
{
158158
return wait_all_until_(std::chrono::time_point_cast<clock_type::duration>(tp), make_event_vector(begin_, end_));
159159
}
160160
template<class _Clock, class _Duration, class _Cont>
161-
static awaitable_t<bool>
161+
static future_t<bool>
162162
wait_all_until(const std::chrono::time_point<_Clock, _Duration> & tp, const _Cont & cnt_)
163163
{
164164
return wait_all_until_(std::chrono::time_point_cast<clock_type::duration>(tp), make_event_vector(std::begin(cnt_), std::end(cnt_)));
165165
}
166166

167167

168168

169-
170169
RF_API event_t(const event_t &) = default;
171170
RF_API event_t(event_t &&) = default;
172171
RF_API event_t & operator = (const event_t &) = default;
@@ -184,27 +183,28 @@ namespace resumef
184183
return std::move(evts);
185184
}
186185

187-
inline awaitable_t<bool> wait_for_(const clock_type::duration & dt) const
186+
public:
187+
inline future_t<bool> wait_for_(const clock_type::duration & dt) const
188188
{
189189
return wait_until_(clock_type::now() + dt);
190190
}
191-
RF_API awaitable_t<bool> wait_until_(const clock_type::time_point & tp) const;
191+
RF_API future_t<bool> wait_until_(const clock_type::time_point & tp) const;
192192

193193

194-
RF_API static awaitable_t<intptr_t> wait_any_(std::vector<event_impl_ptr> && evts);
195-
inline static awaitable_t<intptr_t> wait_any_for_(const clock_type::duration & dt, std::vector<event_impl_ptr> && evts)
194+
RF_API static future_t<intptr_t> wait_any_(std::vector<event_impl_ptr> && evts);
195+
inline static future_t<intptr_t> wait_any_for_(const clock_type::duration & dt, std::vector<event_impl_ptr> && evts)
196196
{
197197
return wait_any_until_(clock_type::now() + dt, std::forward<std::vector<event_impl_ptr>>(evts));
198198
}
199-
RF_API static awaitable_t<intptr_t> wait_any_until_(const clock_type::time_point & tp, std::vector<event_impl_ptr> && evts);
199+
RF_API static future_t<intptr_t> wait_any_until_(const clock_type::time_point & tp, std::vector<event_impl_ptr> && evts);
200200

201201

202-
RF_API static awaitable_t<bool> wait_all_(std::vector<event_impl_ptr> && evts);
203-
inline static awaitable_t<bool> wait_all_for_(const clock_type::duration & dt, std::vector<event_impl_ptr> && evts)
202+
RF_API static future_t<bool> wait_all_(std::vector<event_impl_ptr> && evts);
203+
inline static future_t<bool> wait_all_for_(const clock_type::duration & dt, std::vector<event_impl_ptr> && evts)
204204
{
205205
return wait_all_until_(clock_type::now() + dt, std::forward<std::vector<event_impl_ptr>>(evts));
206206
}
207-
RF_API static awaitable_t<bool> wait_all_until_(const clock_type::time_point & tp, std::vector<event_impl_ptr> && evts);
207+
RF_API static future_t<bool> wait_all_until_(const clock_type::time_point & tp, std::vector<event_impl_ptr> && evts);
208208
};
209209

210210
}

librf/src/future.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ namespace resumef
2020
future_impl_t() = default;
2121
future_impl_t(future_impl_t&& f) = default;
2222
future_impl_t & operator = (future_impl_t&& f) = default;
23-
future_impl_t(const future_impl_t&) = delete;
24-
future_impl_t & operator = (const future_impl_t&) = delete;
23+
future_impl_t(const future_impl_t&) = default;
24+
future_impl_t & operator = (const future_impl_t&) = default;
2525

2626
//------------------------------------------------------------------------------------------
2727
//以下是与编译器生成的resumable function交互的接口
@@ -78,11 +78,11 @@ namespace resumef
7878
}
7979

8080
// movable, but not copyable
81-
future_t(const future_t&) = delete;
81+
future_t(const future_t&) = default;
8282
future_t(future_t&& f) = default;
8383
future_t() = default;
8484

85-
future_t & operator = (const future_t&) = delete;
85+
future_t & operator = (const future_t&) = default;
8686
future_t & operator = (future_t&& f) = default;
8787

8888
//------------------------------------------------------------------------------------------
@@ -290,6 +290,7 @@ namespace resumef
290290

291291
using promise_vt = promise_t<void>;
292292

293+
/*
293294
template <typename T = void>
294295
struct awaitable_t
295296
{
@@ -331,6 +332,7 @@ namespace resumef
331332
};
332333
333334
using awaitable_vt = awaitable_t<void>;
335+
*/
334336

335337
#if RESUMEF_ENABLE_MULT_SCHEDULER
336338
inline promise_t<void> * state_base::parent_promise() const

0 commit comments

Comments
 (0)