-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.ts
77 lines (65 loc) · 2.35 KB
/
worker.ts
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
// https://github.com/snowpackjs/snowpack/pull/3154
// version 3.0.0 of @snowpack/plugin-webpack should make workers easier
import type { WhichSort } from '../wasm-sorts';
interface Exports {
bubble_sort: (length: number) => void;
quick_sort_queue: (length: number) => void;
quick_sort_stack: (length: number) => void;
shell_sort: (length: number) => void;
memory: WebAssembly.Memory;
}
interface InitMessage {
module: WebAssembly.Module;
which: WhichSort;
length: number;
array: Int32Array;
}
self.onmessage = async ({ data: { module, which, length, array } }: MessageEvent<InitMessage>) => {
console.log('in worker onmessage');
function compare(i: number, j: number): number {
console.log('compare() called');
array[0] = 2;
postMessage({ type: 'compare', i, j });
console.log('sent comparison request');
Atomics.wait(array, 0, 2);
console.log('stopped waiting');
const result = array[0];
array[0] = 2;
return result;
}
function swap(i: number, j: number) {
array[0] = 0;
postMessage({ type: 'swap', i, j });
Atomics.wait(array, 0, 0);
array[0] = 0;
}
const { exports } = await WebAssembly.instantiate(module, {
env: {
compare,
swap,
print(str: number) {
const array = new Uint8Array(exports.memory.buffer, str, 256);
const string = new TextDecoder('utf8').decode(array);
console.log(string.split('\0')[0]);
},
print_number(n: number) {
console.log(n.toString());
}
},
}) as { exports: unknown } as { exports: Exports };
exports[which](length);
postMessage({ type: 'done' });
console.log('onmessage exited');
};
// parent worker gets message with wasm module, which sort, and length
// parent worker forwards that to child
// child worker launches wasm function
// when wasm wants to compare or swap:
// child worker posts message with the indices
// child worker calls Atomics.wait
// parent worker receives indices
// parent worker forwards them to main thread
// main thread runs animation, sends result to parent worker
// parent worker calls Atomics.notify
// wasm function returns
// worker posts message indicating that it's done