2323// /===------------------------------------------------------------------===///
2424
2525#include < chrono>
26- #include < thread>
26+ #ifndef SWIFT_THREADING_NONE
27+ # include < thread>
28+ #endif
29+ #include < errno.h>
2730#include " swift/Basic/ListMerger.h"
2831
2932#if __has_include(<time.h>)
@@ -195,6 +198,25 @@ static void recognizeReadyDelayedJobs() {
195198 DelayedJobQueue = nextDelayedJob;
196199}
197200
201+ static void sleepThisThreadUntil (JobDeadline deadline) {
202+ #ifdef SWIFT_THREADING_NONE
203+ auto duration = deadline - std::chrono::steady_clock::now ();
204+ // If the deadline is in the past, don't sleep with invalid negative value
205+ if (duration <= std::chrono::nanoseconds::zero ()) {
206+ return ;
207+ }
208+ auto sec = std::chrono::duration_cast<std::chrono::seconds>(duration);
209+ auto ns = std::chrono::duration_cast<std::chrono::nanoseconds>(duration - sec);
210+
211+ struct timespec ts;
212+ ts.tv_sec = sec.count ();
213+ ts.tv_nsec = ns.count ();
214+ while (nanosleep (&ts, &ts) == -1 && errno == EINTR);
215+ #else
216+ std::this_thread::sleep_until (deadline);
217+ #endif
218+ }
219+
198220// / Claim the next job from the cooperative global queue.
199221static Job *claimNextFromCooperativeGlobalQueue () {
200222 while (true ) {
@@ -211,7 +233,7 @@ static Job *claimNextFromCooperativeGlobalQueue() {
211233 // TODO: should the donator have some say in this?
212234 if (auto delayedJob = DelayedJobQueue) {
213235 auto deadline = JobDeadlineStorage<>::get (delayedJob);
214- std::this_thread::sleep_until (deadline);
236+ sleepThisThreadUntil (deadline);
215237 continue ;
216238 }
217239
0 commit comments