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/system/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 namespace mojo { | 14 namespace mojo { |
15 namespace system { | 15 namespace util { |
16 namespace { | 16 namespace { |
17 | 17 |
18 // Sleeps for a "very small" amount of time. | 18 // Sleeps for a "very small" amount of time. |
19 void EpsilonRandomSleep() { | 19 void EpsilonRandomSleep() { |
20 test::SleepMilliseconds(static_cast<unsigned>(rand()) % 20u); | 20 system::test::SleepMilliseconds(static_cast<unsigned>(rand()) % 20u); |
21 } | 21 } |
22 | 22 |
23 // 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 ---------- |
24 | 24 |
25 TEST(MutexTest, Basic) { | 25 TEST(MutexTest, Basic) { |
26 Mutex mutex; | 26 Mutex mutex; |
27 | 27 |
28 int thread_acquired = 0; | 28 int thread_acquired = 0; |
29 auto thread = std::thread([&mutex, &thread_acquired]() { | 29 auto thread = std::thread([&mutex, &thread_acquired]() { |
30 for (int i = 0; i < 10; i++) { | 30 for (int i = 0; i < 10; i++) { |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 EXPECT_GE(thread_acquired, 20); | 86 EXPECT_GE(thread_acquired, 20); |
87 } | 87 } |
88 | 88 |
89 TEST(MutexTest, AssertHeld) { | 89 TEST(MutexTest, AssertHeld) { |
90 Mutex mutex; | 90 Mutex mutex; |
91 | 91 |
92 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) | 92 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) |
93 // For non-Debug builds, |AssertHeld()| should do nothing. | 93 // For non-Debug builds, |AssertHeld()| should do nothing. |
94 mutex.AssertHeld(); | 94 mutex.AssertHeld(); |
95 #else | 95 #else |
96 EXPECT_DEATH_IF_SUPPORTED({ mutex.AssertHeld(); }, "Check failed"); | 96 EXPECT_DEATH_IF_SUPPORTED({ mutex.AssertHeld(); }, "pthread_mutex_lock"); |
97 #endif // defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) | 97 #endif // defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) |
98 | 98 |
99 // TODO(vtl): Should also test the case when the mutex is held by another | 99 // TODO(vtl): Should also test the case when the mutex is held by another |
100 // thread, though this is more annoying since it requires synchronization. | 100 // thread, though this is more annoying since it requires synchronization. |
101 } | 101 } |
102 | 102 |
103 // Test that TryLock() works as expected --------------------------------------- | 103 // Test that TryLock() works as expected --------------------------------------- |
104 | 104 |
105 TEST(MutexTest, TryLock) MOJO_NO_THREAD_SAFETY_ANALYSIS { | 105 TEST(MutexTest, TryLock) MOJO_NO_THREAD_SAFETY_ANALYSIS { |
106 Mutex mutex; | 106 Mutex mutex; |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 mutex.AssertHeld(); | 190 mutex.AssertHeld(); |
191 } | 191 } |
192 | 192 |
193 // The destruction of |locker| should unlock |mutex|. | 193 // The destruction of |locker| should unlock |mutex|. |
194 ASSERT_TRUE(mutex.TryLock()); | 194 ASSERT_TRUE(mutex.TryLock()); |
195 mutex.AssertHeld(); | 195 mutex.AssertHeld(); |
196 mutex.Unlock(); | 196 mutex.Unlock(); |
197 } | 197 } |
198 | 198 |
199 } // namespace | 199 } // namespace |
200 } // namespace system | 200 } // namespace util |
201 } // namespace mojo | 201 } // namespace mojo |
OLD | NEW |