Skip to content

Commit 56dd749

Browse files
committed
[SE-0316] Global actors
Enable global actors by default. The proposal has been accepted and is implemented. (cherry picked from commit f5dc1a1)
1 parent 03a1f62 commit 56dd749

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

CHANGELOG.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,47 @@ CHANGELOG
2929
Swift 5.5
3030
---------
3131

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+
3270
* [SE-0313][]:
3371

34-
Declarations inside an actor that would normally by actor-isolated can
72+
Declarations inside an actor that would normally be actor-isolated can
3573
explicitly become non-isolated using the `nonisolated` keyword. Non-isolated
3674
declarations can be used to conform to synchronous protocol requirements:
3775

@@ -8535,6 +8573,7 @@ Swift 1.0
85358573
[SE-0306]: <https://github.com/apple/swift-evolution/blob/main/proposals/0306-actors.md>
85368574
[SE-0310]: <https://github.com/apple/swift-evolution/blob/main/proposals/0310-effectful-readonly-properties.md>
85378575
[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>
85388577

85398578
[SR-75]: <https://bugs.swift.org/browse/SR-75>
85408579
[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
@@ -585,7 +585,7 @@ DECL_ATTR(actorIndependent, ActorIndependent,
585585
103)
586586

587587
SIMPLE_DECL_ATTR(globalActor, GlobalActor,
588-
OnClass | OnStruct | OnEnum | ConcurrencyOnly |
588+
OnClass | OnStruct | OnEnum |
589589
ABIStableToAdd | ABIBreakingToRemove |
590590
APIStableToAdd | APIBreakingToRemove,
591591
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)