| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/win/wait_chain.h" | 5 #include "base/win/wait_chain.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 void WaitForMutexAcquired() { | 102 void WaitForMutexAcquired() { |
| 103 EXPECT_EQ(WAIT_OBJECT_0, | 103 EXPECT_EQ(WAIT_OBJECT_0, |
| 104 ::WaitForSingleObject(mutex_acquired_event_.Get(), INFINITE)); | 104 ::WaitForSingleObject(mutex_acquired_event_.Get(), INFINITE)); |
| 105 } | 105 } |
| 106 | 106 |
| 107 // Signal the thread to acquire the second mutex. | 107 // Signal the thread to acquire the second mutex. |
| 108 void SignalToAcquireMutex() { EXPECT_TRUE(::SetEvent(wait_event_.Get())); } | 108 void SignalToAcquireMutex() { EXPECT_TRUE(::SetEvent(wait_event_.Get())); } |
| 109 | 109 |
| 110 // Terminates the thread. | 110 // Terminates the thread. |
| 111 bool Terminate() { | 111 bool Terminate() { |
| 112 ScopedHandle thread_handle(::OpenThread(THREAD_TERMINATE, FALSE, GetTid())); | 112 ScopedHandle thread_handle(::OpenThread(THREAD_TERMINATE, FALSE, tid())); |
| 113 return ::TerminateThread(thread_handle.Get(), 0); | 113 return ::TerminateThread(thread_handle.Get(), 0); |
| 114 } | 114 } |
| 115 | 115 |
| 116 private: | 116 private: |
| 117 ScopedHandle wait_event_; | 117 ScopedHandle wait_event_; |
| 118 ScopedHandle mutex_acquired_event_; | 118 ScopedHandle mutex_acquired_event_; |
| 119 | 119 |
| 120 // The 2 mutex to acquire. | 120 // The 2 mutex to acquire. |
| 121 HANDLE mutex_1_; | 121 HANDLE mutex_1_; |
| 122 HANDLE mutex_2_; | 122 HANDLE mutex_2_; |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 254 Sleep(10); | 254 Sleep(10); |
| 255 | 255 |
| 256 // Create a few waiting threads to get a longer wait chain. | 256 // Create a few waiting threads to get a longer wait chain. |
| 257 std::unique_ptr<SingleTaskThread> waiting_thread_1 = | 257 std::unique_ptr<SingleTaskThread> waiting_thread_1 = |
| 258 CreateJoiningThread(deadlock_thread_1.get()); | 258 CreateJoiningThread(deadlock_thread_1.get()); |
| 259 std::unique_ptr<SingleTaskThread> waiting_thread_2 = | 259 std::unique_ptr<SingleTaskThread> waiting_thread_2 = |
| 260 CreateJoiningThread(waiting_thread_1.get()); | 260 CreateJoiningThread(waiting_thread_1.get()); |
| 261 | 261 |
| 262 WaitChainNodeVector wait_chain; | 262 WaitChainNodeVector wait_chain; |
| 263 bool is_deadlock; | 263 bool is_deadlock; |
| 264 ASSERT_TRUE(GetThreadWaitChain(waiting_thread_2->GetTid(), &wait_chain, | 264 ASSERT_TRUE(GetThreadWaitChain(waiting_thread_2->tid(), &wait_chain, |
| 265 &is_deadlock, nullptr, nullptr)); | 265 &is_deadlock, nullptr, nullptr)); |
| 266 | 266 |
| 267 EXPECT_EQ(9U, wait_chain.size()); | 267 EXPECT_EQ(9U, wait_chain.size()); |
| 268 EXPECT_TRUE(is_deadlock); | 268 EXPECT_TRUE(is_deadlock); |
| 269 EXPECT_TRUE(WaitChainStructureIsCorrect(wait_chain)); | 269 EXPECT_TRUE(WaitChainStructureIsCorrect(wait_chain)); |
| 270 EXPECT_FALSE(WaitChainIsCrossProcess(wait_chain)); | 270 EXPECT_FALSE(WaitChainIsCrossProcess(wait_chain)); |
| 271 | 271 |
| 272 ASSERT_TRUE(deadlock_thread_1->Terminate()); | 272 ASSERT_TRUE(deadlock_thread_1->Terminate()); |
| 273 | 273 |
| 274 // The SimpleThread API expect Join() to be called before destruction. | 274 // The SimpleThread API expect Join() to be called before destruction. |
| (...skipping 18 matching lines...) Expand all Loading... |
| 293 // Create a few waiting threads to get a longer wait chain. | 293 // Create a few waiting threads to get a longer wait chain. |
| 294 std::unique_ptr<SingleTaskThread> waiting_thread_1 = | 294 std::unique_ptr<SingleTaskThread> waiting_thread_1 = |
| 295 CreateWaitingThread(mutex.Get()); | 295 CreateWaitingThread(mutex.Get()); |
| 296 std::unique_ptr<SingleTaskThread> waiting_thread_2 = | 296 std::unique_ptr<SingleTaskThread> waiting_thread_2 = |
| 297 CreateJoiningThread(waiting_thread_1.get()); | 297 CreateJoiningThread(waiting_thread_1.get()); |
| 298 std::unique_ptr<SingleTaskThread> waiting_thread_3 = | 298 std::unique_ptr<SingleTaskThread> waiting_thread_3 = |
| 299 CreateJoiningThread(waiting_thread_2.get()); | 299 CreateJoiningThread(waiting_thread_2.get()); |
| 300 | 300 |
| 301 WaitChainNodeVector wait_chain; | 301 WaitChainNodeVector wait_chain; |
| 302 bool is_deadlock; | 302 bool is_deadlock; |
| 303 ASSERT_TRUE(GetThreadWaitChain(waiting_thread_3->GetTid(), &wait_chain, | 303 ASSERT_TRUE(GetThreadWaitChain(waiting_thread_3->tid(), &wait_chain, |
| 304 &is_deadlock, nullptr, nullptr)); | 304 &is_deadlock, nullptr, nullptr)); |
| 305 | 305 |
| 306 EXPECT_EQ(7U, wait_chain.size()); | 306 EXPECT_EQ(7U, wait_chain.size()); |
| 307 EXPECT_FALSE(is_deadlock); | 307 EXPECT_FALSE(is_deadlock); |
| 308 EXPECT_TRUE(WaitChainStructureIsCorrect(wait_chain)); | 308 EXPECT_TRUE(WaitChainStructureIsCorrect(wait_chain)); |
| 309 EXPECT_TRUE(WaitChainIsCrossProcess(wait_chain)); | 309 EXPECT_TRUE(WaitChainIsCrossProcess(wait_chain)); |
| 310 | 310 |
| 311 // Unblock child process and wait for it to terminate. | 311 // Unblock child process and wait for it to terminate. |
| 312 ASSERT_TRUE(::SetEvent(sync_event.Get())); | 312 ASSERT_TRUE(::SetEvent(sync_event.Get())); |
| 313 ASSERT_TRUE(child_process.WaitForExit(nullptr)); | 313 ASSERT_TRUE(child_process.WaitForExit(nullptr)); |
| 314 | 314 |
| 315 // The SimpleThread API expect Join() to be called before destruction. | 315 // The SimpleThread API expect Join() to be called before destruction. |
| 316 waiting_thread_3->Join(); | 316 waiting_thread_3->Join(); |
| 317 } | 317 } |
| 318 | 318 |
| 319 } // namespace win | 319 } // namespace win |
| 320 } // namespace base | 320 } // namespace base |
| OLD | NEW |