Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions unittests/Threading/ThreadingHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,37 @@
#ifndef THREADING_HELPERS_H
#define THREADING_HELPERS_H

#if SWIFT_THREADING_NONE

template <typename ThreadBody, typename AfterSpinRelease>
void threadedExecute(int threadCount, ThreadBody threadBody,
AfterSpinRelease afterSpinRelease) {
for (int i = 0; i < threadCount; ++i) {
threadBody(i);
}
}

template <typename ThreadBody>
void threadedExecute(int threadCount, ThreadBody threadBody) {
threadedExecute(threadCount, threadBody, [] {});
}

template <typename M, typename C, typename ConsumerBody, typename ProducerBody>
void threadedExecute(M &mutex, C &condition, bool &doneCondition,
ConsumerBody consumerBody, ProducerBody producerBody) {
for (int i = 1; i <= 5; ++i) {
producerBody(i);
}
mutex.withLockThenNotifyAll(condition, [&] {
doneCondition = true;
});
for (int i = 1; i <= 8; ++i) {
consumerBody(i);
}
}

#else // !SWIFT_THREADING_NONE

#include <thread>

// When true many of the threaded tests log activity to help triage issues.
Expand Down Expand Up @@ -131,4 +162,6 @@ void threadedExecute(M &mutex, C &condition, bool &doneCondition,
}
}

#endif // !SWIFT_THREADING_NONE

#endif