sel_ldr: Remove support for rodata segment at start of executable
[nativeclient.git] / service_runtime / time.cc
blob4c16869deb230b9ea17da52610fdda8eb25e2165
1 // @REWRITE(insert c-copyright)
2 // @REWRITE(delete-start)
3 // Copyright 2007 Google Inc. All Rights Reserved.
4 // @REWRITE(delete-end)
7 #include "native_client/include/portability.h"
8 #include "native_client/include/base/basictypes.h"
10 #include "native_client/service_runtime/time.h"
13 namespace {
15 // Time between resampling the un-granular clock for this API. 60 seconds.
16 const int kMaxMillisecondsToAvoidDrift =
17 60 * NaCl::Time::kMillisecondsPerSecond;
19 } // namespace
21 // TimeDelta ------------------------------------------------------------------
23 // static
24 NaCl::TimeDelta NaCl::TimeDelta::FromDays(int64 days) {
25 return TimeDelta(days * Time::kMicrosecondsPerDay);
28 // static
29 NaCl::TimeDelta NaCl::TimeDelta::FromHours(int64 hours) {
30 return TimeDelta(hours * Time::kMicrosecondsPerHour);
33 // static
34 NaCl::TimeDelta NaCl::TimeDelta::FromMinutes(int64 minutes) {
35 return TimeDelta(minutes * Time::kMicrosecondsPerMinute);
38 // static
39 NaCl::TimeDelta NaCl::TimeDelta::FromSeconds(int64 secs) {
40 return TimeDelta(secs * Time::kMicrosecondsPerSecond);
43 // static
44 NaCl::TimeDelta NaCl::TimeDelta::FromMilliseconds(int64 ms) {
45 return TimeDelta(ms * Time::kMicrosecondsPerMillisecond);
48 // static
49 NaCl::TimeDelta NaCl::TimeDelta::FromMicroseconds(int64 us) {
50 return TimeDelta(us);
53 int NaCl::TimeDelta::InDays() const {
54 return static_cast<int>(delta_ / Time::kMicrosecondsPerDay);
57 double NaCl::TimeDelta::InSecondsF() const {
58 return static_cast<double>(delta_) / Time::kMicrosecondsPerSecond;
61 int64 NaCl::TimeDelta::InSeconds() const {
62 return delta_ / Time::kMicrosecondsPerSecond;
65 double NaCl::TimeDelta::InMillisecondsF() const {
66 return static_cast<double>(delta_) / Time::kMicrosecondsPerMillisecond;
69 int64 NaCl::TimeDelta::InMilliseconds() const {
70 return delta_ / Time::kMicrosecondsPerMillisecond;
73 int64 NaCl::TimeDelta::InMicroseconds() const {
74 return delta_;
77 // Time -----------------------------------------------------------------------
79 int64 NaCl::Time::initial_time_ = 0;
80 NaCl::TimeTicks NaCl::Time::initial_ticks_;
82 // static
83 void NaCl::Time::InitializeClock() {
84 initial_ticks_ = TimeTicks::Now();
85 initial_time_ = CurrentWallclockMicroseconds();
88 // static
89 NaCl::Time NaCl::Time::Now() {
90 if (initial_time_ == 0)
91 InitializeClock();
93 // We implement time using the high-resolution timers so that we can get
94 // timeouts which are smaller than 10-15ms. If we just used
95 // CurrentWallclockMicroseconds(), we'd have the less-granular timer.
97 // To make this work, we initialize the clock (initial_time) and the
98 // counter (initial_ctr). To compute the initial time, we can check
99 // the number of ticks that have elapsed, and compute the delta.
101 // To avoid any drift, we periodically resync the counters to the system
102 // clock.
103 while (true) {
104 TimeTicks ticks = TimeTicks::Now();
106 // Calculate the time elapsed since we started our timer
107 TimeDelta elapsed = ticks - initial_ticks_;
109 // Check if enough time has elapsed that we need to resync the clock.
110 if (elapsed.InMilliseconds() > kMaxMillisecondsToAvoidDrift) {
111 InitializeClock();
112 continue;
115 return elapsed + initial_time_;
119 // static
120 NaCl::Time NaCl::Time::FromTimeT(time_t tt) {
121 if (tt == 0)
122 return Time(); // Preserve 0 so we can tell it doesn't exist.
123 return (tt * kMicrosecondsPerSecond) + kTimeTToMicrosecondsOffset;
126 time_t NaCl::Time::ToTimeT() const {
127 if (us_ == 0)
128 return 0; // Preserve 0 so we can tell it doesn't exist.
129 return (us_ - kTimeTToMicrosecondsOffset) / kMicrosecondsPerSecond;
132 double NaCl::Time::ToDoubleT() const {
133 if (us_ == 0)
134 return 0; // Preserve 0 so we can tell it doesn't exist.
135 return (static_cast<double>(us_ - kTimeTToMicrosecondsOffset) /
136 static_cast<double>(kMicrosecondsPerSecond));
139 NaCl::Time NaCl::Time::LocalMidnight() const {
140 Exploded exploded;
141 LocalExplode(&exploded);
142 exploded.hour = 0;
143 exploded.minute = 0;
144 exploded.second = 0;
145 exploded.millisecond = 0;
146 return FromLocalExploded(exploded);