|
| 1 | +// Copyright 2021 Google LLC. Use of this source code is governed by an |
| 2 | +// MIT-style license that can be found in the LICENSE file or at |
| 3 | +// https://opensource.org/licenses/MIT. |
| 4 | + |
| 5 | +import {strict as assert} from 'assert'; |
| 6 | +import {EventEmitter} from 'events'; |
| 7 | +import { |
| 8 | + receiveMessageOnPort, |
| 9 | + MessageChannel, |
| 10 | + MessagePort, |
| 11 | + TransferListItem, |
| 12 | +} from 'worker_threads'; |
| 13 | + |
| 14 | +// TODO(nex3): Make this its own package. |
| 15 | + |
| 16 | +/** |
| 17 | + * An enum of possible states for the shared buffer that two `SyncMessagePort`s |
| 18 | + * use to communicate. |
| 19 | + */ |
| 20 | +enum BufferState { |
| 21 | + /** |
| 22 | + * The initial state. When an endpoint is ready to receive messages, it'll set |
| 23 | + * the buffer to this state so that it can use `Atomics.wait()` to be notified |
| 24 | + * when it switches to `MessageSent`. |
| 25 | + */ |
| 26 | + AwaitingMessage, |
| 27 | + /** |
| 28 | + * The state indicating that a message has been sent. Whenever an endpoint |
| 29 | + * sends a message, it'll set the buffer to this state so that the other |
| 30 | + * endpoint's `Atomics.wait()` call terminates. |
| 31 | + */ |
| 32 | + MessageSent, |
| 33 | + /** |
| 34 | + * The state indicating that the channel has been closed. This never |
| 35 | + * transitions to any other states. |
| 36 | + */ |
| 37 | + Closed, |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * A communication port that can receive messages synchronously from another |
| 42 | + * `SyncMessagePort`. |
| 43 | + * |
| 44 | + * This also emits the same asynchronous events as `MessagePort`. |
| 45 | + */ |
| 46 | +export class SyncMessagePort extends EventEmitter { |
| 47 | + /** Creates a channel whose ports can be passed to `new SyncMessagePort()`. */ |
| 48 | + static createChannel(): MessageChannel { |
| 49 | + const channel = new MessageChannel(); |
| 50 | + // Four bytes is the minimum necessary to use `Atomics.wait()`. |
| 51 | + const buffer = new SharedArrayBuffer(4); |
| 52 | + |
| 53 | + // Queue up messages on each port so the caller doesn't have to explicitly |
| 54 | + // pass the buffer around along with them. |
| 55 | + channel.port1.postMessage(buffer); |
| 56 | + channel.port2.postMessage(buffer); |
| 57 | + return channel; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * An Int32 view of the shared buffer. |
| 62 | + * |
| 63 | + * Each port sets this to `BufferState.AwaitingMessage` before checking for |
| 64 | + * new messages in `receiveMessage()`, and each port sets it to |
| 65 | + * `BufferState.MessageSent` after sending a new message. It's set to |
| 66 | + * `BufferState.Closed` when the channel is closed. |
| 67 | + */ |
| 68 | + private readonly buffer: Int32Array; |
| 69 | + |
| 70 | + /** |
| 71 | + * Creates a new message port. The `port` must be created by |
| 72 | + * `SyncMessagePort.createChannel()` and must connect to a port passed to |
| 73 | + * another `SyncMessagePort` in another worker. |
| 74 | + */ |
| 75 | + constructor(private readonly port: MessagePort) { |
| 76 | + super(); |
| 77 | + |
| 78 | + const buffer = receiveMessageOnPort(this.port)?.message; |
| 79 | + if (!buffer) { |
| 80 | + throw new Error( |
| 81 | + 'new SyncMessagePort() must be passed a port from ' + |
| 82 | + 'SyncMessagePort.createChannel().' |
| 83 | + ); |
| 84 | + } |
| 85 | + this.buffer = new Int32Array(buffer as SharedArrayBuffer); |
| 86 | + |
| 87 | + this.on('newListener', (event, listener) => { |
| 88 | + this.port.on(event, listener); |
| 89 | + }); |
| 90 | + this.on('removeListener', (event, listener) => |
| 91 | + this.port.removeListener(event, listener) |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + /** See `MessagePort.postMesage()`. */ |
| 96 | + postMessage(value: unknown, transferList?: TransferListItem[]): void { |
| 97 | + this.port.postMessage(value, transferList); |
| 98 | + |
| 99 | + // If the other port is waiting for a new message, notify it that the |
| 100 | + // message is ready. Use `Atomics.compareExchange` so that we don't |
| 101 | + // overwrite the "closed" state. |
| 102 | + if ( |
| 103 | + Atomics.compareExchange( |
| 104 | + this.buffer, |
| 105 | + 0, |
| 106 | + BufferState.AwaitingMessage, |
| 107 | + BufferState.MessageSent |
| 108 | + ) === BufferState.AwaitingMessage |
| 109 | + ) { |
| 110 | + Atomics.notify(this.buffer, 0); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + // TODO(nex3): |
| 115 | + // * Add a non-blocking `receiveMessage()` |
| 116 | + // * Add a timeout option to `receiveMessage()` |
| 117 | + // * Add an option to `receiveMessage()` to return a special value if the |
| 118 | + // channel is closed. |
| 119 | + |
| 120 | + /** |
| 121 | + * Blocks and returns the next message sent by the other port. |
| 122 | + * |
| 123 | + * This may not be called while this has a listener for the `'message'` event. |
| 124 | + * Throws an error if the channel is closed, including if it closes while this |
| 125 | + * is waiting for a message. |
| 126 | + */ |
| 127 | + receiveMessage(): unknown { |
| 128 | + if (this.listenerCount('message')) { |
| 129 | + throw new Error( |
| 130 | + 'SyncMessageChannel.receiveMessage() may not be called while there ' + |
| 131 | + 'are message listeners.' |
| 132 | + ); |
| 133 | + } |
| 134 | + |
| 135 | + // Set the "new message" indicator to zero before we check for new messages. |
| 136 | + // That way if the other port sets it to 1 between the call to |
| 137 | + // `receiveMessageOnPort` and the call to `Atomics.wait()`, we won't |
| 138 | + // overwrite it. Use `Atomics.compareExchange` so that we don't overwrite |
| 139 | + // the "closed" state. |
| 140 | + if ( |
| 141 | + Atomics.compareExchange( |
| 142 | + this.buffer, |
| 143 | + 0, |
| 144 | + BufferState.MessageSent, |
| 145 | + BufferState.AwaitingMessage |
| 146 | + ) === BufferState.Closed |
| 147 | + ) { |
| 148 | + throw new Error("The SyncMessagePort's channel is closed."); |
| 149 | + } |
| 150 | + |
| 151 | + let message = receiveMessageOnPort(this.port); |
| 152 | + if (message) return message.message; |
| 153 | + |
| 154 | + // If there's no new message, wait for the other port to flip the "new |
| 155 | + // message" indicator to 1. If it's been set to 1 since we stored 0, this |
| 156 | + // will terminate immediately. |
| 157 | + Atomics.wait(this.buffer, 0, BufferState.AwaitingMessage); |
| 158 | + message = receiveMessageOnPort(this.port); |
| 159 | + if (message) return message.message; |
| 160 | + |
| 161 | + assert.equal(Atomics.load(this.buffer, 0), BufferState.Closed); |
| 162 | + throw new Error("The SyncMessagePort's channel is closed."); |
| 163 | + } |
| 164 | + |
| 165 | + /** See `MessagePort.close()`. */ |
| 166 | + close(): void { |
| 167 | + Atomics.store(this.buffer, 0, BufferState.Closed); |
| 168 | + this.port.close(); |
| 169 | + } |
| 170 | +} |
0 commit comments