| 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/waitable_event.h" | 5 #include "mojo/edk/util/waitable_event.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 #include <stdlib.h> | 8 #include <stdlib.h> |
| 9 | 9 |
| 10 #include <atomic> | 10 #include <atomic> |
| 11 #include <thread> | 11 #include <thread> |
| 12 #include <type_traits> | 12 #include <type_traits> |
| 13 #include <vector> | 13 #include <vector> |
| 14 | 14 |
| 15 #include "mojo/edk/system/test/sleep.h" | 15 #include "mojo/edk/platform/thread_utils.h" |
| 16 #include "mojo/edk/system/test/stopwatch.h" | 16 #include "mojo/edk/system/test/stopwatch.h" |
| 17 #include "mojo/edk/system/test/timeouts.h" | 17 #include "mojo/edk/system/test/timeouts.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::platform::ThreadSleep; |
| 21 using mojo::system::test::ActionTimeout; | 22 using mojo::system::test::ActionTimeout; |
| 22 using mojo::system::test::DeadlineFromMilliseconds; | 23 using mojo::system::test::DeadlineFromMilliseconds; |
| 23 using mojo::system::test::EpsilonTimeout; | 24 using mojo::system::test::EpsilonTimeout; |
| 24 using mojo::system::test::Sleep; | |
| 25 using mojo::system::test::SleepMilliseconds; | |
| 26 using mojo::system::test::Stopwatch; | 25 using mojo::system::test::Stopwatch; |
| 27 using mojo::system::test::TinyTimeout; | 26 using mojo::system::test::TinyTimeout; |
| 28 | 27 |
| 29 namespace mojo { | 28 namespace mojo { |
| 30 namespace util { | 29 namespace util { |
| 31 namespace { | 30 namespace { |
| 32 | 31 |
| 33 // Sleeps for a "very small" amount of time. | 32 // Sleeps for a "very small" amount of time. |
| 34 void EpsilonRandomSleep() { | 33 void EpsilonRandomSleep() { |
| 35 SleepMilliseconds(static_cast<unsigned>(rand()) % 20u); | 34 ThreadSleep(DeadlineFromMilliseconds(static_cast<unsigned>(rand()) % 20u)); |
| 36 } | 35 } |
| 37 | 36 |
| 38 // We'll use |MojoDeadline| with |uint64_t| (for |WaitWithTimeout()|'s timeout | 37 // We'll use |MojoDeadline| with |uint64_t| (for |WaitWithTimeout()|'s timeout |
| 39 // argument), though note that |WaitWithTimeout()| doesn't support | 38 // argument), though note that |WaitWithTimeout()| doesn't support |
| 40 // |MOJO_DEADLINE_INDEFINITE|. | 39 // |MOJO_DEADLINE_INDEFINITE|. |
| 41 static_assert(std::is_same<uint64_t, MojoDeadline>::value, | 40 static_assert(std::is_same<uint64_t, MojoDeadline>::value, |
| 42 "MojoDeadline isn't uint64_t!"); | 41 "MojoDeadline isn't uint64_t!"); |
| 43 | 42 |
| 44 // AutoResetWaitableEvent ------------------------------------------------------ | 43 // AutoResetWaitableEvent ------------------------------------------------------ |
| 45 | 44 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 EXPECT_FALSE(ev.WaitWithTimeout(ActionTimeout())); | 84 EXPECT_FALSE(ev.WaitWithTimeout(ActionTimeout())); |
| 86 wake_count.fetch_add(1u); | 85 wake_count.fetch_add(1u); |
| 87 // Note: We can't say anything about the signaled state of |ev| here, | 86 // Note: We can't say anything about the signaled state of |ev| here, |
| 88 // since the main thread may have already signaled it again. | 87 // since the main thread may have already signaled it again. |
| 89 })); | 88 })); |
| 90 } | 89 } |
| 91 | 90 |
| 92 // Unfortunately, we can't really wait for the threads to be waiting, so we | 91 // Unfortunately, we can't really wait for the threads to be waiting, so we |
| 93 // just sleep for a bit, and count on them having started and advanced to | 92 // just sleep for a bit, and count on them having started and advanced to |
| 94 // waiting. | 93 // waiting. |
| 95 Sleep(2 * TinyTimeout()); | 94 ThreadSleep(2 * TinyTimeout()); |
| 96 | 95 |
| 97 for (size_t j = 0u; j < threads.size(); j++) { | 96 for (size_t j = 0u; j < threads.size(); j++) { |
| 98 unsigned old_wake_count = wake_count.load(); | 97 unsigned old_wake_count = wake_count.load(); |
| 99 EXPECT_EQ(j, old_wake_count); | 98 EXPECT_EQ(j, old_wake_count); |
| 100 | 99 |
| 101 // Each |Signal()| should wake exactly one thread. | 100 // Each |Signal()| should wake exactly one thread. |
| 102 ev.Signal(); | 101 ev.Signal(); |
| 103 | 102 |
| 104 // Poll for |wake_count| to change. | 103 // Poll for |wake_count| to change. |
| 105 while (wake_count.load() == old_wake_count) | 104 while (wake_count.load() == old_wake_count) |
| 106 Sleep(EpsilonTimeout()); | 105 ThreadSleep(EpsilonTimeout()); |
| 107 | 106 |
| 108 EXPECT_FALSE(ev.IsSignaledForTest()); | 107 EXPECT_FALSE(ev.IsSignaledForTest()); |
| 109 | 108 |
| 110 // And once it's changed, wait a little longer, to see if any other | 109 // And once it's changed, wait a little longer, to see if any other |
| 111 // threads are awoken (they shouldn't be). | 110 // threads are awoken (they shouldn't be). |
| 112 Sleep(EpsilonTimeout()); | 111 ThreadSleep(EpsilonTimeout()); |
| 113 | 112 |
| 114 EXPECT_EQ(old_wake_count + 1u, wake_count.load()); | 113 EXPECT_EQ(old_wake_count + 1u, wake_count.load()); |
| 115 | 114 |
| 116 EXPECT_FALSE(ev.IsSignaledForTest()); | 115 EXPECT_FALSE(ev.IsSignaledForTest()); |
| 117 } | 116 } |
| 118 | 117 |
| 119 // Having done that, if we signal |ev| now, it should stay signaled. | 118 // Having done that, if we signal |ev| now, it should stay signaled. |
| 120 ev.Signal(); | 119 ev.Signal(); |
| 121 Sleep(EpsilonTimeout()); | 120 ThreadSleep(EpsilonTimeout()); |
| 122 EXPECT_TRUE(ev.IsSignaledForTest()); | 121 EXPECT_TRUE(ev.IsSignaledForTest()); |
| 123 | 122 |
| 124 for (auto& thread : threads) | 123 for (auto& thread : threads) |
| 125 thread.join(); | 124 thread.join(); |
| 126 | 125 |
| 127 ev.Reset(); | 126 ev.Reset(); |
| 128 } | 127 } |
| 129 } | 128 } |
| 130 | 129 |
| 131 TEST(AutoResetWaitableEventTest, Timeouts) { | 130 TEST(AutoResetWaitableEventTest, Timeouts) { |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 ev.Wait(); | 215 ev.Wait(); |
| 217 else | 216 else |
| 218 EXPECT_FALSE(ev.WaitWithTimeout(ActionTimeout())); | 217 EXPECT_FALSE(ev.WaitWithTimeout(ActionTimeout())); |
| 219 ev.Reset(); | 218 ev.Reset(); |
| 220 })); | 219 })); |
| 221 } | 220 } |
| 222 | 221 |
| 223 // Unfortunately, we can't really wait for the threads to be waiting, so we | 222 // Unfortunately, we can't really wait for the threads to be waiting, so we |
| 224 // just sleep for a bit, and count on them having started and advanced to | 223 // just sleep for a bit, and count on them having started and advanced to |
| 225 // waiting. | 224 // waiting. |
| 226 Sleep(2 * TinyTimeout()); | 225 ThreadSleep(2 * TinyTimeout()); |
| 227 | 226 |
| 228 ev.Signal(); | 227 ev.Signal(); |
| 229 | 228 |
| 230 // In fact, we may ourselves call |Reset()| immediately. | 229 // In fact, we may ourselves call |Reset()| immediately. |
| 231 ev.Reset(); | 230 ev.Reset(); |
| 232 | 231 |
| 233 // The threads will only terminate once they've successfully waited (or | 232 // The threads will only terminate once they've successfully waited (or |
| 234 // timed out). | 233 // timed out). |
| 235 for (auto& thread : threads) | 234 for (auto& thread : threads) |
| 236 thread.join(); | 235 thread.join(); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 256 // It should time out after *at least* the specified amount of time. | 255 // It should time out after *at least* the specified amount of time. |
| 257 EXPECT_GE(elapsed, timeout); | 256 EXPECT_GE(elapsed, timeout); |
| 258 // But we expect that it should time out soon after that amount of time. | 257 // But we expect that it should time out soon after that amount of time. |
| 259 EXPECT_LT(elapsed, timeout + EpsilonTimeout()); | 258 EXPECT_LT(elapsed, timeout + EpsilonTimeout()); |
| 260 } | 259 } |
| 261 } | 260 } |
| 262 | 261 |
| 263 } // namespace | 262 } // namespace |
| 264 } // namespace util | 263 } // namespace util |
| 265 } // namespace mojo | 264 } // namespace mojo |
| OLD | NEW |