| 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/cond_var.h" | 5 #include "mojo/edk/util/cond_var.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 #include <stdlib.h> | 8 #include <stdlib.h> |
| 9 | 9 |
| 10 #include <thread> | 10 #include <thread> |
| 11 #include <type_traits> | 11 #include <type_traits> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "mojo/edk/system/test/sleep.h" | 14 #include "mojo/edk/system/test/sleep.h" |
| 15 #include "mojo/edk/system/test/stopwatch.h" | 15 #include "mojo/edk/system/test/stopwatch.h" |
| 16 #include "mojo/edk/system/test/timeouts.h" | 16 #include "mojo/edk/system/test/timeouts.h" |
| 17 #include "mojo/edk/util/mutex.h" | 17 #include "mojo/edk/util/mutex.h" |
| 18 #include "mojo/public/cpp/system/macros.h" | 18 #include "mojo/public/cpp/system/macros.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 20 |
| 21 using mojo::system::test::DeadlineFromMilliseconds; |
| 22 using mojo::system::test::EpsilonTimeout; |
| 23 using mojo::system::test::SleepMilliseconds; |
| 24 using mojo::system::test::Stopwatch; |
| 25 using mojo::system::test::TinyTimeout; |
| 26 |
| 21 namespace mojo { | 27 namespace mojo { |
| 22 namespace util { | 28 namespace util { |
| 23 namespace { | 29 namespace { |
| 24 | 30 |
| 25 // Sleeps for a "very small" amount of time. | 31 // Sleeps for a "very small" amount of time. |
| 26 void EpsilonRandomSleep() { | 32 void EpsilonRandomSleep() { |
| 27 system::test::SleepMilliseconds(static_cast<unsigned>(rand()) % 20u); | 33 SleepMilliseconds(static_cast<unsigned>(rand()) % 20u); |
| 28 } | 34 } |
| 29 | 35 |
| 30 // We'll use |MojoDeadline| with |uint64_t| (for |CondVar::WaitWithTimeout()|'s | 36 // We'll use |MojoDeadline| with |uint64_t| (for |CondVar::WaitWithTimeout()|'s |
| 31 // timeout argument), though note that |WaitWithTimeout()| doesn't support | 37 // timeout argument), though note that |WaitWithTimeout()| doesn't support |
| 32 // |MOJO_DEADLINE_INDEFINITE|. | 38 // |MOJO_DEADLINE_INDEFINITE|. |
| 33 static_assert(std::is_same<uint64_t, MojoDeadline>::value, | 39 static_assert(std::is_same<uint64_t, MojoDeadline>::value, |
| 34 "MojoDeadline isn't uint64_t!"); | 40 "MojoDeadline isn't uint64_t!"); |
| 35 | 41 |
| 36 TEST(CondVarTest, Basic) { | 42 TEST(CondVarTest, Basic) { |
| 37 // Create/destroy. | 43 // Create/destroy. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 49 Mutex mu; | 55 Mutex mu; |
| 50 CondVar cv; | 56 CondVar cv; |
| 51 | 57 |
| 52 MutexLocker locker(&mu); | 58 MutexLocker locker(&mu); |
| 53 | 59 |
| 54 // Note: Theoretically, pthreads is allowed to wake us up spuriously, in | 60 // Note: Theoretically, pthreads is allowed to wake us up spuriously, in |
| 55 // which case |WaitWithTimeout()| would return false. (This would also | 61 // which case |WaitWithTimeout()| would return false. (This would also |
| 56 // happen if we're interrupted, e.g., by ^Z.) | 62 // happen if we're interrupted, e.g., by ^Z.) |
| 57 EXPECT_TRUE(cv.WaitWithTimeout(&mu, 0)); | 63 EXPECT_TRUE(cv.WaitWithTimeout(&mu, 0)); |
| 58 mu.AssertHeld(); | 64 mu.AssertHeld(); |
| 59 EXPECT_TRUE( | 65 EXPECT_TRUE(cv.WaitWithTimeout(&mu, DeadlineFromMilliseconds(1))); |
| 60 cv.WaitWithTimeout(&mu, system::test::DeadlineFromMilliseconds(1))); | |
| 61 mu.AssertHeld(); | 66 mu.AssertHeld(); |
| 62 } | 67 } |
| 63 | 68 |
| 64 // Wait using |Wait()| or |WaitWithTimeout()|, to be signaled by |Signal()| or | 69 // Wait using |Wait()| or |WaitWithTimeout()|, to be signaled by |Signal()| or |
| 65 // |SignalAll()|. | 70 // |SignalAll()|. |
| 66 for (size_t i = 0; i < 30; i++) { | 71 for (size_t i = 0; i < 30; i++) { |
| 67 Mutex mu; | 72 Mutex mu; |
| 68 CondVar cv; | 73 CondVar cv; |
| 69 bool condition = false; | 74 bool condition = false; |
| 70 | 75 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 82 EpsilonRandomSleep(); | 87 EpsilonRandomSleep(); |
| 83 | 88 |
| 84 MutexLocker locker(&mu); | 89 MutexLocker locker(&mu); |
| 85 if (rand() % 2 == 0) { | 90 if (rand() % 2 == 0) { |
| 86 while (!condition) { | 91 while (!condition) { |
| 87 cv.Wait(&mu); | 92 cv.Wait(&mu); |
| 88 mu.AssertHeld(); | 93 mu.AssertHeld(); |
| 89 } | 94 } |
| 90 } else { | 95 } else { |
| 91 while (!condition) { | 96 while (!condition) { |
| 92 EXPECT_FALSE(cv.WaitWithTimeout(&mu, system::test::TinyTimeout())); | 97 EXPECT_FALSE(cv.WaitWithTimeout(&mu, TinyTimeout())); |
| 93 mu.AssertHeld(); | 98 mu.AssertHeld(); |
| 94 } | 99 } |
| 95 } | 100 } |
| 96 | 101 |
| 97 thread.join(); | 102 thread.join(); |
| 98 } | 103 } |
| 99 } | 104 } |
| 100 | 105 |
| 101 TEST(CondVarTest, SignalAll) { | 106 TEST(CondVarTest, SignalAll) { |
| 102 Mutex mu; | 107 Mutex mu; |
| 103 CondVar cv; | 108 CondVar cv; |
| 104 bool condition = false; | 109 bool condition = false; |
| 105 | 110 |
| 106 for (size_t i = 0; i < 10; i++) { | 111 for (size_t i = 0; i < 10; i++) { |
| 107 for (size_t num_waiters = 1; num_waiters < 5; num_waiters++) { | 112 for (size_t num_waiters = 1; num_waiters < 5; num_waiters++) { |
| 108 std::vector<std::thread> threads; | 113 std::vector<std::thread> threads; |
| 109 for (size_t j = 0; j < num_waiters; j++) { | 114 for (size_t j = 0; j < num_waiters; j++) { |
| 110 threads.push_back(std::thread([&mu, &cv, &condition]() { | 115 threads.push_back(std::thread([&mu, &cv, &condition]() { |
| 111 EpsilonRandomSleep(); | 116 EpsilonRandomSleep(); |
| 112 | 117 |
| 113 MutexLocker locker(&mu); | 118 MutexLocker locker(&mu); |
| 114 if (rand() % 2 == 0) { | 119 if (rand() % 2 == 0) { |
| 115 while (!condition) { | 120 while (!condition) { |
| 116 cv.Wait(&mu); | 121 cv.Wait(&mu); |
| 117 mu.AssertHeld(); | 122 mu.AssertHeld(); |
| 118 } | 123 } |
| 119 } else { | 124 } else { |
| 120 while (!condition) { | 125 while (!condition) { |
| 121 EXPECT_FALSE( | 126 EXPECT_FALSE(cv.WaitWithTimeout(&mu, TinyTimeout())); |
| 122 cv.WaitWithTimeout(&mu, system::test::TinyTimeout())); | |
| 123 mu.AssertHeld(); | 127 mu.AssertHeld(); |
| 124 } | 128 } |
| 125 } | 129 } |
| 126 })); | 130 })); |
| 127 } | 131 } |
| 128 | 132 |
| 129 EpsilonRandomSleep(); | 133 EpsilonRandomSleep(); |
| 130 | 134 |
| 131 { | 135 { |
| 132 MutexLocker locker(&mu); | 136 MutexLocker locker(&mu); |
| 133 condition = true; | 137 condition = true; |
| 134 cv.SignalAll(); | 138 cv.SignalAll(); |
| 135 } | 139 } |
| 136 | 140 |
| 137 for (auto& thread : threads) | 141 for (auto& thread : threads) |
| 138 thread.join(); | 142 thread.join(); |
| 139 } | 143 } |
| 140 } | 144 } |
| 141 } | 145 } |
| 142 | 146 |
| 143 TEST(CondVarTest, Timeouts) { | 147 TEST(CondVarTest, Timeouts) { |
| 144 static const unsigned kTestTimeoutsMs[] = {0, 10, 20, 40, 80, 160}; | 148 static const unsigned kTestTimeoutsMs[] = {0, 10, 20, 40, 80, 160}; |
| 145 | 149 |
| 146 system::test::Stopwatch stopwatch; | 150 Stopwatch stopwatch; |
| 147 | 151 |
| 148 Mutex mu; | 152 Mutex mu; |
| 149 CondVar cv; | 153 CondVar cv; |
| 150 | 154 |
| 151 MutexLocker locker(&mu); | 155 MutexLocker locker(&mu); |
| 152 | 156 |
| 153 for (size_t i = 0; i < MOJO_ARRAYSIZE(kTestTimeoutsMs); i++) { | 157 for (size_t i = 0; i < MOJO_ARRAYSIZE(kTestTimeoutsMs); i++) { |
| 154 uint64_t timeout = | 158 uint64_t timeout = DeadlineFromMilliseconds(kTestTimeoutsMs[i]); |
| 155 system::test::DeadlineFromMilliseconds(kTestTimeoutsMs[i]); | |
| 156 | 159 |
| 157 stopwatch.Start(); | 160 stopwatch.Start(); |
| 158 // See note in CondVarTest.Basic about spurious wakeups. | 161 // See note in CondVarTest.Basic about spurious wakeups. |
| 159 EXPECT_TRUE(cv.WaitWithTimeout(&mu, timeout)); | 162 EXPECT_TRUE(cv.WaitWithTimeout(&mu, timeout)); |
| 160 MojoDeadline elapsed = stopwatch.Elapsed(); | 163 MojoDeadline elapsed = stopwatch.Elapsed(); |
| 161 | 164 |
| 162 // It should time out after *at least* the specified amount of time. | 165 // It should time out after *at least* the specified amount of time. |
| 163 EXPECT_GE(elapsed, timeout); | 166 EXPECT_GE(elapsed, timeout); |
| 164 // But we expect that it should time out soon after that amount of time. | 167 // But we expect that it should time out soon after that amount of time. |
| 165 EXPECT_LT(elapsed, timeout + system::test::EpsilonTimeout()); | 168 EXPECT_LT(elapsed, timeout + EpsilonTimeout()); |
| 166 } | 169 } |
| 167 } | 170 } |
| 168 | 171 |
| 169 // TODO(vtl): Test that |Signal()| (usually) wakes only one waiter. | 172 // TODO(vtl): Test that |Signal()| (usually) wakes only one waiter. |
| 170 | 173 |
| 171 } // namespace | 174 } // namespace |
| 172 } // namespace util | 175 } // namespace util |
| 173 } // namespace mojo | 176 } // namespace mojo |
| OLD | NEW |