Browse Source

track time independently of reads and connections

master
Alexander Avery 2 months ago
parent
commit
9477ebdefc
  1. 29
      main.ha

29
main.ha

@ -102,6 +102,20 @@ fn closefd(fd: *poll::pollfd) void = {
fd.revents = 0;
};
// shouldtick returns a boolean representing if a tick
// message should be sent and a time::duration indicating
// when the next tick should be.
fn shouldtick(next: time::instant) (bool, time::duration) = {
let now = time::now(time::clock::MONOTONIC);
if (time::compare(now, next) >= 0) {
return (true, time::SECOND);
};
let diff = time::diff(now, next);
return (false, diff);
};
export fn main() void = {
let socket = tcp::listen(ip::LOCAL_V4, 8080)!;
@ -123,17 +137,28 @@ export fn main() void = {
revents = 0
};
let start = time::now(time::clock::MONOTONIC);
let nexttick = time::add(start, time::SECOND);
let wait = time::SECOND;
for (true) {
match(poll::poll(fds, time::SECOND)) {
match(poll::poll(fds, wait)) {
case let n: uint =>
if (n > 0) {
tryconn(fds);
tryread(fds);
} else {
};
let (shouldtick, d) = shouldtick(nexttick);
if (shouldtick) {
let now = time::now(time::clock::MONOTONIC);
nexttick = time::add(now, d);
write(fds);
};
wait = d;
case let err: poll::error =>
log::fatal("poll failed");
};

Loading…
Cancel
Save