Files
engine/fml/platform/linux/timerfd.cc
T

65 lines
1.7 KiB
C++
Raw Normal View History

2018-11-07 12:24:35 -08:00
// Copyright 2013 The Flutter Authors. All rights reserved.
2017-03-20 14:49:10 -07:00
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "flutter/fml/platform/linux/timerfd.h"
#include <sys/types.h>
#include <unistd.h>
#include "flutter/fml/eintr_wrapper.h"
2017-03-20 14:49:10 -07:00
#if FML_TIMERFD_AVAILABLE == 0
#include <asm/unistd.h>
#include <sys/syscall.h>
int timerfd_create(int clockid, int flags) {
return syscall(__NR_timerfd_create, clockid, flags);
}
int timerfd_settime(int ufc,
int flags,
const struct itimerspec* utmr,
struct itimerspec* otmr) {
return syscall(__NR_timerfd_settime, ufc, flags, utmr, otmr);
}
#endif // FML_TIMERFD_AVAILABLE == 0
namespace fml {
#ifndef NSEC_PER_SEC
#define NSEC_PER_SEC 1000000000
#endif
2018-07-26 12:49:34 -07:00
bool TimerRearm(int fd, fml::TimePoint time_point) {
uint64_t nano_secs = time_point.ToEpochDelta().ToNanoseconds();
// "0" will disarm the timer, desired behavior is to immediately
// trigger the timer.
if (nano_secs < 1) {
nano_secs = 1;
}
2017-03-20 14:49:10 -07:00
struct itimerspec spec = {};
spec.it_value.tv_sec = (time_t)(nano_secs / NSEC_PER_SEC);
spec.it_value.tv_nsec = nano_secs % NSEC_PER_SEC;
spec.it_interval = spec.it_value; // single expiry.
int result = ::timerfd_settime(fd, TFD_TIMER_ABSTIME, &spec, nullptr);
return result == 0;
}
bool TimerDrain(int fd) {
2019-05-25 13:14:46 -07:00
// 8 bytes must be read from a signaled timer file descriptor when signaled.
2017-03-20 14:49:10 -07:00
uint64_t fire_count = 0;
ssize_t size = FML_HANDLE_EINTR(::read(fd, &fire_count, sizeof(uint64_t)));
2017-03-20 14:49:10 -07:00
if (size != sizeof(uint64_t)) {
return false;
}
return fire_count > 0;
}
} // namespace fml