|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const { |
| 4 | + Int32Array, |
| 5 | + SafeMap, |
| 6 | + globalThis: { |
| 7 | + Atomics, |
| 8 | + SharedArrayBuffer, |
| 9 | + }, |
| 10 | +} = primordials; |
| 11 | + |
| 12 | +const { |
| 13 | + isMainThread, |
| 14 | + threadId: currentThreadId, |
| 15 | +} = internalBinding('worker'); |
| 16 | + |
| 17 | +const { |
| 18 | + codes: { |
| 19 | + ERR_WORKER_MESSAGING_ERRORED, |
| 20 | + ERR_WORKER_MESSAGING_FAILED, |
| 21 | + ERR_WORKER_MESSAGING_SAME_THREAD, |
| 22 | + ERR_WORKER_MESSAGING_TIMEOUT, |
| 23 | + }, |
| 24 | +} = require('internal/errors'); |
| 25 | + |
| 26 | +const { MessageChannel } = require('internal/worker/io'); |
| 27 | + |
| 28 | +const { validateNumber } = require('internal/validators'); |
| 29 | + |
| 30 | +const messageTypes = { |
| 31 | + REGISTER_MAIN_THREAD_PORT: 'registerMainThreadPort', |
| 32 | + UNREGISTER_MAIN_THREAD_PORT: 'unregisterMainThreadPort', |
| 33 | + SEND_MESSAGE_TO_WORKER: 'sendMessageToWorker', |
| 34 | + RECEIVE_MESSAGE_FROM_WORKER: 'receiveMessageFromWorker', |
| 35 | +}; |
| 36 | + |
| 37 | +// This is only populated by main thread and always empty in other threads |
| 38 | +const threadsPorts = new SafeMap(); |
| 39 | + |
| 40 | +// This is only populated in child threads and always undefined in main thread |
| 41 | +let mainThreadPort; |
| 42 | + |
| 43 | +// SharedArrayBuffer must always be Int32, so it's * 4. |
| 44 | +// We need one for the operation status (performing / performed) and one for the result (success / failure). |
| 45 | +const WORKER_MESSAGING_SHARED_DATA = 2 * 4; |
| 46 | +const WORKER_MESSAGING_STATUS_INDEX = 0; |
| 47 | +const WORKER_MESSAGING_RESULT_INDEX = 1; |
| 48 | + |
| 49 | +// Response codes |
| 50 | +const WORKER_MESSAGING_RESULT_DELIVERED = 0; |
| 51 | +const WORKER_MESSAGING_RESULT_NO_LISTENERS = 1; |
| 52 | +const WORKER_MESSAGING_RESULT_LISTENER_ERROR = 2; |
| 53 | + |
| 54 | +// This event handler is always executed on the main thread only |
| 55 | +function handleMessageFromThread(message) { |
| 56 | + switch (message.type) { |
| 57 | + case messageTypes.REGISTER_MAIN_THREAD_PORT: |
| 58 | + { |
| 59 | + const { threadId, port } = message; |
| 60 | + |
| 61 | + // Register the port |
| 62 | + threadsPorts.set(threadId, port); |
| 63 | + |
| 64 | + // Handle messages on this port |
| 65 | + // When a new thread wants to register a children |
| 66 | + // this take care of doing that. |
| 67 | + // This way any thread can be linked to the main one. |
| 68 | + port.on('message', handleMessageFromThread); |
| 69 | + |
| 70 | + // Never block the thread on this port |
| 71 | + port.unref(); |
| 72 | + } |
| 73 | + |
| 74 | + break; |
| 75 | + case messageTypes.UNREGISTER_MAIN_THREAD_PORT: |
| 76 | + threadsPorts.get(message.threadId).close(); |
| 77 | + threadsPorts.delete(message.threadId); |
| 78 | + break; |
| 79 | + case messageTypes.SEND_MESSAGE_TO_WORKER: |
| 80 | + { |
| 81 | + // Send the message to the target thread |
| 82 | + const { source, destination, value, transferList, memory } = message; |
| 83 | + sendMessageToWorker(source, destination, value, transferList, memory); |
| 84 | + } |
| 85 | + break; |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +function handleMessageFromMainThread(message) { |
| 90 | + switch (message.type) { |
| 91 | + case messageTypes.RECEIVE_MESSAGE_FROM_WORKER: |
| 92 | + receiveMessageFromWorker(message.source, message.value, message.memory); |
| 93 | + break; |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +function sendMessageToWorker(source, destination, value, transferList, memory) { |
| 98 | + if (SharedArrayBuffer === undefined || Atomics === undefined) { |
| 99 | + return; |
| 100 | + } |
| 101 | + |
| 102 | + // We are on the main thread, we can directly process the message |
| 103 | + if (destination === 0) { |
| 104 | + receiveMessageFromWorker(source, value, memory); |
| 105 | + return; |
| 106 | + } |
| 107 | + |
| 108 | + // Search the port to the target thread |
| 109 | + const port = threadsPorts.get(destination); |
| 110 | + |
| 111 | + if (!port) { |
| 112 | + const status = new Int32Array(memory); |
| 113 | + Atomics.store(status, WORKER_MESSAGING_RESULT_INDEX, WORKER_MESSAGING_RESULT_NO_LISTENERS); |
| 114 | + Atomics.store(status, WORKER_MESSAGING_STATUS_INDEX, 1); |
| 115 | + Atomics.notify(status, WORKER_MESSAGING_STATUS_INDEX, 1); |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + port.postMessage( |
| 120 | + { |
| 121 | + type: messageTypes.RECEIVE_MESSAGE_FROM_WORKER, |
| 122 | + source, |
| 123 | + destination, |
| 124 | + value, |
| 125 | + memory, |
| 126 | + }, |
| 127 | + transferList, |
| 128 | + ); |
| 129 | +} |
| 130 | + |
| 131 | +function receiveMessageFromWorker(source, value, memory) { |
| 132 | + if (SharedArrayBuffer === undefined || Atomics === undefined) { |
| 133 | + return; |
| 134 | + } |
| 135 | + |
| 136 | + let response = WORKER_MESSAGING_RESULT_NO_LISTENERS; |
| 137 | + |
| 138 | + try { |
| 139 | + if (process.emit('workerMessage', value, source)) { |
| 140 | + response = WORKER_MESSAGING_RESULT_DELIVERED; |
| 141 | + } |
| 142 | + } catch { |
| 143 | + response = WORKER_MESSAGING_RESULT_LISTENER_ERROR; |
| 144 | + } |
| 145 | + |
| 146 | + // Populate the result |
| 147 | + const status = new Int32Array(memory); |
| 148 | + Atomics.store(status, WORKER_MESSAGING_RESULT_INDEX, response); |
| 149 | + Atomics.store(status, WORKER_MESSAGING_STATUS_INDEX, 1); |
| 150 | + Atomics.notify(status, WORKER_MESSAGING_STATUS_INDEX, 1); |
| 151 | +} |
| 152 | + |
| 153 | +function createMainThreadPort(threadId) { |
| 154 | + // Create a channel that links the new thread to the main thread |
| 155 | + const { |
| 156 | + port1: mainThreadPortToMain, |
| 157 | + port2: mainThreadPortToThread, |
| 158 | + } = new MessageChannel(); |
| 159 | + |
| 160 | + const registrationMessage = { |
| 161 | + type: messageTypes.REGISTER_MAIN_THREAD_PORT, |
| 162 | + threadId, |
| 163 | + port: mainThreadPortToMain, |
| 164 | + }; |
| 165 | + |
| 166 | + if (isMainThread) { |
| 167 | + handleMessageFromThread(registrationMessage); |
| 168 | + } else { |
| 169 | + mainThreadPort.postMessage(registrationMessage, [mainThreadPortToMain]); |
| 170 | + } |
| 171 | + |
| 172 | + return mainThreadPortToThread; |
| 173 | +} |
| 174 | + |
| 175 | +function destroyMainThreadPort(threadId) { |
| 176 | + const unregistrationMessage = { |
| 177 | + type: messageTypes.UNREGISTER_MAIN_THREAD_PORT, |
| 178 | + threadId, |
| 179 | + }; |
| 180 | + |
| 181 | + if (isMainThread) { |
| 182 | + handleMessageFromThread(unregistrationMessage); |
| 183 | + } else { |
| 184 | + mainThreadPort.postMessage(unregistrationMessage); |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +function setupMainThreadPort(port) { |
| 189 | + mainThreadPort = port; |
| 190 | + mainThreadPort.on('message', handleMessageFromMainThread); |
| 191 | + |
| 192 | + // Never block the process on this port |
| 193 | + mainThreadPort.unref(); |
| 194 | +} |
| 195 | + |
| 196 | +async function postMessageToThread(threadId, value, transferList, timeout) { |
| 197 | + if (SharedArrayBuffer === undefined || Atomics === undefined) { |
| 198 | + return; |
| 199 | + } |
| 200 | + |
| 201 | + if (typeof transferList === 'number' && typeof timeout === 'undefined') { |
| 202 | + timeout = transferList; |
| 203 | + transferList = []; |
| 204 | + } |
| 205 | + |
| 206 | + if (typeof timeout !== 'undefined') { |
| 207 | + validateNumber(timeout, 'timeout', 0); |
| 208 | + } |
| 209 | + |
| 210 | + if (threadId === currentThreadId) { |
| 211 | + throw new ERR_WORKER_MESSAGING_SAME_THREAD(); |
| 212 | + } |
| 213 | + |
| 214 | + const memory = new SharedArrayBuffer(WORKER_MESSAGING_SHARED_DATA); |
| 215 | + const status = new Int32Array(memory); |
| 216 | + const promise = Atomics.waitAsync(status, WORKER_MESSAGING_STATUS_INDEX, 0, timeout).value; |
| 217 | + |
| 218 | + const message = { |
| 219 | + type: messageTypes.SEND_MESSAGE_TO_WORKER, |
| 220 | + source: currentThreadId, |
| 221 | + destination: threadId, |
| 222 | + value, |
| 223 | + memory, |
| 224 | + transferList, |
| 225 | + }; |
| 226 | + |
| 227 | + if (isMainThread) { |
| 228 | + handleMessageFromThread(message); |
| 229 | + } else { |
| 230 | + mainThreadPort.postMessage(message, transferList); |
| 231 | + } |
| 232 | + |
| 233 | + // Wait for the response |
| 234 | + const response = await promise; |
| 235 | + |
| 236 | + if (response === 'timed-out') { |
| 237 | + throw new ERR_WORKER_MESSAGING_TIMEOUT(); |
| 238 | + } else if (status[WORKER_MESSAGING_RESULT_INDEX] === WORKER_MESSAGING_RESULT_NO_LISTENERS) { |
| 239 | + throw new ERR_WORKER_MESSAGING_FAILED(); |
| 240 | + } else if (status[WORKER_MESSAGING_RESULT_INDEX] === WORKER_MESSAGING_RESULT_LISTENER_ERROR) { |
| 241 | + throw new ERR_WORKER_MESSAGING_ERRORED(); |
| 242 | + } |
| 243 | +} |
| 244 | + |
| 245 | +module.exports = { |
| 246 | + createMainThreadPort, |
| 247 | + destroyMainThreadPort, |
| 248 | + setupMainThreadPort, |
| 249 | + postMessageToThread, |
| 250 | +}; |
0 commit comments