|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const { |
| 4 | + AtomicsNotify, |
| 5 | + AtomicsStore, |
| 6 | + AtomicsWait, |
| 7 | + AtomicsWaitAsync, |
| 8 | + Int32Array, |
| 9 | + ObjectKeys, |
| 10 | +} = primordials; |
| 11 | + |
| 12 | +const { |
| 13 | + codes: { |
| 14 | + ERR_WORKER_MESSAGING_TIMEOUT, |
| 15 | + }, |
| 16 | +} = require('internal/errors'); |
| 17 | + |
| 18 | +const { read, write } = require('internal/worker/everysync/objects'); |
| 19 | +const { |
| 20 | + OFFSET, |
| 21 | + TO_MAIN, |
| 22 | + TO_WORKER, |
| 23 | +} = require('internal/worker/everysync/indexes'); |
| 24 | + |
| 25 | +/** |
| 26 | + * Creates a synchronous API facade from a shared memory buffer. |
| 27 | + * This function is meant to be used in the main thread to communicate with |
| 28 | + * a worker thread that has called `wire()` on the same shared memory. |
| 29 | + * @param {SharedArrayBuffer} data - The shared memory buffer for communication |
| 30 | + * @param {object} [opts={}] - Options object |
| 31 | + * @param {number} [opts.timeout=1000] - Timeout in milliseconds for synchronous operations |
| 32 | + * @returns {object} - An object with methods that match the ones exposed by the worker |
| 33 | + */ |
| 34 | +function makeSync(data, opts = {}) { |
| 35 | + const timeout = opts.timeout || 1000; |
| 36 | + const metaView = new Int32Array(data); |
| 37 | + |
| 38 | + const res = AtomicsWait(metaView, TO_WORKER, 0, timeout); |
| 39 | + AtomicsStore(metaView, TO_WORKER, 0); |
| 40 | + |
| 41 | + if (res === 'ok') { |
| 42 | + const obj = read(data, OFFSET); |
| 43 | + |
| 44 | + const api = {}; |
| 45 | + for (const key of obj) { |
| 46 | + api[key] = (...args) => { |
| 47 | + write(data, { key, args }, OFFSET); |
| 48 | + AtomicsStore(metaView, TO_MAIN, 1); |
| 49 | + AtomicsNotify(metaView, TO_MAIN, 1); |
| 50 | + const res = AtomicsWait(metaView, TO_WORKER, 0, timeout); |
| 51 | + AtomicsStore(metaView, TO_WORKER, 0); |
| 52 | + if (res === 'ok') { |
| 53 | + const obj = read(data, OFFSET); |
| 54 | + return obj; |
| 55 | + } |
| 56 | + throw new ERR_WORKER_MESSAGING_TIMEOUT(); |
| 57 | + }; |
| 58 | + } |
| 59 | + |
| 60 | + return api; |
| 61 | + } |
| 62 | + throw new ERR_WORKER_MESSAGING_TIMEOUT(); |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * Wires up a shared memory buffer to invoke methods on an object. |
| 67 | + * This function is meant to be used in a worker thread to expose methods |
| 68 | + * to the main thread that has called `makeSync()` on the same shared memory. |
| 69 | + * @param {SharedArrayBuffer} data - The shared memory buffer for communication |
| 70 | + * @param {object} obj - Object with methods to expose to the main thread |
| 71 | + * @returns {Promise<void>} - A promise that never resolves unless there's an error |
| 72 | + */ |
| 73 | +async function wire(data, obj) { |
| 74 | + write(data, ObjectKeys(obj), OFFSET); |
| 75 | + |
| 76 | + const metaView = new Int32Array(data); |
| 77 | + |
| 78 | + AtomicsStore(metaView, TO_WORKER, 1); |
| 79 | + AtomicsNotify(metaView, TO_WORKER); |
| 80 | + |
| 81 | + while (true) { |
| 82 | + const waitAsync = AtomicsWaitAsync(metaView, TO_MAIN, 0); |
| 83 | + const res = await waitAsync.value; |
| 84 | + AtomicsStore(metaView, TO_MAIN, 0); |
| 85 | + |
| 86 | + if (res === 'ok') { |
| 87 | + const { key, args } = read(data, OFFSET); |
| 88 | + // This is where the magic happens - invoke the requested method |
| 89 | + const result = await obj[key](...args); |
| 90 | + write(data, result, OFFSET); |
| 91 | + AtomicsStore(metaView, TO_WORKER, 1); |
| 92 | + AtomicsNotify(metaView, TO_WORKER, 1); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +module.exports = { |
| 98 | + makeSync, |
| 99 | + wire, |
| 100 | +}; |
0 commit comments