Skip to content

Commit

Permalink
Serial executor warnings (#94)
Browse files Browse the repository at this point in the history
* wip

* Fix some executor warnings

* wip
  • Loading branch information
stephencelis authored May 26, 2023
1 parent 25c9b67 commit de1a984
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/combine-schedulers",
"state" : {
"revision" : "882ac01eb7ef9e36d4467eb4b1151e74fcef85ab",
"version" : "0.9.1"
"revision" : "0625932976b3ae23949f6b816d13bd97f3b40b7c",
"version" : "0.10.0"
}
},
{
Expand All @@ -32,8 +32,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-clocks",
"state" : {
"revision" : "20b25ca0dd88ebfb9111ec937814ddc5a8880172",
"version" : "0.2.0"
"revision" : "f9acfa1a45f4483fe0f2c434a74e6f68f865d12d",
"version" : "0.3.0"
}
},
{
Expand Down
28 changes: 16 additions & 12 deletions Sources/Dependencies/ConcurrencySupport/MainSerialExecutor.swift
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

0 comments on commit de1a984

Please sign in to comment.