|
1 | | -import Foundation |
| 1 | +#if !os(WASI) && !os(Windows) |
| 2 | + import Foundation |
2 | 3 |
|
3 | | -/// Perform an operation on the main serial executor. |
4 | | -/// |
5 | | -/// Some asynchronous code is [notoriously |
6 | | -/// difficult](https://forums.swift.org/t/reliably-testing-code-that-adopts-swift-concurrency/57304) |
7 | | -/// to test in Swift due to how suspension points are processed by the runtime. This function runs |
8 | | -/// all tasks spawned in the given operation serially and deterministically. It makes asynchronous |
9 | | -/// tests faster and less flakey. |
10 | | -/// |
11 | | -/// ```swift |
12 | | -/// await withMainSerialExecutor { |
13 | | -/// // Everything performed in this scope is performed serially... |
14 | | -/// } |
15 | | -/// ``` |
16 | | -/// |
17 | | -/// - Parameter operation: An operation to be performed on the main serial executor. |
18 | | -@MainActor |
19 | | -public func withMainSerialExecutor( |
20 | | - @_implicitSelfCapture operation: @MainActor @Sendable () async throws -> Void |
21 | | -) async rethrows { |
22 | | - let didUseMainSerialExecutor = uncheckedUseMainSerialExecutor |
23 | | - defer { uncheckedUseMainSerialExecutor = didUseMainSerialExecutor } |
24 | | - uncheckedUseMainSerialExecutor = true |
25 | | - try await operation() |
26 | | -} |
| 4 | + /// Perform an operation on the main serial executor. |
| 5 | + /// |
| 6 | + /// Some asynchronous code is [notoriously |
| 7 | + /// difficult](https://forums.swift.org/t/reliably-testing-code-that-adopts-swift-concurrency/57304) |
| 8 | + /// to test in Swift due to how suspension points are processed by the runtime. This function runs |
| 9 | + /// all tasks spawned in the given operation serially and deterministically. It makes asynchronous |
| 10 | + /// tests faster and less flakey. |
| 11 | + /// |
| 12 | + /// ```swift |
| 13 | + /// await withMainSerialExecutor { |
| 14 | + /// // Everything performed in this scope is performed serially... |
| 15 | + /// } |
| 16 | + /// ``` |
| 17 | + /// |
| 18 | + /// - Parameter operation: An operation to be performed on the main serial executor. |
| 19 | + @MainActor |
| 20 | + public func withMainSerialExecutor( |
| 21 | + @_implicitSelfCapture operation: @MainActor @Sendable () async throws -> Void |
| 22 | + ) async rethrows { |
| 23 | + let didUseMainSerialExecutor = uncheckedUseMainSerialExecutor |
| 24 | + defer { uncheckedUseMainSerialExecutor = didUseMainSerialExecutor } |
| 25 | + uncheckedUseMainSerialExecutor = true |
| 26 | + try await operation() |
| 27 | + } |
27 | 28 |
|
28 | | -/// Perform an operation on the main serial executor. |
29 | | -/// |
30 | | -/// A synchronous version of ``withMainSerialExecutor(operation:)-79jpc`` that can be used in |
31 | | -/// `XCTestCase.invokeTest` to ensure all async tests are performed serially: |
32 | | -/// |
33 | | -/// ```swift |
34 | | -/// class BaseTestCase: XCTestCase { |
35 | | -/// override func invokeTest() { |
36 | | -/// withMainSerialExecutor { |
37 | | -/// super.invokeTest() |
38 | | -/// } |
39 | | -/// } |
40 | | -/// } |
41 | | -/// ``` |
42 | | -/// |
43 | | -/// - Parameter operation: An operation to be performed on the main serial executor. |
44 | | -public func withMainSerialExecutor( |
45 | | - @_implicitSelfCapture operation: () throws -> Void |
46 | | -) rethrows { |
47 | | - let didUseMainSerialExecutor = uncheckedUseMainSerialExecutor |
48 | | - defer { uncheckedUseMainSerialExecutor = didUseMainSerialExecutor } |
49 | | - uncheckedUseMainSerialExecutor = true |
50 | | - try operation() |
51 | | -} |
| 29 | + /// Perform an operation on the main serial executor. |
| 30 | + /// |
| 31 | + /// A synchronous version of ``withMainSerialExecutor(operation:)-79jpc`` that can be used in |
| 32 | + /// `XCTestCase.invokeTest` to ensure all async tests are performed serially: |
| 33 | + /// |
| 34 | + /// ```swift |
| 35 | + /// class BaseTestCase: XCTestCase { |
| 36 | + /// override func invokeTest() { |
| 37 | + /// withMainSerialExecutor { |
| 38 | + /// super.invokeTest() |
| 39 | + /// } |
| 40 | + /// } |
| 41 | + /// } |
| 42 | + /// ``` |
| 43 | + /// |
| 44 | + /// - Parameter operation: An operation to be performed on the main serial executor. |
| 45 | + public func withMainSerialExecutor( |
| 46 | + @_implicitSelfCapture operation: () throws -> Void |
| 47 | + ) rethrows { |
| 48 | + let didUseMainSerialExecutor = uncheckedUseMainSerialExecutor |
| 49 | + defer { uncheckedUseMainSerialExecutor = didUseMainSerialExecutor } |
| 50 | + uncheckedUseMainSerialExecutor = true |
| 51 | + try operation() |
| 52 | + } |
52 | 53 |
|
53 | | -/// Overrides Swift's global executor with the main serial executor in an unchecked fashion. |
54 | | -/// |
55 | | -/// > Warning: When set to `true`, all tasks will be enqueued on the main serial executor till set |
56 | | -/// > back to `false`. Consider using ``withMainSerialExecutor(operation:)-79jpc``, instead, which |
57 | | -/// > scopes this work to the duration of a given operation. |
58 | | -public var uncheckedUseMainSerialExecutor: Bool { |
59 | | - get { swift_task_enqueueGlobal_hook != nil } |
60 | | - set { |
61 | | - swift_task_enqueueGlobal_hook = |
62 | | - newValue |
63 | | - ? { job, _ in MainActor.shared.enqueue(job) } |
64 | | - : nil |
| 54 | + /// Overrides Swift's global executor with the main serial executor in an unchecked fashion. |
| 55 | + /// |
| 56 | + /// > Warning: When set to `true`, all tasks will be enqueued on the main serial executor till set |
| 57 | + /// > back to `false`. Consider using ``withMainSerialExecutor(operation:)-79jpc``, instead, which |
| 58 | + /// > scopes this work to the duration of a given operation. |
| 59 | + public var uncheckedUseMainSerialExecutor: Bool { |
| 60 | + get { swift_task_enqueueGlobal_hook != nil } |
| 61 | + set { |
| 62 | + swift_task_enqueueGlobal_hook = |
| 63 | + newValue |
| 64 | + ? { job, _ in MainActor.shared.enqueue(job) } |
| 65 | + : nil |
| 66 | + } |
65 | 67 | } |
66 | | -} |
67 | 68 |
|
68 | | -private typealias Original = @convention(thin) (UnownedJob) -> Void |
69 | | -private typealias Hook = @convention(thin) (UnownedJob, Original) -> Void |
| 69 | + private typealias Original = @convention(thin) (UnownedJob) -> Void |
| 70 | + private typealias Hook = @convention(thin) (UnownedJob, Original) -> Void |
70 | 71 |
|
71 | | -private var swift_task_enqueueGlobal_hook: Hook? { |
72 | | - get { _swift_task_enqueueGlobal_hook.pointee } |
73 | | - set { _swift_task_enqueueGlobal_hook.pointee = newValue } |
74 | | -} |
75 | | -private let _swift_task_enqueueGlobal_hook: UnsafeMutablePointer<Hook?> = |
76 | | - dlsym(dlopen(nil, 0), "swift_task_enqueueGlobal_hook").assumingMemoryBound(to: Hook?.self) |
| 72 | + private var swift_task_enqueueGlobal_hook: Hook? { |
| 73 | + get { _swift_task_enqueueGlobal_hook.pointee } |
| 74 | + set { _swift_task_enqueueGlobal_hook.pointee = newValue } |
| 75 | + } |
| 76 | + private let _swift_task_enqueueGlobal_hook: UnsafeMutablePointer<Hook?> = |
| 77 | + dlsym(dlopen(nil, 0), "swift_task_enqueueGlobal_hook").assumingMemoryBound(to: Hook?.self) |
| 78 | +#endif |
0 commit comments