-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtimer.cc
More file actions
42 lines (31 loc) · 730 Bytes
/
timer.cc
File metadata and controls
42 lines (31 loc) · 730 Bytes
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
#include "timer.h"
#include <lace/try.h>
#include "core.h"
#include <cassert>
#include <unistd.h>
#include <sys/timerfd.h>
namespace {
static const int flags = TFD_NONBLOCK | TFD_CLOEXEC;
}
timer::timer(duration d, bool realtime)
: simple_task(TRY(timerfd_create, realtime ? CLOCK_REALTIME : CLOCK_MONOTONIC, flags))
{
struct itimerspec its = { .it_interval = d, .it_value = d };
TRY(timerfd_settime, t.fd(), 0, &its, NULL);
}
void
timer::operator()() {
uint64_t c;
while (!dead()) {
int r = TRY_ERR(EAGAIN, read, t.fd(), &c, sizeof c);
if (r < 0) {
core::wait_for_read(t);
continue;
}
assert(r == sizeof c);
assert(c);
while (!dead() && c-->0)
on_tick();
}
}
//