Stress is a chaotic random test. One thread (owner) calls PushFront/PopFront, other threads call PushBack/ PopBack. Ensure that we don't crash, deadlock, and all sanity checks pass.
| 158 | // One thread (owner) calls PushFront/PopFront, other threads call PushBack/ |
| 159 | // PopBack. Ensure that we don't crash, deadlock, and all sanity checks pass. |
| 160 | void test_stress_runqueue() |
| 161 | { |
| 162 | static const int kEvents = 1 << 18; |
| 163 | RunQueue<int, 8> q; |
| 164 | std::atomic<int> total(0); |
| 165 | std::vector<std::unique_ptr<std::thread>> threads; |
| 166 | threads.emplace_back(new std::thread([&q, &total]() { |
| 167 | int sum = 0; |
| 168 | int pushed = 1; |
| 169 | int popped = 1; |
| 170 | while (pushed < kEvents || popped < kEvents) { |
| 171 | if (pushed < kEvents) { |
| 172 | if (q.PushFront(pushed) == 0) { |
| 173 | sum += pushed; |
| 174 | pushed++; |
| 175 | } |
| 176 | } |
| 177 | if (popped < kEvents) { |
| 178 | int v = q.PopFront(); |
| 179 | if (v != 0) { |
| 180 | sum -= v; |
| 181 | popped++; |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | total += sum; |
| 186 | })); |
| 187 | for (int i = 0; i < 2; i++) { |
| 188 | threads.emplace_back(new std::thread([&q, &total]() { |
| 189 | int sum = 0; |
| 190 | for (int j = 1; j < kEvents; j++) { |
| 191 | if (q.PushBack(j) == 0) { |
| 192 | sum += j; |
| 193 | continue; |
| 194 | } |
| 195 | EIGEN_THREAD_YIELD(); |
| 196 | j--; |
| 197 | } |
| 198 | total += sum; |
| 199 | })); |
| 200 | threads.emplace_back(new std::thread([&q, &total]() { |
| 201 | int sum = 0; |
| 202 | std::vector<int> stolen; |
| 203 | for (int j = 1; j < kEvents;) { |
| 204 | if (q.PopBackHalf(&stolen) == 0) { |
| 205 | EIGEN_THREAD_YIELD(); |
| 206 | continue; |
| 207 | } |
| 208 | while (stolen.size() && j < kEvents) { |
| 209 | int v = stolen.back(); |
| 210 | stolen.pop_back(); |
| 211 | VERIFY_IS_NOT_EQUAL(v, 0); |
| 212 | sum += v; |
| 213 | j++; |
| 214 | } |
| 215 | } |
| 216 | while (stolen.size()) { |
| 217 | int v = stolen.back(); |
no test coverage detected