Skip to content

Commit 8ae6ec7

Browse files
Merge pull request #69006 from kateinoigakukun/pr-0983246048e8181baa144ac66ad3344b4b1b774c
2 parents ac91a58 + 87e7030 commit 8ae6ec7

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

stdlib/public/Concurrency/CooperativeGlobalExecutor.inc

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@
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.
199221
static 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

Comments
 (0)