|
2 | 2 |
|
3 | 3 | * Proposal: [SE-NNNN](...)
|
4 | 4 | * Author: [Konrad 'ktoso' Malawski](https://github.com/ktoso)
|
5 |
| -* Review Manager: |
6 |
| -* Status: **Partial implementation** |
7 |
| -* Implementation: https://github.com/swiftlang/swift/pull/79788 |
| 5 | +* Review Manager: TBD |
| 6 | +* Status: Implemented |
| 7 | + * https://github.com/swiftlang/swift/pull/79788 |
| 8 | + * https://github.com/swiftlang/swift/pull/79946 |
| 9 | + |
| 10 | +* Pitch: [[Pitch][SerialExecutor] Improved Custom SerialExecutor isolation checking](https://forums.swift.org/t/pitch-serialexecutor-improved-custom-serialexecutor-isolation-checking/78237/) |
8 | 11 | * Review: TODO
|
9 | 12 |
|
10 | 13 | ## Introduction
|
@@ -125,37 +128,15 @@ This proposal specifically adds the "if `isIsolatingCurrentContext` is available
|
125 | 128 |
|
126 | 129 | If `isIsolatingCurrentContext` is available, effectively it replaces `checkIsolated` because it does offer a sub-par error message experience and is not able to offer a warning if Swift would be asked to check the isolation but not crash upon discovering a violation.
|
127 | 130 |
|
128 |
| -### Enabling `isIsolatingCurrentContext` checking mode |
129 |
| - |
130 |
| -Similar as complex equality in `SerialExecutors` this feature must be opted into by flagging it when the `UnownedSerialExecutor` value is returned from a serial executor's `asUnownedSerialExecutor()`. |
131 |
| - |
132 |
| -Previously this was done by signalling the feature in the `UnownedSerialExecutor` initializer like this: |
133 |
| - |
134 |
| -```swift |
135 |
| -// Existing API |
136 |
| -public func asUnownedSerialExecutor() -> UnownedSerialExecutor { |
137 |
| - UnownedSerialExecutor(complexEquality: self) |
138 |
| -} |
139 |
| -``` |
140 |
| - |
141 |
| -Which enables the following `isSameExclusiveExecutionContext` check, which can only be used when a "current" executor is present, and cannot be used when running code outside of a Swift concurrency task (!): |
| 131 | +### Detecting the `isIsolatingCurrentContext` checking mode |
142 | 132 |
|
143 |
| -```swift |
144 |
| -// Existing API |
145 |
| -public func isSameExclusiveExecutionContext(other: NaiveQueueExecutor) -> Bool { |
146 |
| - other.secretIdentifier == self.secretIdentifier |
147 |
| -} |
148 |
| -``` |
| 133 | +The `isIsolatingCurrentContext` method effectively replaces the `checkIsolated` method, because it can answer the same question _if it is implemented_. |
149 | 134 |
|
150 |
| -In order to enable the runtime to call into the `isIsolatingCurrentContext` the `UnownedSerialExecutor` **must** be constructed as follows: |
| 135 | +Some runtimes may not be able to implement a the returning `isIsolatingCurrentContext`, and they are not expected to implement the new protocol requirement. |
151 | 136 |
|
152 |
| -```swift |
153 |
| -public func asUnownedSerialExecutor() -> UnownedSerialExecutor { |
154 |
| - UnownedSerialExecutor(hasIsIsolatingCurrentContext: self) |
155 |
| -} |
156 |
| -``` |
| 137 | +The general guidance about which method to implement is to implement `isIsolatingCurrentContext` whenever possible. This method can be used by the Swift runtime in "warning mode". When running a check in this mode, the `checkIsolated` method cannot and will not be used because it would cause an unexpected crash. |
157 | 138 |
|
158 |
| -This sets a flag inside the internal executor reference which makes the swift runtime call into the new `isIsolatingCurrentContext` function, rather than the other versions of isolation checking. In many ways this API is the most general of them all, and generally preferable _if_ your executor is using some kind of mechanism to track the "current" context that the Swift concurrency runtime cannot know about, e.g. like thread local values inside threads managed by your executor. |
| 139 | +The presence of a non-default implementation of the `isIsolatingCurrentContext` protocol requirement. In other words, if there is an implementation of the requirement available _other than_ the default one provided in the concurrency library, the runtime will attempt to use this method _over_ the `checkIsolated` API. This allows for a smooth migration to the new API, and enables the use of this method in if the runtime would like issue a check that cannot cause a crash. |
159 | 140 |
|
160 | 141 | ### Compatibility strategy for custom SerialExecutor authors
|
161 | 142 |
|
@@ -194,3 +175,7 @@ It would be ideal if this method could have been bool returning initially, but d
|
194 | 175 | ### Deprecate `checkIsolated`?
|
195 | 176 |
|
196 | 177 | In order to make adoption of this new mode less painful and not cause deprecation warnings to libraries which intend to support multiple versions of Swift, the `SerialExcecutor/checkIsolated` protocol requirement remains _not_ deprecated. It may eventually become deprecated in the future, but right now we have no plans of doing so.
|
| 178 | + |
| 179 | +## Changelog |
| 180 | + |
| 181 | +- removed the manual need to signal to the runtime that the specific executor supports the new checking mode. It is now detected by the compiler and runtime, checking for the presence of a non-default implementation of the protocol requirement. |
0 commit comments