/
awaitable.cpp
79 lines (61 loc) · 1.66 KB
/
awaitable.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
// awaitable.cpp
#include <chrono>
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <sys/epoll.h>
#include <sys/timerfd.h>
#include <stdcoro/coroutine.hpp>
#include <libcoro/eager_task.hpp>
#include <libcoro/nix/unique_fd.hpp>
#include <libcoro/nix/system_error.hpp>
#include "awaitable_timer.hpp"
constexpr static auto const DEFAULT_N_EXPIRATIONS = 5ul;
void reactor(
int const ioc,
unsigned long const n_expirations)
{
struct epoll_event ev{};
for (auto count = 0ul; count < n_expirations; ++count)
{
int const n_events = ::epoll_wait(ioc, &ev, 1, -1);
if (-1 == n_events)
{
throw coro::nix::system_error{};
}
if (ev.events & EPOLLIN)
{
awaitable_timer::on_timer_expire(ev.data.ptr);
}
}
}
coro::eager_task<void> waiter(
int const ioc,
unsigned long const n_expirations)
{
using namespace std::chrono_literals;
awaitable_timer two_seconds{ioc, 2s};
for (auto count = 0ul; count < n_expirations; ++count)
{
co_await two_seconds;
puts("[+] timer fired");
}
}
int main(int argc, char* argv[])
{
using namespace std::chrono_literals;
auto const n_expirations = (argc > 1)
? std::stoul(argv[1])
: DEFAULT_N_EXPIRATIONS;
// create the epoll instance
auto instance = coro::nix::unique_fd{::epoll_create1(0)};
if (!instance)
{
throw coro::nix::system_error{};
}
// start the waiter task
auto waiter_res = waiter(instance.get(), n_expirations);
// run the reactor
reactor(instance.get(), n_expirations);
return EXIT_SUCCESS;
}