@@ -29,9 +29,47 @@ CHANGELOG
29
29
Swift 5.5
30
30
---------
31
31
32
+ * [ SE-0316] [ ] :
33
+
34
+ A type can be defined as a global actor. Global actors extend the notion
35
+ of actor isolation outside of a single actor type, so that global state
36
+ (and the functions that access it) can benefit from actor isolation,
37
+ even if the state and functions are scattered across many different
38
+ types, functions and modules. Global actors make it possible to safely
39
+ work with global variables in a concurrent program, as well as modeling
40
+ other global program constraints such as code that must only execute on
41
+ the "main thread" or "UI thread". A new global actor can be defined with
42
+ the ` globalActor ` attribute:
43
+
44
+ ``` swift
45
+ @globalActor
46
+ struct DatabaseActor {
47
+ actor ActorType { }
48
+
49
+ static let shared: ActorType = ActorType ()
50
+ }
51
+ ```
52
+
53
+ Global actor types can be used as custom attributes on various declarations,
54
+ which ensures that those declarations are only accessed on the actor described
55
+ by the global actor's ` shared ` instance. For example:
56
+
57
+ ``` swift
58
+ @DatabaseActor func queryDB (query : Query) throws -> QueryResult
59
+
60
+ func runQuery (queryString : String ) async throws -> QueryResult {
61
+ let query = try Query (parsing : queryString)
62
+ return try await queryDB (query : query) // 'await' because this implicitly hops to DatabaseActor.shared
63
+ }
64
+ ```
65
+
66
+ The concurrency library defines one global actor, ` MainActor ` , which
67
+ represents the main thread of execution. It should be used for any code that
68
+ must execute on the main thread, e.g., for updating UI.
69
+
32
70
* [ SE-0313] [ ] :
33
71
34
- Declarations inside an actor that would normally by actor-isolated can
72
+ Declarations inside an actor that would normally be actor-isolated can
35
73
explicitly become non-isolated using the ` nonisolated ` keyword. Non-isolated
36
74
declarations can be used to conform to synchronous protocol requirements:
37
75
@@ -8535,6 +8573,7 @@ Swift 1.0
8535
8573
[SE- 0306 ]: < https: // github.com/apple/swift-evolution/blob/main/proposals/0306-actors.md>
8536
8574
[SE- 0310 ]: < https: // github.com/apple/swift-evolution/blob/main/proposals/0310-effectful-readonly-properties.md>
8537
8575
[SE- 0313 ]: < https: // github.com/apple/swift-evolution/blob/main/proposals/0313-actor-isolation-control.md>
8576
+ [SE- 0316 ]: < https: // github.com/apple/swift-evolution/blob/main/proposals/0316-global-actors.md>
8538
8577
8539
8578
[SR- 75 ]: < https: // bugs.swift.org/browse/SR-75>
8540
8579
[SR- 106 ]: < https: // bugs.swift.org/browse/SR-106>
0 commit comments