Skip to content
New issue

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

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

Already on GitHub? # to your account

Isolation and sharing between contexts #3148

Closed
darfink opened this issue Jun 13, 2017 · 3 comments
Closed

Isolation and sharing between contexts #3148

darfink opened this issue Jun 13, 2017 · 3 comments
Labels

Comments

@darfink
Copy link

darfink commented Jun 13, 2017

There was a previous discussion regarding this (#2092), when marshalling fields between contexts had just become more permissive, but I still had some questions that remained.

Baseline: to what extent do contexts on the same thread and runtime share data?

  • Do values share the reference count?
  • Are all types of values shareable?
  • Can I reuse script results between contexts?
  • Are there any gotchas?

Perhaps it's easier the name the things that are not shared?

Since JsCreateSharedArrayBufferWithSharedContent was recently implemented, I can only assume there are some limitations.

The catalyst for this discussion stems from chakracore-rs#14 — how to manage JsRelease in destructors. JsRelease requires an active context, and this cannot be ensured in destructors with chakracore-rs current implementation. Therefore I was contemplating whether a default context could be used to support JsRelease (for cases when the user no longer have any active contexts).

@curtisman
Copy link
Contributor

Let's answer your question on JsRelease first.
Yes, JsRelease doesn't care which context you have, so you can set a default context in general
To be more precise though:

  • For JsContextRef, you can call JsRelease without any context set (itself being a context)
  • For JsValueRef, you can get the context of the object using JsGetContextOfObject to get the context
  • For JsPropertyIdRef, require some context to be set, so you can use a default context

@curtisman
Copy link
Contributor

To answer your general question about cross context:

Are all types of values shareable?

Any JsValueRef can be used on any context as long as they are created from the same JsRuntimeHandle

Do values share the reference count?

Yes, they share the same reference count for JsAddRef/JsRelease.

Can I reuse script results between contexts?

Yes

Are there any gotchas?

The only gotchas is that using JsValueRef value from another JsContextRef is slightly slower, because it needs to go thru some cross context thunk to track the transitions.

Note that I didn't use share in my answer above, just because the word share in JsCreateSharedArrayBufferWithSharedContent has a different meaning. That is an API to create SharedArrayBuffer, analogous to ArrayBuffer, new to the ECMAScript spec https://tc39.github.io/ecma262/#sec-sharedarraybuffer-objects.

The purpose of this language feature is to enable concurrent access to a shared buffers between threads. You can use JsGetSharedArrayBufferContent to get the buffer content of a SharedArrayBuffer, and use JsCreateSharedArrayBufferWithSharedContext on a JsContextRef from another JsRuntimeHandle.

@darfink
Copy link
Author

darfink commented Jun 20, 2017

Thanks a ton! I believe this answers all of my questions.

@darfink darfink closed this as completed Jun 20, 2017
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants