|
2 | 2 | //
|
3 | 3 | // This source file is part of the Swift.org open source project
|
4 | 4 | //
|
5 |
| -// Copyright (c) 2021 Apple Inc. and the Swift project authors |
| 5 | +// Copyright (c) 2023 Apple Inc. and the Swift project authors |
6 | 6 | // Licensed under Apache License v2.0 with Runtime Library Exception
|
7 | 7 | //
|
8 | 8 | // See https://swift.org/LICENSE.txt for license information
|
|
11 | 11 | //===----------------------------------------------------------------------===//
|
12 | 12 |
|
13 | 13 | import Swift
|
| 14 | +import SwiftShims |
| 15 | + |
| 16 | +// ==== ----------------------------------------------------------------------- |
| 17 | +// MARK: Precondition executors |
| 18 | + |
| 19 | +/// Unconditionally if the current task is executing on the expected serial executor, |
| 20 | +/// and if not crash the program offering information about the executor mismatch. |
| 21 | +/// |
| 22 | +/// This function's effect varies depending on the build flag used: |
| 23 | +/// |
| 24 | +/// * In playgrounds and `-Onone` builds (the default for Xcode's Debug |
| 25 | +/// configuration), stops program execution in a debuggable state after |
| 26 | +/// printing `message`. |
| 27 | +/// |
| 28 | +/// * In `-O` builds (the default for Xcode's Release configuration), stops |
| 29 | +/// program execution. |
| 30 | +/// |
| 31 | +/// * In `-Ounchecked` builds, the optimizer may assume that this function is |
| 32 | +/// never called. Failure to satisfy that assumption is a serious |
| 33 | +/// programming error. |
| 34 | +/// |
| 35 | +/// - Parameter executor: the expected current executor |
| 36 | +@available(SwiftStdlib 5.9, *) // FIXME: use @backDeploy(before: SwiftStdlib 5.9) |
| 37 | +public |
| 38 | +func preconditionTaskOnExecutor( |
| 39 | + _ executor: some SerialExecutor, |
| 40 | + message: @autoclosure () -> String = String(), |
| 41 | + file: StaticString = #fileID, line: UInt = #line |
| 42 | +) { |
| 43 | + guard _isDebugAssertConfiguration() || _isReleaseAssertConfiguration() else { |
| 44 | + return |
| 45 | + } |
| 46 | + |
| 47 | + let expectationCheck = _taskIsCurrentExecutor(executor.asUnownedSerialExecutor().executor) |
| 48 | + |
| 49 | + /// TODO: implement the logic in-place perhaps rather than delegating to precondition()? |
| 50 | + precondition(expectationCheck, |
| 51 | + // TODO: offer information which executor we actually got |
| 52 | + "Incorrect actor executor assumption; Expected '\(executor)' executor. \(message())", |
| 53 | + file: file, line: line) // short-cut so we get the exact same failure reporting semantics |
| 54 | +} |
| 55 | + |
| 56 | +/// Unconditionally if the current task is executing on the serial executor of the passed in `actor`, |
| 57 | +/// and if not crash the program offering information about the executor mismatch. |
| 58 | +/// |
| 59 | +/// This function's effect varies depending on the build flag used: |
| 60 | +/// |
| 61 | +/// * In playgrounds and `-Onone` builds (the default for Xcode's Debug |
| 62 | +/// configuration), stops program execution in a debuggable state after |
| 63 | +/// printing `message`. |
| 64 | +/// |
| 65 | +/// * In `-O` builds (the default for Xcode's Release configuration), stops |
| 66 | +/// program execution. |
| 67 | +/// |
| 68 | +/// * In `-Ounchecked` builds, the optimizer may assume that this function is |
| 69 | +/// never called. Failure to satisfy that assumption is a serious |
| 70 | +/// programming error. |
| 71 | +/// |
| 72 | +/// - Parameter actor: the actor whose serial executor we expect to be the current executor |
| 73 | +@available(SwiftStdlib 5.9, *) // FIXME: use @backDeploy(before: SwiftStdlib 5.9) |
| 74 | +public |
| 75 | +func preconditionTaskOnActorExecutor( |
| 76 | + _ actor: some Actor, |
| 77 | + message: @autoclosure () -> String = String(), |
| 78 | + file: StaticString = #fileID, line: UInt = #line |
| 79 | +) { |
| 80 | + guard _isDebugAssertConfiguration() || _isReleaseAssertConfiguration() else { |
| 81 | + return |
| 82 | + } |
| 83 | + |
| 84 | + let expectationCheck = _taskIsCurrentExecutor(actor.unownedExecutor.executor) |
| 85 | + |
| 86 | + // TODO: offer information which executor we actually got |
| 87 | + precondition(expectationCheck, |
| 88 | + // TODO: figure out a way to get the typed repr out of the unowned executor |
| 89 | + "Incorrect actor executor assumption; Expected '\(actor.unownedExecutor)' executor. \(message())", |
| 90 | + file: file, line: line) |
| 91 | +} |
| 92 | + |
| 93 | +// ==== ----------------------------------------------------------------------- |
| 94 | +// MARK: Assert executors |
| 95 | + |
| 96 | +/// Performs an executor check in debug builds. |
| 97 | +/// |
| 98 | +/// * In playgrounds and `-Onone` builds (the default for Xcode's Debug |
| 99 | +/// configuration): If `condition` evaluates to `false`, stop program |
| 100 | +/// execution in a debuggable state after printing `message`. |
| 101 | +/// |
| 102 | +/// * In `-O` builds (the default for Xcode's Release configuration), |
| 103 | +/// `condition` is not evaluated, and there are no effects. |
| 104 | +/// |
| 105 | +/// * In `-Ounchecked` builds, `condition` is not evaluated, but the optimizer |
| 106 | +/// may assume that it *always* evaluates to `true`. Failure to satisfy that |
| 107 | +/// assumption is a serious programming error. |
| 108 | +/// |
| 109 | +/// - Parameter executor: the expected current executor |
| 110 | +@available(SwiftStdlib 5.9, *) // FIXME: use @backDeploy(before: SwiftStdlib 5.9) |
| 111 | +public |
| 112 | +func assertTaskOnExecutor( |
| 113 | + _ executor: some SerialExecutor, |
| 114 | + _ message: @autoclosure () -> String = String(), |
| 115 | + file: StaticString = #fileID, line: UInt = #line |
| 116 | +) { |
| 117 | + guard _isDebugAssertConfiguration() else { |
| 118 | + return |
| 119 | + } |
| 120 | + |
| 121 | + guard _taskIsCurrentExecutor(executor.asUnownedSerialExecutor().executor) else { |
| 122 | + // TODO: offer information which executor we actually got |
| 123 | + let msg = "Incorrect actor executor assumption; Expected '\(executor)' executor. \(message())" |
| 124 | + /// TODO: implement the logic in-place perhaps rather than delegating to precondition()? |
| 125 | + assertionFailure(msg, file: file, line: line) |
| 126 | + return |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +/// Performs an executor check in debug builds. |
| 131 | +/// |
| 132 | +/// * In playgrounds and `-Onone` builds (the default for Xcode's Debug |
| 133 | +/// configuration): If `condition` evaluates to `false`, stop program |
| 134 | +/// execution in a debuggable state after printing `message`. |
| 135 | +/// |
| 136 | +/// * In `-O` builds (the default for Xcode's Release configuration), |
| 137 | +/// `condition` is not evaluated, and there are no effects. |
| 138 | +/// |
| 139 | +/// * In `-Ounchecked` builds, `condition` is not evaluated, but the optimizer |
| 140 | +/// may assume that it *always* evaluates to `true`. Failure to satisfy that |
| 141 | +/// assumption is a serious programming error. |
| 142 | +/// |
| 143 | +/// |
| 144 | +/// - Parameter actor: the actor whose serial executor we expect to be the current executor |
| 145 | +@available(SwiftStdlib 5.9, *) // FIXME: use @backDeploy(before: SwiftStdlib 5.9) |
| 146 | +public |
| 147 | +func assertTaskOnActorExecutor( |
| 148 | + _ actor: some Actor, |
| 149 | + _ message: @autoclosure () -> String = String(), |
| 150 | + file: StaticString = #fileID, line: UInt = #line |
| 151 | +) { |
| 152 | + guard _isDebugAssertConfiguration() else { |
| 153 | + return |
| 154 | + } |
| 155 | + |
| 156 | + guard _taskIsCurrentExecutor(actor.unownedExecutor.executor) else { |
| 157 | + // TODO: offer information which executor we actually got |
| 158 | + // TODO: figure out a way to get the typed repr out of the unowned executor |
| 159 | + let msg = "Incorrect actor executor assumption; Expected '\(actor.unownedExecutor)' executor. \(message())" |
| 160 | + /// TODO: implement the logic in-place perhaps rather than delegating to precondition()? |
| 161 | + assertionFailure(msg, file: file, line: line) // short-cut so we get the exact same failure reporting semantics |
| 162 | + return |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +// ==== ----------------------------------------------------------------------- |
| 167 | +// MARK: Assume Executor |
14 | 168 |
|
15 | 169 | /// A safe way to synchronously assume that the current execution context belongs to the MainActor.
|
16 | 170 | ///
|
|
0 commit comments