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" |
15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
16 | 17 |
17 using base::Time; | 18 using base::Time; |
18 using base::TimeDelta; | 19 using base::TimeDelta; |
19 using base::TimeTicks; | 20 using base::TimeTicks; |
20 using base::TraceTicks; | 21 using base::TraceTicks; |
21 | 22 |
22 namespace { | 23 namespace { |
23 | 24 |
24 class MockTimeTicks : public TimeTicks { | 25 class MockTimeTicks : public TimeTicks { |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 const int kThreads = 8; | 80 const int kThreads = 8; |
80 // Use int64 so we can cast into a void* without a compiler warning. | 81 // Use int64 so we can cast into a void* without a compiler warning. |
81 const int64 kChecks = 10; | 82 const int64 kChecks = 10; |
82 | 83 |
83 // It takes a lot of iterations to reproduce the bug! | 84 // It takes a lot of iterations to reproduce the bug! |
84 // (See bug 1081395) | 85 // (See bug 1081395) |
85 for (int loop = 0; loop < 4096; loop++) { | 86 for (int loop = 0; loop < 4096; loop++) { |
86 // Setup | 87 // Setup |
87 MockTimeTicks::InstallTicker(); | 88 MockTimeTicks::InstallTicker(); |
88 g_rollover_test_start = CreateEvent(0, TRUE, FALSE, 0); | 89 g_rollover_test_start = CreateEvent(0, TRUE, FALSE, 0); |
89 HANDLE threads[kThreads]; | 90 // Since using _beginthreadex() (as opposed to _beginthread), make sure |
| 91 // CloseHandle is called. |
| 92 base::win::ScopedHandle threads[kThreads]; |
90 | 93 |
91 for (int index = 0; index < kThreads; index++) { | 94 for (int index = 0; index < kThreads; index++) { |
92 void* argument = reinterpret_cast<void*>(kChecks); | 95 void* argument = reinterpret_cast<void*>(kChecks); |
93 unsigned thread_id; | 96 unsigned thread_id; |
94 threads[index] = reinterpret_cast<HANDLE>( | 97 threads[index].Set(reinterpret_cast<HANDLE>( |
95 _beginthreadex(NULL, 0, RolloverTestThreadMain, argument, 0, | 98 _beginthreadex(NULL, 0, RolloverTestThreadMain, argument, 0, |
96 &thread_id)); | 99 &thread_id))); |
97 EXPECT_NE((HANDLE)NULL, threads[index]); | 100 EXPECT_EQ(true, threads[index].IsValid()); |
98 } | 101 } |
99 | 102 |
100 // Start! | 103 // Start! |
101 SetEvent(g_rollover_test_start); | 104 SetEvent(g_rollover_test_start); |
102 | 105 |
103 // Wait for threads to finish | 106 // Wait for threads to finish |
104 for (int index = 0; index < kThreads; index++) { | 107 for (int index = 0; index < kThreads; index++) { |
105 DWORD rv = WaitForSingleObject(threads[index], INFINITE); | 108 DWORD rv = WaitForSingleObject(threads[index].Get(), INFINITE); |
106 EXPECT_EQ(rv, WAIT_OBJECT_0); | 109 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 CloseHandle(g_rollover_test_start); | 112 CHECK(::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 |