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

Fix Scheduler implementation for ILP32 platforms such as watchOS #1137

Merged
merged 1 commit into from
Sep 5, 2023
Merged
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
18 changes: 12 additions & 6 deletions Sources/DistributedCluster/Scheduler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ extension DispatchWorkItem: Cancelable {
extension DispatchQueue: Scheduler, @unchecked Sendable {
func scheduleOnce(delay: Duration, _ f: @escaping () -> Void) -> Cancelable {
let workItem = DispatchWorkItem(block: f)
self.asyncAfter(deadline: .now() + Dispatch.DispatchTimeInterval.nanoseconds(Int(delay.nanoseconds)), execute: workItem)
self.asyncAfter(deadline: .init(nowDelayedBy: delay), execute: workItem)
return workItem
}

Expand All @@ -76,7 +76,7 @@ extension DispatchQueue: Scheduler, @unchecked Sendable {
await f()
}
}
self.asyncAfter(deadline: .now() + Dispatch.DispatchTimeInterval.nanoseconds(Int(delay.nanoseconds)), execute: workItem)
self.asyncAfter(deadline: .init(nowDelayedBy: delay), execute: workItem)
return workItem
}

Expand All @@ -91,13 +91,13 @@ extension DispatchQueue: Scheduler, @unchecked Sendable {

func sched() {
if !cancellable.isCanceled {
let nextDeadline = DispatchTime.now() + Dispatch.DispatchTimeInterval.nanoseconds(Int(interval.nanoseconds))
let nextDeadline = DispatchTime(nowDelayedBy: interval)
f()
self.asyncAfter(deadline: nextDeadline, execute: sched)
}
}

self.asyncAfter(deadline: .now() + Dispatch.DispatchTimeInterval.nanoseconds(Int(initialDelay.nanoseconds)), execute: sched)
self.asyncAfter(deadline: .init(nowDelayedBy: initialDelay), execute: sched)

return cancellable
}
Expand All @@ -107,15 +107,15 @@ extension DispatchQueue: Scheduler, @unchecked Sendable {

@Sendable func sched() {
if !cancellable.isCanceled {
let nextDeadline = DispatchTime.now() + Dispatch.DispatchTimeInterval.nanoseconds(Int(interval.nanoseconds))
let nextDeadline = DispatchTime(nowDelayedBy: interval)
Task {
await f()
self.asyncAfter(deadline: nextDeadline, execute: sched)
}
}
}

self.asyncAfter(deadline: .now() + Dispatch.DispatchTimeInterval.nanoseconds(Int(initialDelay.nanoseconds)), execute: sched)
self.asyncAfter(deadline: .init(nowDelayedBy: initialDelay), execute: sched)

return cancellable
}
Expand All @@ -126,3 +126,9 @@ extension DispatchQueue: Scheduler, @unchecked Sendable {
}
}
}

extension Dispatch.DispatchTime {
fileprivate init(nowDelayedBy delay: Duration) {
self.init(uptimeNanoseconds: DispatchTime.now().uptimeNanoseconds + UInt64(delay.nanoseconds))
}
}