Node: N-API: Need SharedArrayBuffer api

Created on 5 Oct 2018  Â·  9Comments  Â·  Source: nodejs/node

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.

n-api

Most helpful comment

@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.

All 9 comments

@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.

@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.

@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.

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

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.

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

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

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

The commit message for https://github.com/nodejs/node/pull/23279 should say that it fixes this issue, and this issue should be open.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

seishun picture seishun  Â·  3Comments

danielstaleiny picture danielstaleiny  Â·  3Comments

addaleax picture addaleax  Â·  3Comments

fanjunzhi picture fanjunzhi  Â·  3Comments

mcollina picture mcollina  Â·  3Comments