2
* Copyright © 2016 Canonical Ltd.
4
* This program is free software: you can redistribute it and/or modify it
5
* under the terms of the GNU Lesser General Public License version 3,
6
* as published by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU Lesser General Public License for more details.
13
* You should have received a copy of the GNU Lesser General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
* Authored by: Andreas Pokorny <andreas.pokorny@canonical.com>
19
#include "mir/time/steady_timer_fd.h"
20
#include <sys/timerfd.h>
23
namespace mt = mir::time;
25
mt::SteadyTimerFd::SteadyTimerFd()
26
: timer{timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC | TFD_NONBLOCK)}
30
mir::Fd const& mt::SteadyTimerFd::get_fd()
35
void mt::SteadyTimerFd::schedule_in(mir::time::Duration delay)
37
using namespace std::chrono;
38
auto const in_seconds = duration_cast<seconds>(delay);
39
itimerspec delay_spec;
41
delay_spec.it_value.tv_sec = in_seconds.count();
42
delay_spec.it_value.tv_nsec = nanoseconds(delay - in_seconds).count();
43
delay_spec.it_interval.tv_sec = 0;
44
delay_spec.it_interval.tv_nsec = 0;
46
const int absolute_timeout = TFD_TIMER_ABSTIME;
47
timerfd_settime(timer, absolute_timeout, &delay_spec, nullptr);
50
void mt::SteadyTimerFd::cancel()
52
const itimerspec cancel_timer = {{0,0},{0,0}};
53
timerfd_settime(timer, 0, &cancel_timer, nullptr);