-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathschedule_after_steady_timer.cpp
80 lines (63 loc) · 2.11 KB
/
schedule_after_steady_timer.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
//
// Copyright (c) 2023-present DeepGrace (complex dot invoke at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/deepgrace/snp
//
#define BOOST_ASIO_HAS_IO_URING
#define BOOST_ASIO_DISABLE_EPOLL
#include <iostream>
#include <snp.hpp>
#include <unifex/on.hpp>
#include <unifex/just.hpp>
#include <unifex/then.hpp>
#include <unifex/upon_error.hpp>
#include <unifex/scheduler_concepts.hpp>
// g++ -std=c++23 -Wall -O3 -Os -s -I include -l uring example/schedule_after_steady_timer.cpp -o /tmp/schedule_after_steady_timer
namespace net = boost::asio;
using error_code_t = boost::system::error_code;
int main(int argc, char* argv[])
{
if (argc != 2)
{
std::cerr << "Usage: " << argv[0] << " <number>" << std::endl;
return 1;
}
int number = std::stoi(argv[1]);
bool timed_out = false;
snp::asio_context ctx;
auto sch = ctx.get_scheduler();
auto duration = std::chrono::seconds(number);
unifex::then(unifex::schedule_after(sch, duration), []{ return 16; })
| unifex::then([&](int n)
{
timed_out = true;
std::cout << "schedule_after steady_timer " << n << std::endl;
})
| unifex::upon_error([]<typename Error>(Error error)
{
if constexpr(std::is_same_v<Error, std::exception_ptr>)
{
try
{
if (error)
std::rethrow_exception(error);
}
catch (const std::exception& e)
{
std::cout << "caught exception: '" << e.what() << "'" << std::endl;
}
}
})
| snp::start_detached();
assert(!timed_out);
auto begin = std::chrono::steady_clock::now();
ctx.run();
auto end = std::chrono::steady_clock::now();
auto dur = std::chrono::duration_cast<std::chrono::seconds>(end - begin).count();
std::cout << "scheduler elapsed " << dur << " seconds" << std::endl;
assert(timed_out);
return 0;
}