Skip to content

Commit 64f9b95

Browse files
committed
[Freestanding] Disable Task.sleep.
1 parent bd5467b commit 64f9b95

File tree

6 files changed

+76
-0
lines changed

6 files changed

+76
-0
lines changed

stdlib/public/Concurrency/Clock.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ public protocol Clock<Duration>: Sendable {
3838
var now: Instant { get }
3939
var minimumResolution: Instant.Duration { get }
4040

41+
#if !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
4142
func sleep(until deadline: Instant, tolerance: Instant.Duration?) async throws
43+
#endif
4244
}
4345

4446

stdlib/public/Concurrency/ContinuousClock.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ extension ContinuousClock: Clock {
7575
.seconds(seconds) + .nanoseconds(nanoseconds))
7676
}
7777

78+
#if !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
7879
/// Suspend task execution until a given deadline within a tolerance.
7980
/// If no tolerance is specified then the system may adjust the deadline
8081
/// to coalesce CPU wake-ups to more efficiently process the wake-ups in
@@ -93,6 +94,15 @@ extension ContinuousClock: Clock {
9394
tolerance: tolerance,
9495
clock: .continuous)
9596
}
97+
#else
98+
@available(SwiftStdlib 5.7, *)
99+
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
100+
public func sleep(
101+
until deadline: Instant, tolerance: Swift.Duration? = nil
102+
) async throws {
103+
fatalError("Unavailable in task-to-thread concurrency model")
104+
}
105+
#endif
96106
}
97107

98108
@available(SwiftStdlib 5.7, *)

stdlib/public/Concurrency/SuspendingClock.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ extension SuspendingClock: Clock {
7777
return .seconds(seconds) + .nanoseconds(nanoseconds)
7878
}
7979

80+
#if !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
8081
/// Suspend task execution until a given deadline within a tolerance.
8182
/// If no tolerance is specified then the system may adjust the deadline
8283
/// to coalesce CPU wake-ups to more efficiently process the wake-ups in
@@ -96,6 +97,15 @@ extension SuspendingClock: Clock {
9697
tolerance: tolerance,
9798
clock: .suspending)
9899
}
100+
#else
101+
@available(SwiftStdlib 5.7, *)
102+
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
103+
public func sleep(
104+
until deadline: Instant, tolerance: Swift.Duration? = nil
105+
) async throws {
106+
fatalError("Unavailable in task-to-thread concurrency model")
107+
}
108+
#endif
99109
}
100110

101111
@available(SwiftStdlib 5.7, *)

stdlib/public/Concurrency/TaskSleep.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import Swift
1313
@_implementationOnly import _SwiftConcurrencyShims
1414

15+
#if !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
1516
@available(SwiftStdlib 5.1, *)
1617
extension Task where Success == Never, Failure == Never {
1718
@available(*, deprecated, renamed: "Task.sleep(nanoseconds:)")
@@ -295,3 +296,19 @@ extension Task where Success == Never, Failure == Never {
295296
}
296297
}
297298
}
299+
#else
300+
@available(SwiftStdlib 5.1, *)
301+
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
302+
extension Task where Success == Never, Failure == Never {
303+
@available(SwiftStdlib 5.1, *)
304+
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
305+
public static func sleep(_ duration: UInt64) async {
306+
fatalError("Unavailable in task-to-thread concurrency model")
307+
}
308+
@available(SwiftStdlib 5.1, *)
309+
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
310+
public static func sleep(nanoseconds duration: UInt64) async throws {
311+
fatalError("Unavailable in task-to-thread concurrency model")
312+
}
313+
}
314+
#endif

stdlib/public/Concurrency/TaskSleepDuration.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import Swift
1313
@_implementationOnly import _SwiftConcurrencyShims
1414

15+
#if !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
1516
@available(SwiftStdlib 5.7, *)
1617
extension Task where Success == Never, Failure == Never {
1718
@available(SwiftStdlib 5.7, *)
@@ -159,3 +160,23 @@ extension Task where Success == Never, Failure == Never {
159160
try await sleep(until: .now + duration, clock: .continuous)
160161
}
161162
}
163+
#else
164+
@available(SwiftStdlib 5.7, *)
165+
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
166+
extension Task where Success == Never, Failure == Never {
167+
@available(SwiftStdlib 5.7, *)
168+
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
169+
public static func sleep<C: Clock>(
170+
until deadline: C.Instant,
171+
tolerance: C.Instant.Duration? = nil,
172+
clock: C
173+
) async throws {
174+
fatalError("Unavailable in task-to-thread concurrency model")
175+
}
176+
@available(SwiftStdlib 5.7, *)
177+
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
178+
public static func sleep(for duration: Duration) async throws {
179+
fatalError("Unavailable in task-to-thread concurrency model")
180+
}
181+
}
182+
#endif

test/stdlib/freestanding_diags_stdlib.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,22 @@ func foo() async throws {
3131
let _: Int = await withUnsafeContinuation { _ in } // expected-error{{Unavailable in task-to-thread concurrency model}}
3232
let _: Int = try await withUnsafeThrowingContinuation { _ in } // expected-error{{Unavailable in task-to-thread concurrency model}}
3333
_ = UnsafeContinuation<Int, Never>.self // expected-error{{Unavailable in task-to-thread concurrency model}}
34+
await Task.sleep(1 as UInt64) // expected-error{{Unavailable in task-to-thread concurrency model}}
35+
try await Task.sleep(nanoseconds: 1 as UInt64) // expected-error{{Unavailable in task-to-thread concurrency model}}
36+
}
37+
38+
@available(SwiftStdlib 5.7, *)
39+
func bar() async {
40+
func withContinuousClock(_ clock: ContinuousClock) async throws { try await clock.sleep(until: { fatalError() }()) } // expected-error{{Unavailable in task-to-thread concurrency model}}
41+
func withSuspendingClock(_ clock: SuspendingClock) async throws { try await clock.sleep(until: { fatalError() }()) } // expected-error{{Unavailable in task-to-thread concurrency model}}
42+
func withClock<C : Clock>(
43+
until deadline: C.Instant,
44+
tolerance: C.Instant.Duration? = nil,
45+
clock: C
46+
) async throws {
47+
try await Task.sleep(until: deadline, tolerance: tolerance, clock: clock) // expected-error{{Unavailable in task-to-thread concurrency model}}
48+
}
49+
func withDuration(_ duration: Duration) async throws { try await Task.sleep(for: duration) } // expected-error{{Unavailable in task-to-thread concurrency model}}
3450
}
3551

3652
func foo2(

0 commit comments

Comments
 (0)