Skip to content

Commit cbaa1b1

Browse files
committed
[Freestanding] Disable Task.sleep.
1 parent 8535e80 commit cbaa1b1

File tree

6 files changed

+78
-1
lines changed

6 files changed

+78
-1
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: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ func chowMein() async {
1111
@MainActor // expected-error{{not permitted within task-to-thread concurrency model}}
1212
class ChowMein {}
1313

14-
func foo() async {
14+
@available(SwiftStdlib 5.1, *)
15+
func foo() async throws {
1516
Task<Void, Never> {} // expected-error{{Unavailable in task-to-thread concurrency model}}
1617
Task<Void, Error> {} // expected-error{{Unavailable in task-to-thread concurrency model}}
1718
Task<Void, Never>.detached {} // expected-error{{Unavailable in task-to-thread concurrency model}}
@@ -24,6 +25,22 @@ func foo() async {
2425
asyncDetached { () async -> () in } // expected-error{{Unavailable in task-to-thread concurrency model}}
2526
asyncDetached { () async throws -> () in } // expected-error{{Unavailable in task-to-thread concurrency model}}
2627
_ = MainActor.self // expected-error{{Unavailable in task-to-thread concurrency model}}
28+
await Task.sleep(1 as UInt64) // expected-error{{Unavailable in task-to-thread concurrency model}}
29+
try await Task.sleep(nanoseconds: 1 as UInt64) // expected-error{{Unavailable in task-to-thread concurrency model}}
30+
}
31+
32+
@available(SwiftStdlib 5.7, *)
33+
func bar() async {
34+
func withContinuousClock(_ clock: ContinuousClock) async throws { try await clock.sleep(until: { fatalError() }()) } // expected-error{{Unavailable in task-to-thread concurrency model}}
35+
func withSuspendingClock(_ clock: SuspendingClock) async throws { try await clock.sleep(until: { fatalError() }()) } // expected-error{{Unavailable in task-to-thread concurrency model}}
36+
func withClock<C : Clock>(
37+
until deadline: C.Instant,
38+
tolerance: C.Instant.Duration? = nil,
39+
clock: C
40+
) async throws {
41+
try await Task.sleep(until: deadline, tolerance: tolerance, clock: clock) // expected-error{{Unavailable in task-to-thread concurrency model}}
42+
}
43+
func withDuration(_ duration: Duration) async throws { try await Task.sleep(for: duration) } // expected-error{{Unavailable in task-to-thread concurrency model}}
2744
}
2845

2946
func foo2(

0 commit comments

Comments
 (0)