You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: CHANGELOG.md
+34-7Lines changed: 34 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -111,29 +111,56 @@ strict concurrency checking ahead of Swift 6.
111
111
112
112
*[SE-0412][]:
113
113
114
-
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.
114
+
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:
115
+
116
+
1. isolated to a global actor, or
117
+
2. immutable and of `Sendable` type.
118
+
119
+
For example:
115
120
116
121
```swift
117
122
var mutableGlobal =1
118
123
// warning: var 'mutableGlobal' is not concurrency-safe because it is non-isolated global shared mutable state
119
124
// (unless it is top-level code which implicitly isolates to @MainActor)
//warning: static property 'immutableNonsendable' is not concurrency-safe because it is not either conforming to 'Sendable' or isolated to a global actor
135
+
staticletimmutableSendable=10
136
+
//okay; 'immutableSendable' is safe to access concurrently because it's immutable and 'Int' is 'Sendable'
128
137
}
129
138
```
130
139
131
-
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.
140
+
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:
132
141
133
142
```swift
134
-
nonisolated(unsafe) var global: String
143
+
// This global is only set in one part of the program
144
+
nonisolated(unsafe) var global: String!
135
145
```
136
146
147
+
`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:
148
+
149
+
```swift
150
+
importDispatch
151
+
152
+
// 'MutableData' is not 'Sendable'
153
+
classMutableData { ... }
154
+
155
+
finalclassMyModel: Sendable {
156
+
privatelet queue =DispatchQueue(...)
157
+
// 'protectedState' is manually isolated by 'queue'
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.
163
+
137
164
*[SE-0411][]:
138
165
139
166
Default value expressions can now have the same isolation as the enclosing
0 commit comments