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/system/mutex.h" |
6 | 6 |
7 #include <stdlib.h> | 7 #include <stdlib.h> |
8 | 8 |
9 #include "base/threading/platform_thread.h" | 9 #include "base/threading/platform_thread.h" |
10 #include "mojo/edk/system/test_utils.h" | 10 #include "mojo/edk/system/test_utils.h" |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 EpsilonRandomSleep(); | 95 EpsilonRandomSleep(); |
96 mutex.Unlock(); | 96 mutex.Unlock(); |
97 } | 97 } |
98 | 98 |
99 base::PlatformThread::Join(handle); | 99 base::PlatformThread::Join(handle); |
100 | 100 |
101 EXPECT_GE(acquired, 20); | 101 EXPECT_GE(acquired, 20); |
102 EXPECT_GE(thread.acquired(), 20); | 102 EXPECT_GE(thread.acquired(), 20); |
103 } | 103 } |
104 | 104 |
| 105 TEST(MutexTest, AssertHeld) { |
| 106 Mutex mutex; |
| 107 |
| 108 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) |
| 109 // For non-Debug builds, |AssertHeld()| should do nothing. |
| 110 mutex.AssertHeld(); |
| 111 #else |
| 112 EXPECT_DEATH_IF_SUPPORTED({ mutex.AssertHeld(); }, "Check failed"); |
| 113 #endif // defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) |
| 114 |
| 115 // TODO(vtl): Should also test the case when the mutex is held by another |
| 116 // thread, though this is more annoying since it requires synchronization. |
| 117 } |
| 118 |
105 // Test that TryLock() works as expected --------------------------------------- | 119 // Test that TryLock() works as expected --------------------------------------- |
106 | 120 |
107 class TryLockTestThread : public base::PlatformThread::Delegate { | 121 class TryLockTestThread : public base::PlatformThread::Delegate { |
108 public: | 122 public: |
109 explicit TryLockTestThread(Mutex* mutex) : mutex_(mutex), got_lock_(false) {} | 123 explicit TryLockTestThread(Mutex* mutex) : mutex_(mutex), got_lock_(false) {} |
110 | 124 |
111 void ThreadMain() override MOJO_NO_THREAD_SAFETY_ANALYSIS { | 125 void ThreadMain() override MOJO_NO_THREAD_SAFETY_ANALYSIS { |
112 got_lock_ = mutex_->TryLock(); | 126 got_lock_ = mutex_->TryLock(); |
113 if (got_lock_) { | 127 if (got_lock_) { |
114 mutex_->AssertHeld(); | 128 mutex_->AssertHeld(); |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 | 255 |
242 // The destruction of |locker| should unlock |mutex|. | 256 // The destruction of |locker| should unlock |mutex|. |
243 ASSERT_TRUE(mutex.TryLock()); | 257 ASSERT_TRUE(mutex.TryLock()); |
244 mutex.AssertHeld(); | 258 mutex.AssertHeld(); |
245 mutex.Unlock(); | 259 mutex.Unlock(); |
246 } | 260 } |
247 | 261 |
248 } // namespace | 262 } // namespace |
249 } // namespace system | 263 } // namespace system |
250 } // namespace mojo | 264 } // namespace mojo |
OLD | NEW |