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: proposals/NNNN-custom-isolation-checking-for-serialexecutor.md
+36-12Lines changed: 36 additions & 12 deletions
Original file line number
Diff line number
Diff line change
@@ -91,18 +91,42 @@ This proposal adds another customization point to the Swift concurrency runtime
91
91
92
92
### Extended executor comparison mechanism
93
93
94
-
With this proposal, the logic for checking if the current executor is the same as an expected executor changes as follows:
95
-
96
-
- obtain current executor
97
-
- if no current executor exists, use heurystics to detect the "main actor" executor
98
-
- These heurystics could be removed by using this proposal's `checkIsolation()` API, however we'll first need to expose the MainActor's SerialExecutor as a global property which this proposal does not cover. Please see **Future Directions** for more discussion of this topic.
99
-
- if a current executor exists, perform basic object comparison between them
100
-
- if unable to prove the executors are equal:
101
-
- compare the executors using "complex equality" (see [SE-0392: Custom Actor Executors](https://github.com/apple/swift-evolution/blob/main/proposals/0392-custom-actor-executors.md) for a detailed description of complex exector equality)
102
-
- if still unable to prove the executors are equal:
103
-
-:arrow_right: call the expected executor's `checkIsolation()` method
104
-
105
-
The last step of this used to be just to unconditionally fail the comparison, leaving no space for an executor to take over and use whatever its own tracking -- usually expressed using thread-locals the executor sets as it creates its own worker thread -- to actually save the comparison from failing.
94
+
With this proposal, the logic for checking if the current executor is the same as an expected executor changes, and can be expressed using the following pseudo-code:
95
+
96
+
```swift
97
+
// !!!! PSEUDO-CODE !!!! Simplified for readability.
98
+
99
+
let current = Task.current.executor
100
+
101
+
guardlet current else {
102
+
// no current executor, last effort check performed by the expected executor:
103
+
expected.checkIsolated()
104
+
105
+
// e.g. MainActor:
106
+
// MainActorExecutor.checkIsolated() {
107
+
// guard Thread.isMain else { fatalError("Expected main thread!")
108
+
// return // ok!
109
+
// }
110
+
}
111
+
112
+
ifisSameSerialExecutor(current, expected) {
113
+
// comparison takes into account "complex equality" as introduced by 'SE-0392
114
+
return// ok!
115
+
} else {
116
+
// executor comparisons failed...
117
+
118
+
// give the expected executor a last chance to check isolation by itself:
119
+
expected.checkIsolated()
120
+
121
+
// as the default implementation of checkIsolated is to unconditionally crash,
122
+
// this call usually will result in crashing -- as expected.
123
+
}
124
+
125
+
return// ok, it seems the expected executor was able to prove isolation
126
+
```
127
+
128
+
This pseudo code snippet explains the flow of the executor comparisons. There are two situations in which the new `checkIsolated` method can be invoked: when there is no current executor present, or if all other comparisons have failed.
129
+
For more details on the executor comparison logic you can refer to [SE-0392: Custom Actor Executors'](https://github.com/apple/swift-evolution/blob/main/proposals/0392-custom-actor-executors.md).
106
130
107
131
Specific use-cases of this API include `DispatchSerialQueue`, which would be able to implement the requirement as follows:
0 commit comments