26
26
#include < thread>
27
27
#include " swift/Basic/ListMerger.h"
28
28
29
+ #if __has_include(<time.h>)
30
+ # include < time.h>
31
+ #endif
32
+ #ifndef NSEC_PER_SEC
33
+ # define NSEC_PER_SEC 1000000000ull
34
+ #endif
35
+
29
36
namespace {
30
37
31
38
struct JobQueueTraits {
@@ -108,17 +115,7 @@ static void swift_task_enqueueMainExecutorImpl(Job *job) {
108
115
swift_task_enqueueGlobalImpl (job);
109
116
}
110
117
111
- // / Insert a job into the cooperative global queue with a delay.
112
- SWIFT_CC (swift)
113
- static void swift_task_enqueueGlobalWithDelayImpl(JobDelay delay,
114
- Job *newJob) {
115
- assert (newJob && " no job provided" );
116
-
117
- auto deadline = std::chrono::steady_clock::now ()
118
- + std::chrono::duration_cast<JobDeadline::duration>(
119
- std::chrono::nanoseconds (delay));
120
- JobDeadlineStorage<>::set (newJob, deadline);
121
-
118
+ static void insertDelayedJob (Job *newJob, JobDeadline deadline) {
122
119
Job **position = &DelayedJobQueue;
123
120
while (auto cur = *position) {
124
121
// If we find a job with a later deadline, insert here.
@@ -136,14 +133,40 @@ static void swift_task_enqueueGlobalWithDelayImpl(JobDelay delay,
136
133
*position = newJob;
137
134
}
138
135
136
+ // / Insert a job into the cooperative global queue with a delay.
137
+ SWIFT_CC (swift)
138
+ static void swift_task_enqueueGlobalWithDelayImpl(JobDelay delay,
139
+ Job *newJob) {
140
+ assert (newJob && " no job provided" );
141
+
142
+ auto deadline = std::chrono::steady_clock::now ()
143
+ + std::chrono::duration_cast<JobDeadline::duration>(
144
+ std::chrono::nanoseconds (delay));
145
+ JobDeadlineStorage<>::set (newJob, deadline);
146
+
147
+ insertDelayedJob (newJob, deadline);
148
+ }
149
+
139
150
SWIFT_CC (swift)
140
151
static void swift_task_enqueueGlobalWithDeadlineImpl(long long sec,
141
152
long long nsec,
142
153
long long tsec,
143
154
long long tnsec,
144
- int clock, Job *job) {
145
- assert (job && " no job provided" );
146
- // TODO: implementation
155
+ int clock, Job *newJob) {
156
+ assert (newJob && " no job provided" );
157
+
158
+ long long nowSec;
159
+ long long nowNsec;
160
+ swift_get_time (&nowSec, &nowNsec, (swift_clock_id)clock);
161
+
162
+ uint64_t delta = (sec - nowSec) * NSEC_PER_SEC + nsec - nowNsec;
163
+
164
+ auto deadline = std::chrono::steady_clock::now ()
165
+ + std::chrono::duration_cast<JobDeadline::duration>(
166
+ std::chrono::nanoseconds (delta));
167
+ JobDeadlineStorage<>::set (newJob, deadline);
168
+
169
+ insertDelayedJob (newJob, deadline);
147
170
}
148
171
149
172
// / Recognize jobs in the delayed-jobs queue that are ready to execute
0 commit comments