rbuckton / proposal-refs

Ref declarations and expressions for ECMAScript
BSD 3-Clause "New" or "Revised" License
22 stars 0 forks source link

Exclusive Refs? #6

Open bmeck opened 4 years ago

bmeck commented 4 years ago

With this proposal would it be possible to create an exclusive reference where you can guarantee a binding/Reference has exclusive access to a variable. I'm more just poking at the ideas of smart pointers and trying to see how they would work with this proposal.

rbuckton commented 4 years ago

Do you have an example of this behavior in another language you could point to so I could better understand the requirements?

bmeck commented 4 years ago

@rbuckton I was specifically thinking about http://www.cplusplus.com/reference/memory/shared_ptr/ . Being able to ensure async control flow in JS does not interleave in odd ways would be nice:

if (foo === 1) {
  await bar();
  // foo is not guaranteed to be 1 here currently
}
rbuckton commented 4 years ago

C#/.NET has the concepts of ThreadLocal and AsyncLocal values, which isolate a value to a thread of execution, or an async call context, respectively. While something like ThreadLocal isn't currently necessary for JS (since JS is single threaded), I was hoping that the Zones proposal would provide something akin to an async call context such that AsyncLocal (or something similar) could eventually be implemented.

I haven't considered other aspects of something like shared_ptr.

For other scenarios, I've often used various async coordination primitives to synchronize access to shared resources across different async operations.

rbuckton commented 4 years ago

As I said above, I think AsyncLocal would be needed to address this case: https://github.com/legendecas/proposal-async-context#asynclocal. Aside from that or an external coordination primitive, I don't see a way to support an exclusive reference as you've described, though I am open to suggestions.