Skip to content
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

Add an additional case for clocks to sleep with a wallclock deadline #79139

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion include/swift/Runtime/Concurrency.h
Original file line number Diff line number Diff line change
Expand Up @@ -1022,7 +1022,8 @@ void swift_task_donateThreadToGlobalExecutorUntil(bool (*condition)(void*),

enum swift_clock_id : int {
swift_clock_id_continuous = 1,
swift_clock_id_suspending = 2
swift_clock_id_suspending = 2,
swift_clock_id_wall = 3
};

SWIFT_EXPORT_FROM(swift_Concurrency) SWIFT_CC(swift)
Expand Down
37 changes: 37 additions & 0 deletions stdlib/public/Concurrency/Clock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,29 @@ void swift_get_time(
*seconds = suspending.tv_sec;
*nanoseconds = suspending.tv_nsec;
return;
case swift_clock_id_wall:
struct timespec wall;
#if defined(__linux__) || defined(__APPLE__) || defined(__wasi__) || defined(__OpenBSD__) || defined(__FreeBSD__)
clock_gettime(CLOCK_REALTIME, &wall);
#elif defined(_WIN32)
// This needs to match what swift-corelibs-libdispatch does

static const uint64_t kNTToUNIXBiasAdjustment = 11644473600 * NSEC_PER_SEC;
// FILETIME is 100-nanosecond intervals since January 1, 1601 (UTC).
FILETIME ft;
ULARGE_INTEGER li;
GetSystemTimePreciseAsFileTime(&ft);
li.LowPart = ft.dwLowDateTime;
li.HighPart = ft.dwHighDateTime;
ULONGLONG ns = li.QuadPart * 100ull - kNTToUNIXBiasAdjustment;
wall.tv_sec = ns / 1000000000ull;
wall.tv_nsec = ns % 1000000000ull;
#else
#error Missing platform wall time definition
#endif
*seconds = wall.tv_sec;
*nanoseconds = wall.tv_nsec;
return;
}
}
swift_Concurrency_fatalError(0, "Fatal error: invalid clock ID %d\n",
Expand Down Expand Up @@ -134,6 +157,20 @@ switch (clock_id) {
*nanoseconds = suspending.tv_nsec;
return;
}
case swift_clock_id_wall: {
struct timespec wall;
#if defined(__linux__) || defined(__APPLE__) || defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__wasi__)
clock_getres(CLOCK_REALTIME, &wall);
#elif defined(_WIN32)
wall.tv_sec = 0;
wall.tv_nsec = 100;
#else
#error Missing platform wall time definition
#endif
*seconds = wall.tv_sec;
*nanoseconds = wall.tv_nsec;
return;
}
}
swift_Concurrency_fatalError(0, "Fatal error: invalid clock ID %d\n",
clock_id);
Expand Down
10 changes: 9 additions & 1 deletion stdlib/public/Concurrency/DispatchGlobalExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,8 @@ platform_time(uint64_t nsec) {
#endif

static inline dispatch_time_t
clock_and_value_to_time(int clock, long long deadline) {
clock_and_value_to_time(int clock, long long sec, long long nsec) {
uint64_t deadline = sec * NSEC_PER_SEC + nsec;
uint64_t value = platform_time((uint64_t)deadline);
if (value >= DISPATCH_TIME_MAX_VALUE) {
return DISPATCH_TIME_FOREVER;
Expand All @@ -324,6 +325,13 @@ clock_and_value_to_time(int clock, long long deadline) {
return value;
case swift_clock_id_continuous:
return value | DISPATCH_UP_OR_MONOTONIC_TIME_MASK;
case swift_clock_id_wall: {
struct timespec ts = {
.tv_sec = sec,
.tv_nsec = nsec
};
return dispatch_walltime(&ts, 0);
}
}
__builtin_unreachable();
}
Expand Down