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