Skip to content

Commit f5dc1a1

Browse files
committed
[SE-0316] Global actors
Enable global actors by default. The proposal has been accepted and is implemented.
1 parent 83050d6 commit f5dc1a1

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

CHANGELOG.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,44 @@ _**Note:** This is in reverse chronological order, so newer entries are added to
66
Swift 5.5
77
---------
88

9+
* [SE-0313][]:
10+
11+
A type can be defined as a global actor. Global actors extend the notion
12+
of actor isolation outside of a single actor type, so that global state
13+
(and the functions that access it) can benefit from actor isolation,
14+
even if the state and functions are scattered across many different
15+
types, functions and modules. Global actors make it possible to safely
16+
work with global variables in a concurrent program, as well as modeling
17+
other global program constraints such as code that must only execute on
18+
the "main thread" or "UI thread". A new global actor can be defined with
19+
the `globalActor` attribute:
20+
21+
```swift
22+
@globalActor
23+
struct DatabaseActor {
24+
actor ActorType { }
25+
26+
static let shared: ActorType = ActorType()
27+
}
28+
```
29+
30+
Global actor types can be used as custom attributes on various declarations,
31+
which ensures that those declarations are only accessed on the actor described
32+
by the global actor's `shared` instance. For example:
33+
34+
```swift
35+
@DatabaseActor func queryDB(query: Query) throws -> QueryResult
36+
37+
func runQuery(queryString: String) async throws -> QueryResult {
38+
let query = try Query(parsing: queryString)
39+
return try await queryDB(query: query) // 'await' because this implicitly hops to DatabaseActor.shared
40+
}
41+
```
42+
43+
The concurrency library defines one global actor, `MainActor`, which
44+
represents the main thread of execution. It should be used for any code that
45+
must execute on the main thread, e.g., for updating UI.
46+
947
* [SE-0313][]:
1048

1149
Declarations inside an actor that would normally be actor-isolated can
@@ -8525,6 +8563,7 @@ Swift 1.0
85258563
[SE-0306]: <https://github.com/apple/swift-evolution/blob/main/proposals/0306-actors.md>
85268564
[SE-0310]: <https://github.com/apple/swift-evolution/blob/main/proposals/0310-effectful-readonly-properties.md>
85278565
[SE-0313]: <https://github.com/apple/swift-evolution/blob/main/proposals/0313-actor-isolation-control.md>
8566+
[SE-0316]: <https://github.com/apple/swift-evolution/blob/main/proposals/0316-global-actors.md>
85288567

85298568
[SR-75]: <https://bugs.swift.org/browse/SR-75>
85308569
[SR-106]: <https://bugs.swift.org/browse/SR-106>

include/swift/AST/Attr.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ CONTEXTUAL_SIMPLE_DECL_ATTR(isolated, Isolated,
581581
103)
582582

583583
SIMPLE_DECL_ATTR(globalActor, GlobalActor,
584-
OnClass | OnStruct | OnEnum | ConcurrencyOnly |
584+
OnClass | OnStruct | OnEnum |
585585
ABIStableToAdd | ABIBreakingToRemove |
586586
APIStableToAdd | APIBreakingToRemove,
587587
104)

test/IDE/complete_decl_attribute.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ struct MyStruct {}
9292
// KEYWORD3-NEXT: Keyword/None: usableFromInline[#Class Attribute#]; name=usableFromInline
9393
// KEYWORD3-NEXT: Keyword/None: propertyWrapper[#Class Attribute#]; name=propertyWrapper
9494
// KEYWORD3-NEXT: Keyword/None: resultBuilder[#Class Attribute#]; name=resultBuilder
95+
// KEYWORD3-NEXT: Keyword/None: globalActor[#Class Attribute#]; name=globalActor
9596
// KEYWORD3-NEXT: End completions
9697

9798
@#^KEYWORD3_2^#IB class C2 {}
@@ -108,6 +109,7 @@ struct MyStruct {}
108109
// KEYWORD4-NEXT: Keyword/None: frozen[#Enum Attribute#]; name=frozen
109110
// KEYWORD4-NEXT: Keyword/None: propertyWrapper[#Enum Attribute#]; name=propertyWrapper
110111
// KEYWORD4-NEXT: Keyword/None: resultBuilder[#Enum Attribute#]; name=resultBuilder
112+
// KEYWORD4-NEXT: Keyword/None: globalActor[#Enum Attribute#]; name=globalActor
111113
// KEYWORD4-NEXT: End completions
112114

113115

@@ -121,6 +123,7 @@ struct MyStruct {}
121123
// KEYWORD5-NEXT: Keyword/None: frozen[#Struct Attribute#]; name=frozen
122124
// KEYWORD5-NEXT: Keyword/None: propertyWrapper[#Struct Attribute#]; name=propertyWrapper
123125
// KEYWORD5-NEXT: Keyword/None: resultBuilder[#Struct Attribute#]; name=resultBuilder
126+
// KEYWORD5-NEXT: Keyword/None: globalActor[#Struct Attribute#]; name=globalActor
124127
// KEYWORD5-NEXT: End completions
125128

126129
@#^ON_GLOBALVAR^# var globalVar

0 commit comments

Comments
 (0)