| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "mojo/edk/util/mutex.h" | 5 #include "mojo/edk/util/mutex.h" |
| 6 | 6 |
| 7 #include <stdlib.h> | 7 #include <stdlib.h> |
| 8 | 8 |
| 9 #include <thread> | 9 #include <thread> |
| 10 | 10 |
| 11 #include "mojo/edk/system/test/sleep.h" | 11 #include "mojo/edk/system/test/sleep.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 13 |
| 14 using mojo::system::test::SleepMilliseconds; | |
| 15 | |
| 16 namespace mojo { | 14 namespace mojo { |
| 17 namespace util { | 15 namespace util { |
| 18 namespace { | 16 namespace { |
| 19 | 17 |
| 20 // Sleeps for a "very small" amount of time. | 18 // Sleeps for a "very small" amount of time. |
| 21 void EpsilonRandomSleep() { | 19 void EpsilonRandomSleep() { |
| 22 SleepMilliseconds(static_cast<unsigned>(rand()) % 20u); | 20 system::test::SleepMilliseconds(static_cast<unsigned>(rand()) % 20u); |
| 23 } | 21 } |
| 24 | 22 |
| 25 // Basic test to make sure that Lock()/Unlock()/TryLock() don't crash ---------- | 23 // Basic test to make sure that Lock()/Unlock()/TryLock() don't crash ---------- |
| 26 | 24 |
| 27 TEST(MutexTest, Basic) { | 25 TEST(MutexTest, Basic) { |
| 28 Mutex mutex; | 26 Mutex mutex; |
| 29 | 27 |
| 30 int thread_acquired = 0; | 28 int thread_acquired = 0; |
| 31 auto thread = std::thread([&mutex, &thread_acquired]() { | 29 auto thread = std::thread([&mutex, &thread_acquired]() { |
| 32 for (int i = 0; i < 10; i++) { | 30 for (int i = 0; i < 10; i++) { |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 | 192 |
| 195 // The destruction of |locker| should unlock |mutex|. | 193 // The destruction of |locker| should unlock |mutex|. |
| 196 ASSERT_TRUE(mutex.TryLock()); | 194 ASSERT_TRUE(mutex.TryLock()); |
| 197 mutex.AssertHeld(); | 195 mutex.AssertHeld(); |
| 198 mutex.Unlock(); | 196 mutex.Unlock(); |
| 199 } | 197 } |
| 200 | 198 |
| 201 } // namespace | 199 } // namespace |
| 202 } // namespace util | 200 } // namespace util |
| 203 } // namespace mojo | 201 } // namespace mojo |
| OLD | NEW |