Skip to content
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
7 changes: 3 additions & 4 deletions Sources/Clocks/ImmediateClock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,18 +133,17 @@
}
}

public private(set) var now: Instant
public internal(set) var now: Instant
public private(set) var minimumResolution: Duration = .zero
private let lock = NSLock()
let lock = NSLock()

public init(now: Instant = .init()) {
self.now = now
}

public func sleep(until deadline: Instant, tolerance: Duration?) async throws {
public func sleep(until deadline: Instant, tolerance: Duration?) throws {
try Task.checkCancellation()
self.lock.sync { self.now = deadline }
await Task.megaYield()
}
}

Expand Down
34 changes: 34 additions & 0 deletions Sources/Clocks/NonsendingClock.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#if compiler(>=6.2)
@available(iOS 16, macOS 13, tvOS 16, watchOS 9, *)
public protocol NonsendingClock<Duration>: Clock {
nonisolated(nonsending)
func sleep(until deadline: Instant, tolerance: Instant.Duration?) async throws
}

@available(iOS 16, macOS 13, tvOS 16, watchOS 9, *)
extension NonsendingClock {
nonisolated(nonsending)
public func sleep(for duration: Duration, tolerance: Instant.Duration? = nil) async throws
{
try await sleep(until: now.advanced(by: duration), tolerance: tolerance)
}
}

@available(iOS 16, macOS 13, tvOS 16, watchOS 9, *)
extension AnyClock: NonsendingClock {}

@available(iOS 16, macOS 13, tvOS 16, watchOS 9, *)
extension ContinuousClock: NonsendingClock {}

@available(iOS 16, macOS 13, tvOS 16, watchOS 9, *)
extension ImmediateClock: NonsendingClock {}

@available(iOS 16, macOS 13, tvOS 16, watchOS 9, *)
extension SuspendingClock: NonsendingClock {}

@available(iOS 16, macOS 13, tvOS 16, watchOS 9, *)
extension TestClock: NonsendingClock {}

@available(iOS 16, macOS 13, tvOS 16, watchOS 9, *)
extension UnimplementedClock: NonsendingClock {}
#endif