-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
2 changed files
with
20 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 16 additions & 12 deletions
28
Sources/Dependencies/ConcurrencySupport/MainSerialExecutor.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,27 @@ | ||
#if !os(WASI) && !os(Windows) | ||
import Foundation | ||
|
||
@_spi(Concurrency) public func withMainSerialExecutor<T>( | ||
@_implicitSelfCapture operation: () async throws -> T | ||
@_spi(Concurrency) | ||
@MainActor | ||
public func withMainSerialExecutor<T>( | ||
@_implicitSelfCapture operation: @MainActor () async throws -> T | ||
) async rethrows -> T { | ||
guard let pointer = swift_task_enqueueGlobal_hook else { return try await operation() } | ||
let hook = pointer.pointee | ||
defer { pointer.pointee = hook } | ||
pointer.pointee = { job, original in | ||
let hook = swift_task_enqueueGlobal_hook | ||
defer { swift_task_enqueueGlobal_hook = hook } | ||
swift_task_enqueueGlobal_hook = { job, original in | ||
MainActor.shared.enqueue(job) | ||
} | ||
return try await operation() | ||
} | ||
|
||
// here be dragons | ||
private typealias Orig = @convention(thin) (UnownedJob) -> Void | ||
private typealias Hook = @convention(thin) (UnownedJob, Orig) -> Void | ||
private var swift_task_enqueueGlobal_hook: UnsafeMutablePointer<Hook>? = { | ||
dlsym(dlopen(nil, 0), "swift_task_enqueueGlobal_hook")? | ||
.assumingMemoryBound(to: Hook.self) | ||
typealias Original = @convention(thin) (UnownedJob) -> Void | ||
typealias Hook = @convention(thin) (UnownedJob, Original) -> Void | ||
private let _swift_task_enqueueGlobal_hook: UnsafeMutablePointer<Hook?> = { | ||
dlsym(dlopen(nil, 0), "swift_task_enqueueGlobal_hook").assumingMemoryBound(to: Hook?.self) | ||
}() | ||
|
||
var swift_task_enqueueGlobal_hook: Hook? { | ||
get { _swift_task_enqueueGlobal_hook.pointee } | ||
set { _swift_task_enqueueGlobal_hook.pointee = newValue } | ||
} | ||
#endif |