Skip to content

Commit 358633c

Browse files
committedJul 6, 2024
Add termination callback for worker threads
1 parent 3a99951 commit 358633c

File tree

6 files changed

+35
-0
lines changed

6 files changed

+35
-0
lines changed
 

Diff for: ‎Runtime/src/index.ts

+12
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ export type SwiftRuntimeThreadChannel =
8080
tid: number,
8181
listener: (unownedJob: number) => void
8282
) => void;
83+
84+
/**
85+
* This function is expected to be set in the main thread and called
86+
* when the worker thread is terminated.
87+
*/
88+
terminateWorkerThread?: (tid: number) => void;
8389
};
8490

8591
export type SwiftRuntimeOptions = {
@@ -627,6 +633,12 @@ export class SwiftRuntime {
627633
);
628634
}
629635
},
636+
swjs_terminate_worker_thread: (tid) => {
637+
const threadChannel = this.options.threadChannel;
638+
if (threadChannel && "terminateWorkerThread" in threadChannel) {
639+
threadChannel.terminateWorkerThread?.(tid);
640+
} // Otherwise, just ignore the termination request
641+
},
630642
swjs_get_worker_thread_id: () => {
631643
// Main thread's tid is always -1
632644
return this.tid || -1;

Diff for: ‎Runtime/src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ export interface ImportedFunctions {
110110
swjs_listen_wake_event_from_main_thread: () => void;
111111
swjs_wake_up_worker_thread: (tid: number) => void;
112112
swjs_listen_main_job_from_worker_thread: (tid: number) => void;
113+
swjs_terminate_worker_thread: (tid: number) => void;
113114
swjs_get_worker_thread_id: () => number;
114115
}
115116

Diff for: ‎Sources/JavaScriptEventLoop/WebWorkerTaskExecutor.swift

+6
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,12 @@ public final class WebWorkerTaskExecutor: TaskExecutor {
245245
func terminate() {
246246
trace("Worker.terminate")
247247
state.store(.terminated, ordering: .sequentiallyConsistent)
248+
let tid = self.tid.load(ordering: .sequentiallyConsistent)
249+
guard tid != 0 else {
250+
// The worker is not started yet.
251+
return
252+
}
253+
swjs_terminate_worker_thread(tid)
248254
}
249255
}
250256

Diff for: ‎Sources/JavaScriptKit/Runtime/index.js

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: ‎Sources/JavaScriptKit/Runtime/index.mjs

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: ‎Sources/_CJavaScriptKit/include/_CJavaScriptKit.h

+2
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,8 @@ IMPORT_JS_FUNCTION(swjs_wake_up_worker_thread, void, (int tid))
303303

304304
IMPORT_JS_FUNCTION(swjs_listen_main_job_from_worker_thread, void, (int tid))
305305

306+
IMPORT_JS_FUNCTION(swjs_terminate_worker_thread, void, (int tid))
307+
306308
IMPORT_JS_FUNCTION(swjs_get_worker_thread_id, int, (void))
307309

308310
/// MARK: - thread local storage

0 commit comments

Comments
 (0)