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 
10 #include "flutter/fml/eintr_wrapper.h"
11 
12 #if FML_TIMERFD_AVAILABLE == 0
13 
14 #include <asm/unistd.h>
15 #include <sys/syscall.h>
16 
17 int timerfd_create(int clockid, int flags) {
18  return syscall(__NR_timerfd_create, clockid, flags);
19 }
20 
21 int timerfd_settime(int ufc,
22  int flags,
23  const struct itimerspec* utmr,
24  struct itimerspec* otmr) {
25  return syscall(__NR_timerfd_settime, ufc, flags, utmr, otmr);
26 }
27 
28 #endif // FML_TIMERFD_AVAILABLE == 0
29 
30 namespace fml {
31 
32 #ifndef NSEC_PER_SEC
33 #define NSEC_PER_SEC 1000000000
34 #endif
35 
36 bool TimerRearm(int fd, fml::TimePoint time_point) {
37  uint64_t nano_secs = time_point.ToEpochDelta().ToNanoseconds();
38 
39  // "0" will disarm the timer, desired behavior is to immediately
40  // trigger the timer.
41  if (nano_secs < 1) {
42  nano_secs = 1;
43  }
44 
45  struct itimerspec spec = {};
46  spec.it_value.tv_sec = (time_t)(nano_secs / NSEC_PER_SEC);
47  spec.it_value.tv_nsec = nano_secs % NSEC_PER_SEC;
48  spec.it_interval = spec.it_value; // single expiry.
49 
50  int result = ::timerfd_settime(fd, TFD_TIMER_ABSTIME, &spec, nullptr);
51  return result == 0;
52 }
53 
54 bool TimerDrain(int fd) {
55  // 8 bytes must be read from a signaled timer file descriptor when signaled.
56  uint64_t fire_count = 0;
57  ssize_t size = FML_HANDLE_EINTR(::read(fd, &fire_count, sizeof(uint64_t)));
58  if (size != sizeof(uint64_t)) {
59  return false;
60  }
61  return fire_count > 0;
62 }
63 
64 } // namespace fml
int timerfd_settime(int ufc, int flags, const struct itimerspec *utmr, struct itimerspec *otmr)
Definition: timerfd.cc:21
#define NSEC_PER_SEC
Definition: timerfd.cc:33
constexpr std::size_t size(T(&array)[N])
Definition: size.h:13
TimeDelta ToEpochDelta() const
Definition: time_point.h:40
Definition: ascii_trie.cc:9
#define FML_HANDLE_EINTR(x)
Definition: eintr_wrapper.h:33
int timerfd_create(int clockid, int flags)
Definition: timerfd.cc:17
constexpr int64_t ToNanoseconds() const
Definition: time_delta.h:61
bool TimerDrain(int fd)
Definition: timerfd.cc:54
bool TimerRearm(int fd, fml::TimePoint time_point)
Rearms the timer to expire at the given time point.
Definition: timerfd.cc:36
#define TFD_TIMER_ABSTIME
Definition: timerfd.h:29
DEF_SWITCHES_START snapshot asset Path to the directory containing the four files specified by VmSnapshotInstructions and IsolateSnapshotInstructions vm snapshot The VM instructions snapshot that will be memory mapped as read and executable SnapshotAssetPath must be present isolate snapshot The isolate instructions snapshot that will be memory mapped as read and executable SnapshotAssetPath must be present icu symbol Prefix for the symbols representing ICU data linked into the Flutter library dart flags
Definition: switches.h:66