Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose WebWorkerTaskExecutor even if compiling with toolchain < 6.1 #277

Merged
merged 1 commit into from
Dec 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Expose WebWorkerTaskExecutor when compiling with toolchain < 6.1
  • Loading branch information
kateinoigakukun committed Dec 2, 2024
commit c574eedeceb52acab75929bad8bb0f3cab09adb0
18 changes: 12 additions & 6 deletions Sources/JavaScriptEventLoop/WebWorkerTaskExecutor.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#if compiler(>=6.1) && _runtime(_multithreaded) // @_expose and @_extern are only available in Swift 6.1+
#if compiler(>=6.0) // `TaskExecutor` is available since Swift 6.0

import JavaScriptKit
import _CJavaScriptKit
import _CJavaScriptEventLoop

import Synchronization
#if canImport(Synchronization)
import Synchronization
#endif
#if canImport(wasi_pthread)
import wasi_pthread
import WASILibc
Expand Down Expand Up @@ -282,7 +284,7 @@ public final class WebWorkerTaskExecutor: TaskExecutor {
}

func start(timeout: Duration, checkInterval: Duration) async throws {
#if canImport(wasi_pthread)
#if canImport(wasi_pthread) && compiler(>=6.1) && _runtime(_multithreaded)
class Context: @unchecked Sendable {
let executor: WebWorkerTaskExecutor.Executor
let worker: Worker
Expand Down Expand Up @@ -433,7 +435,7 @@ public final class WebWorkerTaskExecutor: TaskExecutor {
///
/// This function must be called once before using the Web Worker task executor.
public static func installGlobalExecutor() {
#if canImport(wasi_pthread)
#if canImport(wasi_pthread) && compiler(>=6.1) && _runtime(_multithreaded)
// Ensure this function is called only once.
guard _mainThread == nil else { return }

Expand Down Expand Up @@ -471,7 +473,9 @@ public final class WebWorkerTaskExecutor: TaskExecutor {
/// Enqueue a job scheduled from a Web Worker thread to the main thread.
/// This function is called when a job is enqueued from a Web Worker thread.
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
#if compiler(>=6.1) // @_expose and @_extern are only available in Swift 6.1+
@_expose(wasm, "swjs_enqueue_main_job_from_worker")
#endif
func _swjs_enqueue_main_job_from_worker(_ job: UnownedJob) {
WebWorkerTaskExecutor.traceStatsIncrement(\.receiveJobFromWorkerThread)
JavaScriptEventLoop.shared.enqueue(ExecutorJob(job))
Expand All @@ -480,15 +484,17 @@ func _swjs_enqueue_main_job_from_worker(_ job: UnownedJob) {
/// Wake up the worker thread.
/// This function is called when a job is enqueued from the main thread to a worker thread.
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
#if compiler(>=6.1) // @_expose and @_extern are only available in Swift 6.1+
@_expose(wasm, "swjs_wake_worker_thread")
#endif
func _swjs_wake_worker_thread() {
WebWorkerTaskExecutor.Worker.currentThread!.run()
}

#endif

fileprivate func trace(_ message: String) {
#if JAVASCRIPTKIT_TRACE
JSObject.global.process.stdout.write("[trace tid=\(swjs_get_worker_thread_id())] \(message)\n")
#endif
}

#endif // compiler(>=6.0)
Loading