Skip to content

Commit a15f1c8

Browse files
committed
basic: add Waitable concept
1 parent 3628ce0 commit a15f1c8

File tree

6 files changed

+50
-6
lines changed

6 files changed

+50
-6
lines changed

docs/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
- [run and run_forever](headers/basic/run.md)
2222
- [detached](headers/basic/detached.md)
2323
- [spawn](headers/basic/spawn.md)
24+
- [Waitable](headers/basic/waitable.md)
2425
- [async/result.hpp](headers/result.md)
2526
- [async/oneshot.hpp](headers/oneshot-event.md)
2627
- [async/wait-group.hpp](headers/wait-group.md)

docs/src/headers/basic/run.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ via the IO service.
77
## Prototype
88

99
```cpp
10-
template<typename IoService>
10+
template<Waitable IoService>
1111
void run_forever(IoService ios); // (1)
1212

1313
template<typename Sender>
1414
Sender::value_type run(Sender s); // (2)
1515

16-
template<typename Sender, typename IoService>
16+
template<typename Sender, Waitable IoService>
1717
Sender::value_type run(Sender s, IoService ios); // (3)
1818
```
1919
@@ -24,11 +24,13 @@ inline as there's no way to wait for it to complete.
2424
2525
### Requirements
2626
27-
`IoService` is an [IO service](io-service.md), and `Sender` is a sender.
27+
`IoService` is an [IO service](/io-service.md), and must be a
28+
[Waitable](./waitable.md), and `Sender` is a sender.
2829
2930
### Arguments
3031
31-
- `IoService` - the IO service to use to wait for completion.
32+
- `IoService` - the IO service to use to wait for completion. Must fulfill the
33+
[Waitable](./waitable.md) concept.
3234
- `Sender` - the sender to start.
3335
3436
### Return value

docs/src/headers/basic/waitable.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# `concept Waitable`
2+
3+
The `Waitable` concept holds all the requirements for an [IO
4+
service](/io-service.md). Presently, this is only a `wait()` method.
5+
6+
## Prototype
7+
8+
```cpp
9+
template<typename T>
10+
concept Waitable = ...;
11+
```
12+
13+
### Requirements
14+
15+
`T` provides a instance wait method that can be called on a value.
16+
17+
## Examples
18+
19+
```cpp
20+
struct io_service {
21+
/** \pre loop must still be alive */
22+
void wait() {
23+
auto loop = m_loop.lock();
24+
assert(loop);
25+
loop->wait();
26+
}
27+
private:
28+
friend struct event;
29+
io_service(std::weak_ptr<event> e) : m_loop { std::move(e) } {}
30+
std::weak_ptr<event> m_loop;
31+
};
32+
static_assert(async::Waitable<io_service>);
33+
```

docs/src/headers/wait-group.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# async/wait-group.hpp

docs/src/io-service.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ The IO service must provide one method: `void wait()`. This method is called whe
77
there is no more work to do currently. It waits for any event to happen, and wakes
88
up the appropriate coroutine/operation which awaited the event.
99

10+
See also: the [Waitable](/headers/basic/waitable.md) concept.
11+
1012
**Note:** `async::run` and `async::run_forever` (see [here](headers/basic/run.md#prototype))
1113
take the IO service by value, not by reference.
1214

include/async/basic.hpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,12 @@ struct run_queue {
309309
// Top-level execution functions.
310310
// ----------------------------------------------------------------------------
311311

312-
template<typename IoService>
312+
template<typename T>
313+
concept Waitable = requires (T t) {
314+
t.wait();
315+
}
316+
317+
template<Waitable IoService>
313318
void run_forever(IoService ios) {
314319
while(true) {
315320
ios.wait();
@@ -364,7 +369,7 @@ typename Sender::value_type run(Sender s) {
364369
platform::panic("libasync: Operation hasn't completed and we don't know how to wait");
365370
}
366371

367-
template<typename Sender, typename IoService>
372+
template<typename Sender, Waitable IoService>
368373
requires std::same_as<typename Sender::value_type, void>
369374
void run(Sender s, IoService ios) {
370375
struct state {

0 commit comments

Comments
 (0)