|
13 | 13 | #ifndef THREADING_HELPERS_H
|
14 | 14 | #define THREADING_HELPERS_H
|
15 | 15 |
|
| 16 | +#if SWIFT_THREADING_NONE |
| 17 | + |
| 18 | +template <typename ThreadBody, typename AfterSpinRelease> |
| 19 | +void threadedExecute(int threadCount, ThreadBody threadBody, |
| 20 | + AfterSpinRelease afterSpinRelease) { |
| 21 | + for (int i = 0; i < threadCount; ++i) { |
| 22 | + threadBody(i); |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +template <typename ThreadBody> |
| 27 | +void threadedExecute(int threadCount, ThreadBody threadBody) { |
| 28 | + threadedExecute(threadCount, threadBody, [] {}); |
| 29 | +} |
| 30 | + |
| 31 | +template <typename M, typename C, typename ConsumerBody, typename ProducerBody> |
| 32 | +void threadedExecute(M &mutex, C &condition, bool &doneCondition, |
| 33 | + ConsumerBody consumerBody, ProducerBody producerBody) { |
| 34 | + for (int i = 1; i <= 5; ++i) { |
| 35 | + producerBody(i); |
| 36 | + } |
| 37 | + mutex.withLockThenNotifyAll(condition, [&] { |
| 38 | + doneCondition = true; |
| 39 | + }); |
| 40 | + for (int i = 1; i <= 8; ++i) { |
| 41 | + consumerBody(i); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +#else // !SWIFT_THREADING_NONE |
| 46 | + |
16 | 47 | #include <thread>
|
17 | 48 |
|
18 | 49 | // When true many of the threaded tests log activity to help triage issues.
|
@@ -131,4 +162,6 @@ void threadedExecute(M &mutex, C &condition, bool &doneCondition,
|
131 | 162 | }
|
132 | 163 | }
|
133 | 164 |
|
| 165 | +#endif // !SWIFT_THREADING_NONE |
| 166 | + |
134 | 167 | #endif
|
0 commit comments