Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(123)

Side by Side Diff: base/time/time_mac.cc

Issue 56973012: Implement Time::ThreadNow() on Mac. (Closed) Base URL: https://src.chromium.org/chrome/trunk/src/
Patch Set: exclude OS_IOS from the IsThreadNowSupported check Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/time/time.h" 5 #include "base/time/time.h"
6 6
7 #include <CoreFoundation/CFDate.h> 7 #include <CoreFoundation/CFDate.h>
8 #include <CoreFoundation/CFTimeZone.h> 8 #include <CoreFoundation/CFTimeZone.h>
9 #include <mach/mach.h>
9 #include <mach/mach_time.h> 10 #include <mach/mach_time.h>
10 #include <sys/sysctl.h> 11 #include <sys/sysctl.h>
11 #include <sys/time.h> 12 #include <sys/time.h>
12 #include <sys/types.h> 13 #include <sys/types.h>
13 #include <time.h> 14 #include <time.h>
14 15
15 #include "base/basictypes.h" 16 #include "base/basictypes.h"
16 #include "base/logging.h" 17 #include "base/logging.h"
17 #include "base/mac/scoped_cftyperef.h" 18 #include "base/mac/scoped_cftyperef.h"
19 #include "base/mac/scoped_mach_port.h"
18 20
19 namespace { 21 namespace {
20 22
21 uint64_t ComputeCurrentTicks() { 23 uint64_t ComputeCurrentTicks() {
22 #if defined(OS_IOS) 24 #if defined(OS_IOS)
23 // On iOS mach_absolute_time stops while the device is sleeping. Instead use 25 // On iOS mach_absolute_time stops while the device is sleeping. Instead use
24 // now - KERN_BOOTTIME to get a time difference that is not impacted by clock 26 // now - KERN_BOOTTIME to get a time difference that is not impacted by clock
25 // changes. KERN_BOOTTIME will be updated by the system whenever the system 27 // changes. KERN_BOOTTIME will be updated by the system whenever the system
26 // clock change. 28 // clock change.
27 struct timeval boottime; 29 struct timeval boottime;
(...skipping 29 matching lines...) Expand all
57 mach_absolute_time() / base::Time::kNanosecondsPerMicrosecond * 59 mach_absolute_time() / base::Time::kNanosecondsPerMicrosecond *
58 timebase_info.numer / timebase_info.denom; 60 timebase_info.numer / timebase_info.denom;
59 61
60 // Don't bother with the rollover handling that the Windows version does. 62 // Don't bother with the rollover handling that the Windows version does.
61 // With numer and denom = 1 (the expected case), the 64-bit absolute time 63 // With numer and denom = 1 (the expected case), the 64-bit absolute time
62 // reported in nanoseconds is enough to last nearly 585 years. 64 // reported in nanoseconds is enough to last nearly 585 years.
63 return absolute_micro; 65 return absolute_micro;
64 #endif // defined(OS_IOS) 66 #endif // defined(OS_IOS)
65 } 67 }
66 68
69 uint64_t ComputeThreadTicks() {
70 #if defined(OS_IOS)
71 NOTREACHED();
72 return 0;
73 #else
74 base::mac::ScopedMachPort thread(mach_thread_self());
75 mach_msg_type_number_t thread_info_count = THREAD_BASIC_INFO_COUNT;
76 thread_basic_info_data_t thread_info_data;
77
78 if (thread == MACH_PORT_NULL) {
79 DLOG(ERROR) << "Failed to get mach_thread_self()";
80 return 0;
81 }
82
83 kern_return_t kr = thread_info(
84 thread,
85 THREAD_BASIC_INFO,
86 reinterpret_cast<thread_info_t>(&thread_info_data),
87 &thread_info_count);
88 DCHECK_EQ(KERN_SUCCESS, kr);
89
90 return (thread_info_data.user_time.seconds *
91 base::Time::kMicrosecondsPerSecond) +
92 thread_info_data.user_time.microseconds;
93 #endif // defined(OS_IOS)
94 }
95
67 } // namespace 96 } // namespace
68 97
69 namespace base { 98 namespace base {
70 99
71 // The Time routines in this file use Mach and CoreFoundation APIs, since the 100 // The Time routines in this file use Mach and CoreFoundation APIs, since the
72 // POSIX definition of time_t in Mac OS X wraps around after 2038--and 101 // POSIX definition of time_t in Mac OS X wraps around after 2038--and
73 // there are already cookie expiration dates, etc., past that time out in 102 // there are already cookie expiration dates, etc., past that time out in
74 // the field. Using CFDate prevents that problem, and using mach_absolute_time 103 // the field. Using CFDate prevents that problem, and using mach_absolute_time
75 // for TimeTicks gives us nice high-resolution interval timing. 104 // for TimeTicks gives us nice high-resolution interval timing.
76 105
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
189 return Now(); 218 return Now();
190 } 219 }
191 220
192 // static 221 // static
193 bool TimeTicks::IsHighResNowFastAndReliable() { 222 bool TimeTicks::IsHighResNowFastAndReliable() {
194 return true; 223 return true;
195 } 224 }
196 225
197 // static 226 // static
198 TimeTicks TimeTicks::ThreadNow() { 227 TimeTicks TimeTicks::ThreadNow() {
199 NOTREACHED(); 228 return TimeTicks(ComputeThreadTicks());
200 return TimeTicks();
201 } 229 }
202 230
203 // static 231 // static
204 TimeTicks TimeTicks::NowFromSystemTraceTime() { 232 TimeTicks TimeTicks::NowFromSystemTraceTime() {
205 return HighResNow(); 233 return HighResNow();
206 } 234 }
207 235
208 } // namespace base 236 } // namespace base
OLDNEW
« no previous file with comments | « base/time/time.h ('k') | base/time/time_unittest.cc » ('j') | base/time/time_unittest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698