OLD | NEW |
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 <windows.h> | 5 #include <windows.h> |
6 #include <mmsystem.h> | 6 #include <mmsystem.h> |
7 #include <process.h> | 7 #include <process.h> |
8 | 8 |
9 #include <cmath> | 9 #include <cmath> |
10 #include <limits> | 10 #include <limits> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/threading/platform_thread.h" | 13 #include "base/threading/platform_thread.h" |
14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
15 #include "base/win/scoped_handle.h" | |
16 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
17 | 16 |
18 using base::Time; | 17 using base::Time; |
19 using base::TimeDelta; | 18 using base::TimeDelta; |
20 using base::TimeTicks; | 19 using base::TimeTicks; |
21 using base::TraceTicks; | 20 using base::TraceTicks; |
22 | 21 |
23 namespace { | 22 namespace { |
24 | 23 |
25 class MockTimeTicks : public TimeTicks { | 24 class MockTimeTicks : public TimeTicks { |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 const int kThreads = 8; | 79 const int kThreads = 8; |
81 // Use int64 so we can cast into a void* without a compiler warning. | 80 // Use int64 so we can cast into a void* without a compiler warning. |
82 const int64 kChecks = 10; | 81 const int64 kChecks = 10; |
83 | 82 |
84 // It takes a lot of iterations to reproduce the bug! | 83 // It takes a lot of iterations to reproduce the bug! |
85 // (See bug 1081395) | 84 // (See bug 1081395) |
86 for (int loop = 0; loop < 4096; loop++) { | 85 for (int loop = 0; loop < 4096; loop++) { |
87 // Setup | 86 // Setup |
88 MockTimeTicks::InstallTicker(); | 87 MockTimeTicks::InstallTicker(); |
89 g_rollover_test_start = CreateEvent(0, TRUE, FALSE, 0); | 88 g_rollover_test_start = CreateEvent(0, TRUE, FALSE, 0); |
90 // Since using _beginthreadex() (as opposed to _beginthread), make sure | 89 HANDLE threads[kThreads]; |
91 // CloseHandle is called. | |
92 base::win::ScopedHandle threads[kThreads]; | |
93 | 90 |
94 for (int index = 0; index < kThreads; index++) { | 91 for (int index = 0; index < kThreads; index++) { |
95 void* argument = reinterpret_cast<void*>(kChecks); | 92 void* argument = reinterpret_cast<void*>(kChecks); |
96 unsigned thread_id; | 93 unsigned thread_id; |
97 threads[index].Set(reinterpret_cast<HANDLE>( | 94 threads[index] = reinterpret_cast<HANDLE>( |
98 _beginthreadex(NULL, 0, RolloverTestThreadMain, argument, 0, | 95 _beginthreadex(NULL, 0, RolloverTestThreadMain, argument, 0, |
99 &thread_id))); | 96 &thread_id)); |
100 EXPECT_EQ(true, threads[index].IsValid()); | 97 EXPECT_NE((HANDLE)NULL, threads[index]); |
101 } | 98 } |
102 | 99 |
103 // Start! | 100 // Start! |
104 SetEvent(g_rollover_test_start); | 101 SetEvent(g_rollover_test_start); |
105 | 102 |
106 // Wait for threads to finish | 103 // Wait for threads to finish |
107 for (int index = 0; index < kThreads; index++) { | 104 for (int index = 0; index < kThreads; index++) { |
108 DWORD rv = WaitForSingleObject(threads[index].Get(), INFINITE); | 105 DWORD rv = WaitForSingleObject(threads[index], INFINITE); |
109 EXPECT_EQ(rv, WAIT_OBJECT_0); | 106 EXPECT_EQ(rv, WAIT_OBJECT_0); |
| 107 // Since using _beginthreadex() (as opposed to _beginthread), |
| 108 // an explicit CloseHandle() is supposed to be called. |
| 109 CloseHandle(threads[index]); |
110 } | 110 } |
111 | 111 |
112 CHECK(::CloseHandle(g_rollover_test_start)); | 112 CloseHandle(g_rollover_test_start); |
113 | 113 |
114 // Teardown | 114 // Teardown |
115 MockTimeTicks::UninstallTicker(); | 115 MockTimeTicks::UninstallTicker(); |
116 } | 116 } |
117 } | 117 } |
118 | 118 |
119 TEST(TimeTicks, SubMillisecondTimers) { | 119 TEST(TimeTicks, SubMillisecondTimers) { |
120 // IsHighResolution() is false on some systems. Since the product still works | 120 // IsHighResolution() is false on some systems. Since the product still works |
121 // even if it's false, it makes this entire test questionable. | 121 // even if it's false, it makes this entire test questionable. |
122 if (!TimeTicks::IsHighResolution()) | 122 if (!TimeTicks::IsHighResolution()) |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
259 const TimeTicks converted_value = TimeTicks::FromQPCValue(ticks); | 259 const TimeTicks converted_value = TimeTicks::FromQPCValue(ticks); |
260 const double converted_microseconds_since_origin = | 260 const double converted_microseconds_since_origin = |
261 static_cast<double>((converted_value - TimeTicks()).InMicroseconds()); | 261 static_cast<double>((converted_value - TimeTicks()).InMicroseconds()); |
262 EXPECT_NEAR(expected_microseconds_since_origin, | 262 EXPECT_NEAR(expected_microseconds_since_origin, |
263 converted_microseconds_since_origin, | 263 converted_microseconds_since_origin, |
264 1.0) | 264 1.0) |
265 << "ticks=" << ticks << ", to be converted via logic path: " | 265 << "ticks=" << ticks << ", to be converted via logic path: " |
266 << (ticks < Time::kQPCOverflowThreshold ? "FAST" : "SAFE"); | 266 << (ticks < Time::kQPCOverflowThreshold ? "FAST" : "SAFE"); |
267 } | 267 } |
268 } | 268 } |
OLD | NEW |