23
23
// /===------------------------------------------------------------------===///
24
24
25
25
#include < chrono>
26
- #include < thread>
26
+ #ifndef SWIFT_THREADING_NONE
27
+ # include < thread>
28
+ #endif
29
+ #include < errno.h>
27
30
#include " swift/Basic/ListMerger.h"
28
31
29
32
#if __has_include(<time.h>)
@@ -195,6 +198,25 @@ static void recognizeReadyDelayedJobs() {
195
198
DelayedJobQueue = nextDelayedJob;
196
199
}
197
200
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
+
198
220
// / Claim the next job from the cooperative global queue.
199
221
static Job *claimNextFromCooperativeGlobalQueue () {
200
222
while (true ) {
@@ -211,7 +233,7 @@ static Job *claimNextFromCooperativeGlobalQueue() {
211
233
// TODO: should the donator have some say in this?
212
234
if (auto delayedJob = DelayedJobQueue) {
213
235
auto deadline = JobDeadlineStorage<>::get (delayedJob);
214
- std::this_thread::sleep_until (deadline);
236
+ sleepThisThreadUntil (deadline);
215
237
continue ;
216
238
}
217
239
0 commit comments