|
| 1 | +//===--- ConditionVariable.cpp - ConditionVariable Tests ------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2022 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include "swift/Threading/ConditionVariable.h" |
| 14 | +#include "gtest/gtest.h" |
| 15 | + |
| 16 | +#include <chrono> |
| 17 | +#include <vector> |
| 18 | + |
| 19 | +#include "ThreadingHelpers.h" |
| 20 | +#include "LockingHelpers.h" |
| 21 | + |
| 22 | +using namespace swift; |
| 23 | + |
| 24 | +template <typename Body> |
| 25 | +std::chrono::duration<double> measureDuration(Body body) { |
| 26 | + auto start = std::chrono::steady_clock::now(); |
| 27 | + body(); |
| 28 | + return std::chrono::steady_clock::now() - start; |
| 29 | +} |
| 30 | + |
| 31 | +// ----------------------------------------------------------------------------- |
| 32 | + |
| 33 | +// Check we can lock, unlock, lock and unlock an unlocked condition variable |
| 34 | +TEST(ConditionVariableTest, BasicLockable) { |
| 35 | + ConditionVariable cond; |
| 36 | + basicLockable(cond); |
| 37 | +} |
| 38 | + |
| 39 | +// Test ScopedLock and ScopedUnlock |
| 40 | +TEST(ConditionVariableTest, ScopedLockThreaded) { |
| 41 | + ConditionVariable cond; |
| 42 | + scopedLockThreaded<ConditionVariable::ScopedLock>(cond); |
| 43 | +} |
| 44 | + |
| 45 | +TEST(ConditionVariableTest, ScopedUnlockUnderScopedLockThreaded) { |
| 46 | + ConditionVariable cond; |
| 47 | + scopedUnlockUnderScopedLockThreaded<ConditionVariable::ScopedLock, |
| 48 | + ConditionVariable::ScopedUnlock>(cond); |
| 49 | +} |
| 50 | + |
| 51 | +// Test withLock() |
| 52 | +TEST(ConditionVariableTest, CriticalSectionThreaded) { |
| 53 | + ConditionVariable cond; |
| 54 | + criticalSectionThreaded(cond); |
| 55 | +} |
| 56 | + |
| 57 | +// Check that timeouts work |
| 58 | +TEST(ConditionVariableTest, Timeout) { |
| 59 | + using namespace std::chrono_literals; |
| 60 | + |
| 61 | + ConditionVariable cond; |
| 62 | + |
| 63 | + auto duration = measureDuration([&] { |
| 64 | + ConditionVariable::ScopedLock lock(cond); |
| 65 | + bool ret = cond.wait(0.01s); |
| 66 | + ASSERT_FALSE(ret); |
| 67 | + }); |
| 68 | + |
| 69 | + ASSERT_GE(duration.count(), 0.01); |
| 70 | + |
| 71 | + duration = measureDuration([&] { |
| 72 | + ConditionVariable::ScopedLock lock(cond); |
| 73 | + bool ret = cond.wait(0.1s); |
| 74 | + ASSERT_FALSE(ret); |
| 75 | + }); |
| 76 | + |
| 77 | + ASSERT_GE(duration.count(), 0.1); |
| 78 | + |
| 79 | + duration = measureDuration([&] { |
| 80 | + ConditionVariable::ScopedLock lock(cond); |
| 81 | + bool ret = cond.wait(1s); |
| 82 | + ASSERT_FALSE(ret); |
| 83 | + }); |
| 84 | + |
| 85 | + ASSERT_GE(duration.count(), 1.0); |
| 86 | + |
| 87 | + auto deadline = std::chrono::system_clock::now() + 0.5s; |
| 88 | + |
| 89 | + duration = measureDuration([&] { |
| 90 | + ConditionVariable::ScopedLock lock(cond); |
| 91 | + bool ret = cond.waitUntil(deadline); |
| 92 | + ASSERT_FALSE(ret); |
| 93 | + }); |
| 94 | + |
| 95 | + ASSERT_GE(duration.count(), 0.5); |
| 96 | +} |
| 97 | + |
| 98 | +#if !SWIFT_THREADING_NONE |
| 99 | +// Check that signal() wakes exactly one waiter |
| 100 | +TEST(ConditionVariableTest, Signal) { |
| 101 | + ConditionVariable cond; |
| 102 | + |
| 103 | + int ready = 0, done = 0; |
| 104 | + std::vector<std::thread> threads; |
| 105 | + const int thread_count = 10; |
| 106 | + |
| 107 | + for (int i = 0; i < thread_count; ++i) { |
| 108 | + threads.push_back(std::thread([&] { |
| 109 | + cond.lock(); |
| 110 | + ++ready; |
| 111 | + cond.wait(); |
| 112 | + ++done; |
| 113 | + cond.unlock(); |
| 114 | + })); |
| 115 | + } |
| 116 | + |
| 117 | + // Wait for all threads to be ready |
| 118 | + cond.withLock([&] { |
| 119 | + int tries = 1000; |
| 120 | + while (ready < thread_count && tries--) { |
| 121 | + ConditionVariable::ScopedUnlock unlock(cond); |
| 122 | + std::this_thread::sleep_for(std::chrono::milliseconds(10)); |
| 123 | + } |
| 124 | + }); |
| 125 | + |
| 126 | + ASSERT_EQ(ready, thread_count); |
| 127 | + |
| 128 | + // Signal the condition variable and check that done increments by one |
| 129 | + // each time |
| 130 | + for (int i = 0; i < thread_count; ++i) { |
| 131 | + cond.signal(); |
| 132 | + |
| 133 | + cond.withLock([&] { |
| 134 | + int tries = 500; |
| 135 | + while (done == i && tries--) { |
| 136 | + ConditionVariable::ScopedUnlock unlock(cond); |
| 137 | + std::this_thread::sleep_for(std::chrono::milliseconds(10)); |
| 138 | + } |
| 139 | + }); |
| 140 | + |
| 141 | + ASSERT_EQ(done, i + 1); |
| 142 | + } |
| 143 | + |
| 144 | + ASSERT_EQ(done, thread_count); |
| 145 | + |
| 146 | + for (auto &thread : threads) { |
| 147 | + thread.join(); |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +// Check that broadcast() wakes all waiters |
| 152 | +TEST(ConditionVariableTest, Broadcast) { |
| 153 | + ConditionVariable cond; |
| 154 | + |
| 155 | + int ready = 0, done = 0; |
| 156 | + std::vector<std::thread> threads; |
| 157 | + const int thread_count = 10; |
| 158 | + |
| 159 | + for (int i = 0; i < thread_count; ++i) { |
| 160 | + threads.push_back(std::thread([&] { |
| 161 | + cond.lock(); |
| 162 | + ++ready; |
| 163 | + cond.wait(); |
| 164 | + ++done; |
| 165 | + cond.unlock(); |
| 166 | + })); |
| 167 | + } |
| 168 | + |
| 169 | + // Wait for all threads to be ready |
| 170 | + cond.withLock([&] { |
| 171 | + int tries = 1000; |
| 172 | + while (ready < thread_count && tries--) { |
| 173 | + ConditionVariable::ScopedUnlock unlock(cond); |
| 174 | + std::this_thread::sleep_for(std::chrono::milliseconds(10)); |
| 175 | + } |
| 176 | + }); |
| 177 | + |
| 178 | + ASSERT_EQ(ready, thread_count); |
| 179 | + |
| 180 | + // Broadcast and wait |
| 181 | + cond.broadcast(); |
| 182 | + |
| 183 | + cond.withLock([&] { |
| 184 | + int tries = 1000; |
| 185 | + while (done < thread_count && tries--) { |
| 186 | + ConditionVariable::ScopedUnlock unlock(cond); |
| 187 | + std::this_thread::sleep_for(std::chrono::milliseconds(10)); |
| 188 | + } |
| 189 | + }); |
| 190 | + |
| 191 | + ASSERT_EQ(done, thread_count); |
| 192 | + |
| 193 | + for (auto &thread : threads) { |
| 194 | + thread.join(); |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | +#endif |
0 commit comments