Skip to content

Commit fa740bc

Browse files
committed
[Release Notes] Improve the release note for SE-0412.
1 parent fad88f3 commit fa740bc

File tree

1 file changed

+34
-7
lines changed

1 file changed

+34
-7
lines changed

CHANGELOG.md

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,29 +112,56 @@ concurrency checking.
112112

113113
* [SE-0412][]:
114114

115-
Under strict concurrency checking, every global or static variable must be either isolated to a global actor or be both immutable and of `Sendable` type.
115+
Global and static variables are prone to data races because they provide memory that can be accessed from any program context. Strict concurrency checking in Swift 5.10 prevents data races on global and static variables by requiring them to be either:
116+
117+
1. isolated to a global actor, or
118+
2. immutable and of `Sendable` type.
119+
120+
For example:
116121

117122
```swift
118123
var mutableGlobal = 1
119124
// warning: var 'mutableGlobal' is not concurrency-safe because it is non-isolated global shared mutable state
120125
// (unless it is top-level code which implicitly isolates to @MainActor)
121126

122-
final class NonsendableType {
123-
init() {}
127+
@MainActor func mutateGlobalFromMain() {
128+
mutableGlobal += 1
129+
}
130+
131+
nonisolated func mutateGlobalFromNonisolated() async {
132+
mutableGlobal += 10
124133
}
125134

126135
struct S {
127-
static let immutableNonsendable = NonsendableType()
128-
// warning: static property 'immutableNonsendable' is not concurrency-safe because it is not either conforming to 'Sendable' or isolated to a global actor
136+
static let immutableSendable = 10
137+
// okay; 'immutableSendable' is safe to access concurrently because it's immutable and 'Int' is 'Sendable'
129138
}
130139
```
131140

132-
The attribute `nonisolated(unsafe)` can be used to annotate a global variable (or any form of storage) to disable static checking of data isolation, but note that without correct implementation of a synchronization mechanism to achieve data isolation, dynamic run-time analysis from exclusivity enforcement or tools such as Thread Sanitizer could still identify failures.
141+
A new `nonisolated(unsafe)` modifier can be used to annotate a global or static variable to suppress data isolation violations when manual synchronization is provided:
133142

134143
```swift
135-
nonisolated(unsafe) var global: String
144+
// This global is only set in one part of the program
145+
nonisolated(unsafe) var global: String!
136146
```
137147

148+
`nonisolated(unsafe)` can be used on any form of storage, including stored properties and local variables, as a more granular opt out for `Sendable` checking, eliminating the need for `@unchecked Sendable` wrapper types in many use cases:
149+
150+
```swift
151+
import Dispatch
152+
153+
// 'MutableData' is not 'Sendable'
154+
class MutableData { ... }
155+
156+
final class MyModel: Sendable {
157+
private let queue = DispatchQueue(...)
158+
// 'protectedState' is manually isolated by 'queue'
159+
nonisolated(unsafe) private var protectedState: MutableData
160+
}
161+
```
162+
163+
Note that without correct implementation of a synchronization mechanism to achieve data isolation, dynamic run-time analysis from exclusivity enforcement or tools such as the Thread Sanitizer could still identify failures.
164+
138165
* [SE-0411][]:
139166

140167
Default value expressions can now have the same isolation as the enclosing

0 commit comments

Comments
 (0)