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