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