Flutter Engine
The Flutter Engine
timerfd.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "flutter/fml/platform/linux/timerfd.h"
6
7#include <sys/types.h>
8#include <unistd.h>
9#include <cstring>
10
11#include "flutter/fml/eintr_wrapper.h"
12#include "flutter/fml/logging.h"
13
14#if FML_TIMERFD_AVAILABLE == 0
15
16#include <asm/unistd.h>
17#include <sys/syscall.h>
18
19int timerfd_create(int clockid, int flags) {
20 return syscall(__NR_timerfd_create, clockid, flags);
21}
22
23int timerfd_settime(int ufc,
24 int flags,
25 const struct itimerspec* utmr,
26 struct itimerspec* otmr) {
27 return syscall(__NR_timerfd_settime, ufc, flags, utmr, otmr);
28}
29
30#endif // FML_TIMERFD_AVAILABLE == 0
31
32namespace fml {
33
34#ifndef NSEC_PER_SEC
35#define NSEC_PER_SEC 1000000000
36#endif
37
38bool TimerRearm(int fd, fml::TimePoint time_point) {
39 uint64_t nano_secs = time_point.ToEpochDelta().ToNanoseconds();
40
41 // "0" will disarm the timer, desired behavior is to immediately
42 // trigger the timer.
43 if (nano_secs < 1) {
44 nano_secs = 1;
45 }
46
47 struct itimerspec spec = {};
48 spec.it_value.tv_sec = static_cast<time_t>(nano_secs / NSEC_PER_SEC);
49 spec.it_value.tv_nsec = nano_secs % NSEC_PER_SEC;
50 spec.it_interval = spec.it_value; // single expiry.
51
52 int result = ::timerfd_settime(fd, TFD_TIMER_ABSTIME, &spec, nullptr);
53 if (result != 0) {
54 FML_DLOG(ERROR) << "timerfd_settime err:" << strerror(errno);
55 }
56 return result == 0;
57}
58
59bool TimerDrain(int fd) {
60 // 8 bytes must be read from a signaled timer file descriptor when signaled.
61 uint64_t fire_count = 0;
62 ssize_t size = FML_HANDLE_EINTR(::read(fd, &fire_count, sizeof(uint64_t)));
63 if (size != sizeof(uint64_t)) {
64 return false;
65 }
66 return fire_count > 0;
67}
68
69} // namespace fml
static bool read(SkStream *stream, void *buffer, size_t amount)
constexpr int64_t ToNanoseconds() const
Definition: time_delta.h:61
TimeDelta ToEpochDelta() const
Definition: time_point.h:52
FlutterSemanticsFlag flags
GAsyncResult * result
#define FML_HANDLE_EINTR(x)
Definition: eintr_wrapper.h:20
#define FML_DLOG(severity)
Definition: logging.h:102
Definition: ascii_trie.cc:9
constexpr std::size_t size(T(&array)[N])
Definition: size.h:13
bool TimerRearm(int fd, fml::TimePoint time_point)
Rearms the timer to expire at the given time point.
Definition: timerfd.cc:38
bool TimerDrain(int fd)
Definition: timerfd.cc:59
#define ERROR(message)
Definition: elf_loader.cc:260
#define NSEC_PER_SEC
Definition: timerfd.cc:35
int timerfd_settime(int ufc, int flags, const struct itimerspec *utmr, struct itimerspec *otmr)
Definition: timerfd.cc:23
int timerfd_create(int clockid, int flags)
Definition: timerfd.cc:19
#define TFD_TIMER_ABSTIME
Definition: timerfd.h:29