forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime_point.cc
76 lines (60 loc) · 2.05 KB
/
time_point.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright 2016 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "flutter/fml/time/time_point.h"
#include "flutter/fml/build_config.h"
#if defined(OS_MACOSX) || defined(OS_IOS)
#include <mach/kern_return.h>
#include <mach/mach_time.h>
#elif defined(OS_FUCHSIA)
#include <zircon/syscalls.h>
#elif defined(OS_WIN)
#include <windows.h>
#else
#include <time.h>
#endif // defined(OS_MACOSX) || defined(OS_IOS)
#include "flutter/fml/logging.h"
namespace fml {
// Mac OS X/iOS don't have a (useful) |clock_gettime()|.
// Note: Chromium's |base::TimeTicks::Now()| uses boot time (obtained via
// |sysctl()| with |CTL_KERN|/|KERN_BOOTTIME|). For our current purposes,
// monotonic time (which pauses during sleeps) is sufficient. TODO(vtl): If/when
// we use this for other purposes, maybe we should use boot time (maybe also on
// POSIX).
#if defined(OS_MACOSX) || defined(OS_IOS)
mach_timebase_info_data_t GetMachTimebaseInfo() {
mach_timebase_info_data_t timebase_info = {};
kern_return_t error = mach_timebase_info(&timebase_info);
FML_DCHECK(error == KERN_SUCCESS);
return timebase_info;
}
// static
TimePoint TimePoint::Now() {
static mach_timebase_info_data_t timebase_info = GetMachTimebaseInfo();
return TimePoint(mach_absolute_time() * timebase_info.numer /
timebase_info.denom);
}
#elif defined(OS_FUCHSIA)
// static
TimePoint TimePoint::Now() {
return TimePoint(zx_clock_get(ZX_CLOCK_MONOTONIC));
}
#elif defined(OS_WIN)
TimePoint TimePoint::Now() {
uint64_t freq = 0;
uint64_t count = 0;
QueryPerformanceFrequency((LARGE_INTEGER*)&freq);
QueryPerformanceCounter((LARGE_INTEGER*)&count);
return TimePoint((count * 1000000000) / freq);
}
#else
// static
TimePoint TimePoint::Now() {
struct timespec ts;
int res = clock_gettime(CLOCK_MONOTONIC, &ts);
FML_DCHECK(res == 0);
(void)res;
return TimePoint::FromEpochDelta(TimeDelta::FromTimespec(ts));
}
#endif // defined(OS_MACOSX) || defined(OS_IOS)
} // namespace fml