From d1f372f0522558dac6e8dec9e3201a4c3325d3bc Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Sun, 13 May 2018 19:39:32 +0200 Subject: [PATCH] worker: add `SharedArrayBuffer` sharing Logic is added to the `MessagePort` mechanism that attaches hidden objects to those instances when they are transferred that track their lifetime and maintain a reference count, to make sure that memory is freed at the appropriate times. Thanks to Stephen Belanger for reviewing this change in its original PR. Refs: https://github.com/ayojs/ayo/pull/106 PR-URL: https://github.com/nodejs/node/pull/20876 Reviewed-By: Gireesh Punathil Reviewed-By: Benjamin Gruenbaum Reviewed-By: Shingo Inoue Reviewed-By: Matteo Collina Reviewed-By: Tiancheng "Timothy" Gu Reviewed-By: John-David Dalton Reviewed-By: Gus Caplan --- doc/api/worker.md | 15 +++- node.gyp | 2 + src/env.h | 2 + src/node_errors.h | 5 +- src/node_messaging.cc | 57 ++++++++++++- src/node_messaging.h | 6 +- src/sharedarraybuffer_metadata.cc | 129 ++++++++++++++++++++++++++++++ src/sharedarraybuffer_metadata.h | 67 ++++++++++++++++ 8 files changed, 274 insertions(+), 9 deletions(-) create mode 100644 src/sharedarraybuffer_metadata.cc create mode 100644 src/sharedarraybuffer_metadata.h diff --git a/doc/api/worker.md b/doc/api/worker.md index 6a391c5a9e3b19..974ff2e46710db 100644 --- a/doc/api/worker.md +++ b/doc/api/worker.md @@ -85,14 +85,16 @@ to stringify. `transferList` may be a list of `ArrayBuffer` and `MessagePort` objects. After transferring, they will not be usable on the sending side of the channel -anymore (even if they are not contained in `value`). +anymore (even if they are not contained in `value`). Unlike with +[child processes][], transferring handles such as network sockets is currently +not supported. + +If `value` contains [`SharedArrayBuffer`][] instances, those will be accessible +from either thread. They cannot be listed in `transferList`. `value` may still contain `ArrayBuffer` instances that are not in `transferList`; in that case, the underlying memory is copied rather than moved. -For more information on the serialization and deserialization mechanisms -behind this API, see the [serialization API of the `v8` module][v8.serdes]. - Because the object cloning uses the structured clone algorithm, non-enumerable properties, property accessors, and object prototypes are not preserved. In particular, [`Buffer`][] objects will be read as @@ -101,6 +103,9 @@ plain [`Uint8Array`][]s on the receiving side. The message object will be cloned immediately, and can be modified after posting without having side effects. +For more information on the serialization and deserialization mechanisms +behind this API, see the [serialization API of the `v8` module][v8.serdes]. + ### port.ref()