Skip to content

Commit eb8dcc3

Browse files
authored
Merge pull request #82666 from ktoso/wip-changelog-task-immediate
2 parents 852b56f + 9d16901 commit eb8dcc3

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed

CHANGELOG.md

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,125 @@
55
66
## Swift 6.2
77

8+
* [SE-0472][]:
9+
Introduced new `Task.immediate` and `taskGroup.addImmediateTask` APIs, which allow a task to run "immediately" in the
10+
calling context if its isolation is compatible with the enclosing one. This can be used to create tasks which execute
11+
without additional scheduling overhead, and allow for finer-grained control over where a task begins running.
12+
13+
The canonical example for using this new API is using an unstructured immediate task like this:
14+
15+
```swift
16+
func synchronous() { // synchronous function
17+
// executor / thread: "T1"
18+
let task: Task<Void, Never> = Task.immediate {
19+
// executor / thread: "T1"
20+
guard keepRunning() else { return } // synchronous call (1)
21+
22+
// executor / thread: "T1"
23+
await noSuspension() // potential suspension point #1 // (2)
24+
25+
// executor / thread: "T1"
26+
await suspend() // potential suspension point #2 // (3), suspend, (5)
27+
// executor / thread: "other"
28+
}
29+
30+
// (4) continue execution
31+
// executor / thread: "T1"
32+
}
33+
```
34+
35+
* [SE-0471][]:
36+
Actor and global actor annotated types may now declare a synchronous `isolated deinit`, which allows such deinitializer
37+
to access actor isolated state while deinitializing the actor. This enables actor deinitializers to safely access
38+
and shut down or close resources during an actors deinitialization, without explicitly resorting to unstructured
39+
concurrency tasks.
40+
41+
```swift
42+
class NonSendableAhmed {
43+
var state: Int = 0
44+
}
45+
46+
@MainActor
47+
class Maria {
48+
let friend: NonSendableAhmed
49+
50+
init() {
51+
self.friend = NonSendableAhmed()
52+
}
53+
54+
init(sharingFriendOf otherMaria: Maria) {
55+
// While the friend is non-Sendable, this initializer and
56+
// and the otherMaria are isolated to the MainActor. That is,
57+
// they share the same executor. So, it's OK for the non-Sendable value
58+
// to cross between otherMaria and self.
59+
self.friend = otherMaria.friend
60+
}
61+
62+
isolated deinit {
63+
// Used to be a potential data race. Now, deinit is also
64+
// isolated on the MainActor, so this code is perfectly
65+
// correct.
66+
friend.state += 1
67+
}
68+
}
69+
70+
func example() async {
71+
let m1 = await Maria()
72+
let m2 = await Maria(sharingFriendOf: m1)
73+
doSomething(m1, m2)
74+
}
75+
```
76+
77+
* [SE-0469][]:
78+
Swift concurrency tasks (both unstructured and structured, via the TaskGroup `addTask` APIs) may now be given
79+
human-readable names, which can be used to support debugging and identifying tasks.
80+
81+
```swift
82+
let getUsers = Task("Get Users for \(accountID)") {
83+
await users.get(accountID)
84+
}
85+
```
86+
87+
* [SE-0462][]:
88+
Task priority escalation may now be explicitly caused to a `Task`, as well as reacted to using the new task priority escalation handlers:
89+
90+
```swift
91+
// priority: low
92+
// priority: high!
93+
await withTaskPriorityEscalationHandler {
94+
await work()
95+
} onPriorityEscalated: { newPriority in // may not be triggered if ->high escalation happened before handler was installed
96+
// do something
97+
}
98+
```
99+
* [SE-0461][]:
100+
Nonisolated asynchronous functions may now execute on the calling actor, when the upcoming feature `NonisolatedNonsendingByDefault`
101+
is enabled, or when explicitly opted-into using the `nonisolated(nonsending)` keywords. This allows for fine grained control
102+
over where nonisolated asynchronous functions execute, and allows for the default behavior of their execution to be changed
103+
from always executing on the global concurrent pool, to the calling actor, which can yield noticeable performance improvements
104+
thanks to less executor hopping when nonisolated and isolated code is invoked in sequence.
105+
106+
This also allows for safely using asynchronous functions on non-sendable types from actors, like so:
107+
108+
```swift
109+
class NotSendable {
110+
func performSync() { ... }
111+
112+
nonisolated(nonsending)
113+
func performAsync() async { ... }
114+
}
115+
116+
actor MyActor {
117+
let x: NotSendable
118+
119+
func call() async {
120+
x.performSync() // okay
121+
122+
await x.performAsync() // okay
123+
}
124+
}
125+
```
126+
8127
* The Swift compiler no longer diagnoses references to declarations that are
9128
potentially unavailable because the platform version might not be new enough
10129
when those references occur inside of contexts that are also unavailable to
@@ -10787,7 +10906,12 @@ using the `.dynamicType` member to retrieve the type of an expression should mig
1078710906
[SE-0442]: https://github.com/swiftlang/swift-evolution/blob/main/proposals/0442-allow-taskgroup-childtaskresult-type-to-be-inferred.md
1078810907
[SE-0444]: https://github.com/swiftlang/swift-evolution/blob/main/proposals/0444-member-import-visibility.md
1078910908
[SE-0458]: https://github.com/swiftlang/swift-evolution/blob/main/proposals/0458-strict-memory-safety.md
10909+
[SE-0461]: https://github.com/swiftlang/swift-evolution/blob/main/proposals/0461-async-function-isolation.md
10910+
[SE-0462]: https://github.com/swiftlang/swift-evolution/blob/main/proposals/0462-task-priority-escalation-apis.md
10911+
[SE-0469]: https://github.com/swiftlang/swift-evolution/blob/main/proposals/0469-task-names.md
1079010912
[SE-0470]: https://github.com/swiftlang/swift-evolution/blob/main/proposals/0470-isolated-conformances.md
10913+
[SE-0471]: https://github.com/swiftlang/swift-evolution/blob/main/proposals/0371-isolated-synchronous-deinit.md
10914+
[SE-0472]: https://github.com/swiftlang/swift-evolution/blob/main/proposals/0472-task-start-synchronously-on-caller-context.md
1079110915
[#64927]: <https://github.com/apple/swift/issues/64927>
1079210916
[#42697]: <https://github.com/apple/swift/issues/42697>
1079310917
[#42728]: <https://github.com/apple/swift/issues/42728>

0 commit comments

Comments
 (0)