| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 bool LockUnlockThenStopRunFunction(void* obj) { | 71 bool LockUnlockThenStopRunFunction(void* obj) { |
| 72 ProtectedCount* the_count = static_cast<ProtectedCount*>(obj); | 72 ProtectedCount* the_count = static_cast<ProtectedCount*>(obj); |
| 73 the_count->Increment(); | 73 the_count->Increment(); |
| 74 return false; | 74 return false; |
| 75 } | 75 } |
| 76 | 76 |
| 77 TEST_F(CritSectTest, ThreadWakesOnce) NO_THREAD_SAFETY_ANALYSIS { | 77 TEST_F(CritSectTest, ThreadWakesOnce) NO_THREAD_SAFETY_ANALYSIS { |
| 78 CriticalSectionWrapper* crit_sect = | 78 CriticalSectionWrapper* crit_sect = |
| 79 CriticalSectionWrapper::CreateCriticalSection(); | 79 CriticalSectionWrapper::CreateCriticalSection(); |
| 80 ProtectedCount count(crit_sect); | 80 ProtectedCount count(crit_sect); |
| 81 rtc::scoped_ptr<PlatformThread> thread = PlatformThread::CreateThread( | 81 rtc::PlatformThread thread( |
| 82 &LockUnlockThenStopRunFunction, &count, "ThreadWakesOnce"); | 82 &LockUnlockThenStopRunFunction, &count, "ThreadWakesOnce"); |
| 83 crit_sect->Enter(); | 83 crit_sect->Enter(); |
| 84 ASSERT_TRUE(thread->Start()); | 84 thread.Start(); |
| 85 SwitchProcess(); | 85 SwitchProcess(); |
| 86 // The critical section is of reentrant mode, so this should not release | 86 // The critical section is of reentrant mode, so this should not release |
| 87 // the lock, even though count.Count() locks and unlocks the critical section | 87 // the lock, even though count.Count() locks and unlocks the critical section |
| 88 // again. | 88 // again. |
| 89 // Thus, the thread should not be able to increment the count | 89 // Thus, the thread should not be able to increment the count |
| 90 ASSERT_EQ(0, count.Count()); | 90 ASSERT_EQ(0, count.Count()); |
| 91 crit_sect->Leave(); // This frees the thread to act. | 91 crit_sect->Leave(); // This frees the thread to act. |
| 92 EXPECT_TRUE(WaitForCount(1, &count)); | 92 EXPECT_TRUE(WaitForCount(1, &count)); |
| 93 EXPECT_TRUE(thread->Stop()); | 93 thread.Stop(); |
| 94 delete crit_sect; | 94 delete crit_sect; |
| 95 } | 95 } |
| 96 | 96 |
| 97 bool LockUnlockRunFunction(void* obj) { | 97 bool LockUnlockRunFunction(void* obj) { |
| 98 ProtectedCount* the_count = static_cast<ProtectedCount*>(obj); | 98 ProtectedCount* the_count = static_cast<ProtectedCount*>(obj); |
| 99 the_count->Increment(); | 99 the_count->Increment(); |
| 100 SwitchProcess(); | 100 SwitchProcess(); |
| 101 return true; | 101 return true; |
| 102 } | 102 } |
| 103 | 103 |
| 104 TEST_F(CritSectTest, ThreadWakesTwice) NO_THREAD_SAFETY_ANALYSIS { | 104 TEST_F(CritSectTest, ThreadWakesTwice) NO_THREAD_SAFETY_ANALYSIS { |
| 105 CriticalSectionWrapper* crit_sect = | 105 CriticalSectionWrapper* crit_sect = |
| 106 CriticalSectionWrapper::CreateCriticalSection(); | 106 CriticalSectionWrapper::CreateCriticalSection(); |
| 107 ProtectedCount count(crit_sect); | 107 ProtectedCount count(crit_sect); |
| 108 rtc::scoped_ptr<PlatformThread> thread = PlatformThread::CreateThread( | 108 rtc::PlatformThread thread( |
| 109 &LockUnlockRunFunction, &count, "ThreadWakesTwice"); | 109 &LockUnlockRunFunction, &count, "ThreadWakesTwice"); |
| 110 crit_sect->Enter(); // Make sure counter stays 0 until we wait for it. | 110 crit_sect->Enter(); // Make sure counter stays 0 until we wait for it. |
| 111 ASSERT_TRUE(thread->Start()); | 111 thread.Start(); |
| 112 crit_sect->Leave(); | 112 crit_sect->Leave(); |
| 113 | 113 |
| 114 // The thread is capable of grabbing the lock multiple times, | 114 // The thread is capable of grabbing the lock multiple times, |
| 115 // incrementing counter once each time. | 115 // incrementing counter once each time. |
| 116 // It's possible for the count to be incremented by more than 2. | 116 // It's possible for the count to be incremented by more than 2. |
| 117 EXPECT_TRUE(WaitForCount(2, &count)); | 117 EXPECT_TRUE(WaitForCount(2, &count)); |
| 118 EXPECT_LE(2, count.Count()); | 118 EXPECT_LE(2, count.Count()); |
| 119 | 119 |
| 120 // The thread does not increment while lock is held. | 120 // The thread does not increment while lock is held. |
| 121 crit_sect->Enter(); | 121 crit_sect->Enter(); |
| 122 int count_before = count.Count(); | 122 int count_before = count.Count(); |
| 123 for (int i = 0; i < 10; i++) { | 123 for (int i = 0; i < 10; i++) { |
| 124 SwitchProcess(); | 124 SwitchProcess(); |
| 125 } | 125 } |
| 126 EXPECT_EQ(count_before, count.Count()); | 126 EXPECT_EQ(count_before, count.Count()); |
| 127 crit_sect->Leave(); | 127 crit_sect->Leave(); |
| 128 | 128 |
| 129 SwitchProcess(); | 129 SwitchProcess(); |
| 130 EXPECT_TRUE(WaitForCount(count_before + 1, &count)); | 130 EXPECT_TRUE(WaitForCount(count_before + 1, &count)); |
| 131 EXPECT_TRUE(thread->Stop()); | 131 thread.Stop(); |
| 132 delete crit_sect; | 132 delete crit_sect; |
| 133 } | 133 } |
| 134 | 134 |
| 135 } // anonymous namespace | 135 } // anonymous namespace |
| 136 | 136 |
| 137 } // namespace webrtc | 137 } // namespace webrtc |
| OLD | NEW |