forked from denoland/deno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
thread_safe_test.js
107 lines (83 loc) · 2.7 KB
/
thread_safe_test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
// deno-lint-ignore-file
const targetDir = Deno.execPath().replace(/[^\/\\]+$/, "");
const [libPrefix, libSuffix] = {
darwin: ["lib", "dylib"],
linux: ["lib", "so"],
windows: ["", "dll"],
}[Deno.build.os];
const libPath = `${targetDir}/${libPrefix}test_ffi.${libSuffix}`;
const resourcesPre = Deno.resources();
const dylib = Deno.dlopen(libPath, {
store_function: {
parameters: ["function"],
result: "void",
},
call_stored_function: {
parameters: [],
result: "void",
},
call_stored_function_thread_safe: {
parameters: [],
result: "void",
},
});
let resolveWorker;
let workerResponsePromise;
const worker = new Worker(
new URL("./thread_safe_test_worker.js", import.meta.url).href,
{ type: "module" },
);
worker.addEventListener("message", () => {
if (resolveWorker) {
resolveWorker();
}
});
const sendWorkerMessage = async (data) => {
workerResponsePromise = new Promise((res) => {
resolveWorker = res;
});
worker.postMessage(data);
await workerResponsePromise;
};
// Test step 1: Register main thread callback, trigger on worker thread
const mainThreadCallback = new Deno.UnsafeCallback(
{ parameters: [], result: "void" },
() => {
console.log("Callback on main thread");
},
);
mainThreadCallback.ref();
dylib.symbols.store_function(mainThreadCallback.pointer);
await sendWorkerMessage("call");
// Test step 2: Register on worker thread, trigger on main thread
await sendWorkerMessage("register");
dylib.symbols.call_stored_function();
// Unref both main and worker thread callbacks and terminate the worker: Note, the stored function pointer in lib is now dangling.
dylib.symbols.store_function(null);
mainThreadCallback.unref();
await sendWorkerMessage("unref");
worker.terminate();
// Test step 3: Register a callback that will be the only thing left keeping the isolate from exiting.
// Rely on it to keep Deno running until the callback comes in and unrefs the callback, after which Deno should exit.
const cleanupCallback = new Deno.UnsafeCallback(
{ parameters: [], result: "void" },
() => {
console.log("Callback being called");
// Defer the cleanup to give the spawned thread all the time it needs to properly shut down
setTimeout(() => cleanup(), 100);
},
);
cleanupCallback.ref();
function cleanup() {
cleanupCallback.unref();
dylib.symbols.store_function(null);
mainThreadCallback.close();
cleanupCallback.close();
console.log("Isolate should now exit");
}
dylib.symbols.store_function(cleanupCallback.pointer);
console.log(
"Calling callback, isolate should stay asleep until callback is called",
);
dylib.symbols.call_stored_function_thread_safe();