Skip to content

Commit 616284a

Browse files
authored
Merge pull request swiftwasm#152 from yonihemi/single_install_executor
Prevent installGlobalExecutor() from running more than once
2 parents 1034940 + 35b0677 commit 616284a

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

Sources/JavaScriptEventLoop/JavaScriptEventLoop.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,15 @@ public final class JavaScriptEventLoop: SerialExecutor, @unchecked Sendable {
4848
return eventLoop
4949
}()
5050

51+
private static var didInstallGlobalExecutor = false
52+
5153
/// Set JavaScript event loop based executor to be the global executor
5254
/// Note that this should be called before any of the jobs are created.
5355
/// This installation step will be unnecessary after the custom-executor will be introduced officially.
5456
/// See also: https://github.com/rjmccall/swift-evolution/blob/custom-executors/proposals/0000-custom-executors.md#the-default-global-concurrent-executor
5557
public static func installGlobalExecutor() {
58+
guard !didInstallGlobalExecutor else { return }
59+
5660
typealias swift_task_enqueueGlobal_hook_Fn = @convention(thin) (UnownedJob, swift_task_enqueueGlobal_original) -> Void
5761
let swift_task_enqueueGlobal_hook_impl: swift_task_enqueueGlobal_hook_Fn = { job, original in
5862
JavaScriptEventLoop.shared.enqueue(job)
@@ -70,6 +74,8 @@ public final class JavaScriptEventLoop: SerialExecutor, @unchecked Sendable {
7074
JavaScriptEventLoop.shared.enqueue(job)
7175
}
7276
swift_task_enqueueMainExecutor_hook = unsafeBitCast(swift_task_enqueueMainExecutor_hook_impl, to: UnsafeMutableRawPointer?.self)
77+
78+
didInstallGlobalExecutor = true
7379
}
7480

7581
private func enqueue(_ job: UnownedJob, withDelay nanoseconds: UInt64) {

0 commit comments

Comments
 (0)