| 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 "build/build_config.h" | 11 #include "build/build_config.h" |
| 12 #include "mojo/edk/system/test/sleep.h" | 12 #include "mojo/edk/platform/thread_utils.h" |
| 13 #include "mojo/edk/system/test/timeouts.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 15 |
| 15 using mojo::system::test::SleepMilliseconds; | 16 using mojo::platform::ThreadSleep; |
| 17 using mojo::system::test::DeadlineFromMilliseconds; |
| 16 | 18 |
| 17 namespace mojo { | 19 namespace mojo { |
| 18 namespace util { | 20 namespace util { |
| 19 namespace { | 21 namespace { |
| 20 | 22 |
| 21 // Sleeps for a "very small" amount of time. | 23 // Sleeps for a "very small" amount of time. |
| 22 void EpsilonRandomSleep() { | 24 void EpsilonRandomSleep() { |
| 23 SleepMilliseconds(static_cast<unsigned>(rand()) % 20u); | 25 ThreadSleep(DeadlineFromMilliseconds(static_cast<unsigned>(rand()) % 20u)); |
| 24 } | 26 } |
| 25 | 27 |
| 26 // Basic test to make sure that Lock()/Unlock()/TryLock() don't crash ---------- | 28 // Basic test to make sure that Lock()/Unlock()/TryLock() don't crash ---------- |
| 27 | 29 |
| 28 TEST(MutexTest, Basic) { | 30 TEST(MutexTest, Basic) { |
| 29 Mutex mutex; | 31 Mutex mutex; |
| 30 | 32 |
| 31 int thread_acquired = 0; | 33 int thread_acquired = 0; |
| 32 auto thread = std::thread([&mutex, &thread_acquired]() { | 34 auto thread = std::thread([&mutex, &thread_acquired]() { |
| 33 for (int i = 0; i < 10; i++) { | 35 for (int i = 0; i < 10; i++) { |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 | 204 |
| 203 // The destruction of |locker| should unlock |mutex|. | 205 // The destruction of |locker| should unlock |mutex|. |
| 204 ASSERT_TRUE(mutex.TryLock()); | 206 ASSERT_TRUE(mutex.TryLock()); |
| 205 mutex.AssertHeld(); | 207 mutex.AssertHeld(); |
| 206 mutex.Unlock(); | 208 mutex.Unlock(); |
| 207 } | 209 } |
| 208 | 210 |
| 209 } // namespace | 211 } // namespace |
| 210 } // namespace util | 212 } // namespace util |
| 211 } // namespace mojo | 213 } // namespace mojo |
| OLD | NEW |