Skip to content

[Threading][UnitTests] Don't use threads in the unit test in no-threads mode. #59403

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
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