| 63 | }; |
| 64 | |
| 65 | void WaitTest(const Duration& spin_duration, const Duration& delay) { |
| 66 | std::condition_variable condvar; |
| 67 | std::mutex mutex; |
| 68 | std::atomic<int> value(0); |
| 69 | std::atomic<int> end_value(0); |
| 70 | ThreadCountingUpToValue thread_callable(end_value, &value, &condvar, &mutex); |
| 71 | std::thread thread(thread_callable); |
| 72 | std::this_thread::sleep_for(delay); |
| 73 | for (int i = 1; i < 10; i++) { |
| 74 | end_value.store(1000 * i); |
| 75 | const auto& condition = [&value, &end_value]() { |
| 76 | return value.load() == end_value.load(); |
| 77 | }; |
| 78 | ruy::WaitUntil(condition, spin_duration, &condvar, &mutex); |
| 79 | EXPECT_EQ(value.load(), end_value.load()); |
| 80 | } |
| 81 | end_value.store(-1); |
| 82 | thread.join(); |
| 83 | } |
| 84 | |
| 85 | TEST(WaitTest, WaitTestNoSpin) { |
| 86 | WaitTest(DurationFromSeconds(0), DurationFromSeconds(0)); |