Skip to content

Commit ab6b2ac

Browse files
committed
[Release Notes] Improve the release note for SE-0412.
(cherry picked from commit fa740bc)
1 parent 3a7df14 commit ab6b2ac

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
@@ -56,29 +56,56 @@ concurrency checking.
5656

5757
* [SE-0412][]:
5858

59-
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.
59+
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:
60+
61+
1. isolated to a global actor, or
62+
2. immutable and of `Sendable` type.
63+
64+
For example:
6065

6166
```swift
6267
var mutableGlobal = 1
6368
// warning: var 'mutableGlobal' is not concurrency-safe because it is non-isolated global shared mutable state
6469
// (unless it is top-level code which implicitly isolates to @MainActor)
6570

66-
final class NonsendableType {
67-
init() {}
71+
@MainActor func mutateGlobalFromMain() {
72+
mutableGlobal += 1
73+
}
74+
75+
nonisolated func mutateGlobalFromNonisolated() async {
76+
mutableGlobal += 10
6877
}
6978

7079
struct S {
71-
static let immutableNonsendable = NonsendableType()
72-
// warning: static property 'immutableNonsendable' is not concurrency-safe because it is not either conforming to 'Sendable' or isolated to a global actor
80+
static let immutableSendable = 10
81+
// okay; 'immutableSendable' is safe to access concurrently because it's immutable and 'Int' is 'Sendable'
7382
}
7483
```
7584

76-
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.
85+
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:
7786

7887
```swift
79-
nonisolated(unsafe) var global: String
88+
// This global is only set in one part of the program
89+
nonisolated(unsafe) var global: String!
8090
```
8191

92+
`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:
93+
94+
```swift
95+
import Dispatch
96+
97+
// 'MutableData' is not 'Sendable'
98+
class MutableData { ... }
99+
100+
final class MyModel: Sendable {
101+
private let queue = DispatchQueue(...)
102+
// 'protectedState' is manually isolated by 'queue'
103+
nonisolated(unsafe) private var protectedState: MutableData
104+
}
105+
```
106+
107+
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.
108+
82109
* [SE-0411][]:
83110

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

0 commit comments

Comments
 (0)