Skip to content

[Concurrency] Support duration-based sleep in cooperative executor #42610

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

Merged
Merged
Show file tree
Hide file tree
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
51 changes: 37 additions & 14 deletions stdlib/public/Concurrency/CooperativeGlobalExecutor.inc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@
#include <thread>
#include "swift/Basic/ListMerger.h"

#if __has_include(<time.h>)
# include <time.h>
#endif
#ifndef NSEC_PER_SEC
# define NSEC_PER_SEC 1000000000ull
#endif

namespace {

struct JobQueueTraits {
Expand Down Expand Up @@ -108,17 +115,7 @@ static void swift_task_enqueueMainExecutorImpl(Job *job) {
swift_task_enqueueGlobalImpl(job);
}

/// Insert a job into the cooperative global queue with a delay.
SWIFT_CC(swift)
static void swift_task_enqueueGlobalWithDelayImpl(JobDelay delay,
Job *newJob) {
assert(newJob && "no job provided");

auto deadline = std::chrono::steady_clock::now()
+ std::chrono::duration_cast<JobDeadline::duration>(
std::chrono::nanoseconds(delay));
JobDeadlineStorage<>::set(newJob, deadline);

static void insertDelayedJob(Job *newJob, JobDeadline deadline) {
Job **position = &DelayedJobQueue;
while (auto cur = *position) {
// If we find a job with a later deadline, insert here.
Expand All @@ -136,14 +133,40 @@ static void swift_task_enqueueGlobalWithDelayImpl(JobDelay delay,
*position = newJob;
}

/// Insert a job into the cooperative global queue with a delay.
SWIFT_CC(swift)
static void swift_task_enqueueGlobalWithDelayImpl(JobDelay delay,
Job *newJob) {
assert(newJob && "no job provided");

auto deadline = std::chrono::steady_clock::now()
+ std::chrono::duration_cast<JobDeadline::duration>(
std::chrono::nanoseconds(delay));
JobDeadlineStorage<>::set(newJob, deadline);

insertDelayedJob(newJob, deadline);
}

SWIFT_CC(swift)
static void swift_task_enqueueGlobalWithDeadlineImpl(long long sec,
long long nsec,
long long tsec,
long long tnsec,
int clock, Job *job) {
assert(job && "no job provided");
// TODO: implementation
int clock, Job *newJob) {
assert(newJob && "no job provided");

long long nowSec;
long long nowNsec;
swift_get_time(&nowSec, &nowNsec, (swift_clock_id)clock);

uint64_t delta = (sec - nowSec) * NSEC_PER_SEC + nsec - nowNsec;

auto deadline = std::chrono::steady_clock::now()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I guess this implementation just ignores the clock parameter then, since this does not have a suspending style clock (std::chrono only has continuous)

I guess that makes sense w/ a single threaded runtime.

+ std::chrono::duration_cast<JobDeadline::duration>(
std::chrono::nanoseconds(delta));
JobDeadlineStorage<>::set(newJob, deadline);

insertDelayedJob(newJob, deadline);
}

/// Recognize jobs in the delayed-jobs queue that are ready to execute
Expand Down
3 changes: 0 additions & 3 deletions test/Concurrency/Runtime/clock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
// UNSUPPORTED: back_deployment_runtime
// UNSUPPORTED: back_deploy_concurrency

// This is XFAILed on cooperative executor due to missing implementations
// XFAIL: single_threaded_runtime

import _Concurrency
import StdlibUnittest

Expand Down