(startArg, threadId)
| 64 | return proc_exit.call(this, code); |
| 65 | }; |
| 66 | const spawn = (startArg, threadId) => { |
| 67 | const tid = nextTid++; |
| 68 | const name = `pthread-${tid}`; |
| 69 | const sab = new SharedArrayBuffer(8 + 8192); |
| 70 | const result = new Int32Array(sab); |
| 71 | |
| 72 | const workerData = { |
| 73 | name, |
| 74 | startArg, |
| 75 | tid, |
| 76 | wasmModule, |
| 77 | memory: importObject.env.memory, |
| 78 | result, |
| 79 | }; |
| 80 | |
| 81 | const worker = new Worker(__filename, { |
| 82 | name, |
| 83 | argv: process.argv.slice(2), |
| 84 | execArgv: [ |
| 85 | '--experimental-wasi-unstable-preview1', |
| 86 | ], |
| 87 | workerData, |
| 88 | }); |
| 89 | workers[tid] = worker; |
| 90 | |
| 91 | worker.on('message', ({ cmd, startArg, threadId, tid }) => { |
| 92 | if (cmd === 'loaded') { |
| 93 | worker.unref(); |
| 94 | } else if (cmd === 'thread-spawn') { |
| 95 | spawn(startArg, threadId); |
| 96 | } else if (cmd === 'cleanup-thread') { |
| 97 | workers[tid].terminate(); |
| 98 | delete workers[tid]; |
| 99 | } else if (cmd === 'terminate-all-threads') { |
| 100 | terminateAllThreads(); |
| 101 | } |
| 102 | }); |
| 103 | |
| 104 | worker.on('error', (e) => { |
| 105 | terminateAllThreads(); |
| 106 | throw new Error(e); |
| 107 | }); |
| 108 | |
| 109 | const r = Atomics.wait(result, 0, 0, 1000); |
| 110 | if (r === 'timed-out') { |
| 111 | workers[tid].terminate(); |
| 112 | delete workers[tid]; |
| 113 | if (threadId) { |
| 114 | Atomics.store(threadId, 0, -6); |
| 115 | Atomics.notify(threadId, 0); |
| 116 | } |
| 117 | return -6; |
| 118 | } |
| 119 | if (Atomics.load(result, 0) !== 0) { |
| 120 | const decoder = new TextDecoder(); |
| 121 | const nameLength = Atomics.load(result, 1); |
| 122 | const messageLength = Atomics.load(result, 2); |
| 123 | const stackLength = Atomics.load(result, 3); |
searching dependent graphs…