diff options
author | Anna Henningsen <anna@addaleax.net> | 2018-05-13 19:39:32 +0200 |
---|---|---|
committer | Michaƫl Zasso <targos@protonmail.com> | 2018-06-13 08:43:39 +0200 |
commit | d1f372f0522558dac6e8dec9e3201a4c3325d3bc (patch) | |
tree | a09175c02d5101525e3b378d01cc8e0432908618 /src/sharedarraybuffer_metadata.h | |
parent | f447acd87b67dc04b6081d212b79f444e001232f (diff) | |
download | node-new-d1f372f0522558dac6e8dec9e3201a4c3325d3bc.tar.gz |
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 <gpunathi@in.ibm.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>
Diffstat (limited to 'src/sharedarraybuffer_metadata.h')
-rw-r--r-- | src/sharedarraybuffer_metadata.h | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/src/sharedarraybuffer_metadata.h b/src/sharedarraybuffer_metadata.h new file mode 100644 index 0000000000..84bfd224fa --- /dev/null +++ b/src/sharedarraybuffer_metadata.h @@ -0,0 +1,67 @@ +#ifndef SRC_SHAREDARRAYBUFFER_METADATA_H_ +#define SRC_SHAREDARRAYBUFFER_METADATA_H_ + +#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS + +#include "node.h" +#include <memory> + +namespace node { +namespace worker { + +class SharedArrayBufferMetadata; + +// This is an object associated with a SharedArrayBuffer, which keeps track +// of a cross-thread reference count. Once a SharedArrayBuffer is transferred +// for the first time (or is attempted to be transferred), one of these objects +// is created, and the SharedArrayBuffer is moved from internalized mode into +// externalized mode (i.e. the JS engine no longer frees the memory on its own). +// +// This will always be referred to using a std::shared_ptr, since it keeps +// a reference count and is guaranteed to be thread-safe. +typedef std::shared_ptr<SharedArrayBufferMetadata> + SharedArrayBufferMetadataReference; + +class SharedArrayBufferMetadata + : public std::enable_shared_from_this<SharedArrayBufferMetadata> { + public: + static SharedArrayBufferMetadataReference ForSharedArrayBuffer( + Environment* env, + v8::Local<v8::Context> context, + v8::Local<v8::SharedArrayBuffer> source); + ~SharedArrayBufferMetadata(); + + // Create a SharedArrayBuffer object for a specific Environment and Context. + // The created SharedArrayBuffer will be in externalized mode and has + // a hidden object attached to it, during whose lifetime the reference + // count is increased by 1. + v8::MaybeLocal<v8::SharedArrayBuffer> GetSharedArrayBuffer( + Environment* env, v8::Local<v8::Context> context); + + SharedArrayBufferMetadata(SharedArrayBufferMetadata&& other) = delete; + SharedArrayBufferMetadata& operator=( + SharedArrayBufferMetadata&& other) = delete; + SharedArrayBufferMetadata& operator=( + const SharedArrayBufferMetadata&) = delete; + SharedArrayBufferMetadata(const SharedArrayBufferMetadata&) = delete; + + private: + explicit SharedArrayBufferMetadata(void* data, size_t size); + + // Attach a lifetime tracker object with a reference count to `target`. + v8::Maybe<bool> AssignToSharedArrayBuffer( + Environment* env, + v8::Local<v8::Context> context, + v8::Local<v8::SharedArrayBuffer> target); + + void* data = nullptr; + size_t size = 0; +}; + +} // namespace worker +} // namespace node + +#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS + + +#endif // SRC_SHAREDARRAYBUFFER_METADATA_H_ |