Skip to content

Commit

Permalink
Expose WebWorkerTaskExecutor when compiling with toolchain < 6.1
Browse files Browse the repository at this point in the history
  • Loading branch information
kateinoigakukun committed Dec 2, 2024
1 parent 02a4dda commit e13fc44
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 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 @@ -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)

0 comments on commit e13fc44

Please sign in to comment.