Skip to content
Prev Previous commit
Next Next commit
Unify Embedded and non-Embedded code paths for `didInstallGlobalExecu…
…tor`
  • Loading branch information
kateinoigakukun committed May 1, 2025
commit 6bd0492f6ebe42aa303dd41aee7de09c24489c18
25 changes: 0 additions & 25 deletions Sources/JavaScriptEventLoop/JavaScriptEventLoop.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ import JavaScriptKit
import _Concurrency
import _CJavaScriptEventLoop
import _CJavaScriptKit

#if hasFeature(Embedded)
import Synchronization
#endif

// NOTE: `@available` annotations are semantically wrong, but they make it easier to develop applications targeting WebAssembly in Xcode.

Expand Down Expand Up @@ -109,39 +106,21 @@ public final class JavaScriptEventLoop: SerialExecutor, @unchecked Sendable {
return eventLoop
}

#if !hasFeature(Embedded)
@MainActor
private static var didInstallGlobalExecutor = false
#else
private static let didInstallGlobalExecutor = Atomic<Bool>(false)
#endif

/// Set JavaScript event loop based executor to be the global executor
/// Note that this should be called before any of the jobs are created.
/// This installation step will be unnecessary after custom executor are
/// introduced officially. See also [a draft proposal for custom
/// executors](https://github.com/rjmccall/swift-evolution/blob/custom-executors/proposals/0000-custom-executors.md#the-default-global-concurrent-executor)
public static func installGlobalExecutor() {
#if !hasFeature(Embedded)
MainActor.assumeIsolated {
Self.installGlobalExecutorIsolated()
}
#else
Self.installGlobalExecutorIsolated()
#endif
}

#if !hasFeature(Embedded)
@MainActor
#endif
private static func installGlobalExecutorIsolated() {
#if !hasFeature(Embedded)
guard !didInstallGlobalExecutor else { return }
#else
guard !didInstallGlobalExecutor.load(ordering: .sequentiallyConsistent) else {
return
}
#endif

#if compiler(>=5.9)
typealias swift_task_asyncMainDrainQueue_hook_Fn = @convention(thin) (
Expand Down Expand Up @@ -211,11 +190,7 @@ public final class JavaScriptEventLoop: SerialExecutor, @unchecked Sendable {
to: UnsafeMutableRawPointer?.self
)

#if !hasFeature(Embedded)
didInstallGlobalExecutor = true
#else
didInstallGlobalExecutor.store(true, ordering: .sequentiallyConsistent)
#endif
}

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