Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

N-API: Need SharedArrayBuffer api #23276

Open
orange4glace opened this issue Oct 5, 2018 · 11 comments
Open

N-API: Need SharedArrayBuffer api #23276

orange4glace opened this issue Oct 5, 2018 · 11 comments
Labels
feature request Issues that request new features to be added to Node.js. node-api Issues and PRs related to the Node-API.

Comments

@orange4glace
Copy link

needed feature :
napi_create_external_sharedarraybuffer
napi_get_sharedarraybuffer_info
napi_is_sharedarraybuffer

Since V8 api supports external sharedarraybuffer creation, it would be nice if n-api implements this.

@orange4glace orange4glace changed the title N-API: Need SharedArrayBuffer feature N-API: Need SharedArrayBuffer api Oct 5, 2018
@addaleax
Copy link
Member

addaleax commented Oct 5, 2018

@orange4glace Is there a reason you closed this issue?

In any case, one issue with externalized SharedArrayBuffers is that you won’t actually be able to share them between Workers – that doesn’t make them all too useful.

@orange4glace
Copy link
Author

orange4glace commented Oct 5, 2018

@addleax Is there any reason that it can't be shared with workers? I am planning to use it with Electron so the native module will be loaded in Browser process.

PS. I closed it so I can make a PR instead.

@addaleax
Copy link
Member

addaleax commented Oct 5, 2018

@orange4glace I don’t know how Electron/Chromium deals with that, I was just referring to the worker_threads module in Node.js 10+.

The reason it doesn’t work with our Workers is that SharedArrayBuffers can be garbage collected multiple times on different threads, and only when the last reference to it from any thread is gone, we can free it. For that, we need to know how to release the memory – that’s easiest if we don’t allow sharing externalized SharedArrayBuffers. You can still share internalized SharedArrayBuffers without any problems, whether they are created by addons or not.

@orange4glace
Copy link
Author

Well, actually my module will fully manage the lifetime of the underlying buffer so I think I can manage that :?

@devsnek
Copy link
Member

devsnek commented Oct 5, 2018

it's also not a feature that all js engines might support as it's not a spec operation, so putting it in napi doesn't make much sense.

@addaleax
Copy link
Member

addaleax commented Oct 5, 2018

@orange4glace How does your module know when all references to it from all threads are gone?

@orange4glace
Copy link
Author

@addaleax My plan is not accessing it after free it like manner of c pointer. Does it also matter?

@orange4glace
Copy link
Author

@devsnek It's true but it is also true that SharedArrayBuffer is a part of ECMA 2017 standard.

@gabrielschulhof
Copy link
Contributor

The commit message for #23279 should say that it fixes this issue, and this issue should be open.

@huningxin
Copy link

This is useful for an addon that needs to access Wasm shared memory.

For instance, the Web Neural Network (WebNN) node.js addon exchange tensor data with ML frameworks (e.g. TensorFlow.js Wasm backend and ONNXRuntime Wasm) through Wasm memory. This issue prevents WebNN node.js addon to work with multi-threaded build of these frameworks which use SharedArrayBuffer in Electron.js (webmachinelearning/webnn-native#106).

@dygabo
Copy link
Member

dygabo commented Mar 22, 2023

This would be also useful for shared buffers that get updated with Atomic.exchange as Napi::ArrayBuffer seems to not be enough. At least wit node v14.18.0 Atomics.exchange returns: [object BigUint64Array] is not an integer shared typed array.
Atomics.exchange on a value in an ArrayBuffer seems to works fine with node v18.

Are there any plans of supporting SharedArrayBuffer in N-API?

@mhdawson mhdawson added the feature request Issues that request new features to be added to Node.js. label Apr 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Issues that request new features to be added to Node.js. node-api Issues and PRs related to the Node-API.
Projects
Status: Awaiting Triage
Development

No branches or pull requests

8 participants