-
Notifications
You must be signed in to change notification settings - Fork 184
/
Copy pathio_uring.cpp
116 lines (100 loc) · 4.62 KB
/
io_uring.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
* Copyright (c) 2023 Maikel Nadolski
* Copyright (c) 2023 NVIDIA Corporation
*
* Licensed under the Apache License Version 2.0 with LLVM Exceptions
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* https://llvm.org/LICENSE.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "exec/linux/io_uring_context.hpp"
#include "exec/when_any.hpp"
#include "exec/finally.hpp"
#include "stdexec/execution.hpp"
#include <chrono>
#include <thread>
#include <iostream>
auto main() -> int {
exec::io_uring_context context;
exec::io_uring_context context2;
std::thread io_thread{[&] {
context.run_until_stopped();
}};
std::thread io_thread2{[&] {
context2.run_until_stopped();
}};
auto scheduler = context.get_scheduler();
auto scheduler2 = context2.get_scheduler();
using namespace std::chrono_literals;
stdexec::sync_wait(
exec::when_any(
exec::schedule_after(scheduler, 1s) | stdexec::then([] { std::cout << "Hello, 1!\n"; }),
exec::schedule_after(scheduler2, 2s) | stdexec::then([] { std::cout << "Hello, 2!\n"; })
| stdexec::upon_stopped([] { std::cout << "Hello, 2, stopped.\n"; })));
stdexec::sync_wait(
exec::when_any(
exec::schedule_after(scheduler, 1s) | stdexec::then([] { std::cout << "Hello, 1!\n"; })
| stdexec::upon_stopped([] { std::cout << "Hello, 1, stopped.\n"; }),
exec::schedule_after(scheduler2, 500ms) | stdexec::then([] { std::cout << "Hello, 2!\n"; })
| stdexec::upon_stopped([] { std::cout << "Hello, 2, stopped.\n"; })));
stdexec::sync_wait(
stdexec::when_all(
stdexec::schedule(scheduler) | stdexec::then([] { std::cout << "Hello, 0!\n"; }),
exec::schedule_after(scheduler, 1s) | stdexec::then([] { std::cout << "Hello, 1!\n"; }),
exec::schedule_after(scheduler2, 2s) | stdexec::then([] { std::cout << "Hello, 2!\n"; }),
exec::schedule_after(scheduler, 3s) | stdexec::then([] { std::cout << "Stop it!\n"; }),
exec::finally(exec::schedule_after(scheduler2, 4s), stdexec::just() | stdexec::then([&] {
context.request_stop();
})),
exec::finally(exec::schedule_after(scheduler, 4s), stdexec::just() | stdexec::then([&] {
context2.request_stop();
})),
exec::schedule_after(scheduler, 10s) //
| stdexec::then([] { //
std::cout << "Hello, world!\n"; //
}) //
| stdexec::upon_stopped([] { //
std::cout << "Hello, stopped.\n"; //
}), //
exec::schedule_after(scheduler2, 10s) //
| stdexec::then([] { //
std::cout << "Hello, world!\n"; //
}) //
| stdexec::upon_stopped([] { //
std::cout << "Hello, stopped.\n"; //
}))); //
io_thread.join();
io_thread2.join();
stdexec::sync_wait(
stdexec::schedule(scheduler)
| stdexec::then([] { std::cout << "This should not print, because the context is stopped.\n"; })
| stdexec::upon_stopped([] { std::cout << "The context is stopped!\n"; }));
stdexec::sync_wait(
stdexec::schedule(scheduler2)
| stdexec::then([] { std::cout << "This should not print, because the context is stopped.\n"; })
| stdexec::upon_stopped([] { std::cout << "The context is stopped!\n"; }));
context.reset();
io_thread = std::thread{[&] {
context.run_until_stopped();
}};
while (!context.is_running())
;
stdexec::sync_wait(
exec::when_any(
exec::schedule_after(scheduler, 1s) | stdexec::then([] { std::cout << "Hello, 1!\n"; }),
exec::schedule_after(scheduler, 500ms) | stdexec::then([] { std::cout << "Hello, 2!\n"; })));
auto time_point = std::chrono::steady_clock::now() + 1s;
stdexec::sync_wait(exec::schedule_at(scheduler, time_point) | stdexec::then([] {
std::cout << "Hello, schedule_at!\n";
}));
static_assert(exec::timed_scheduler<exec::io_uring_scheduler>);
context.request_stop();
io_thread.join();
}