Skip to content
This repository has been archived by the owner on Jan 25, 2022. It is now read-only.

Atomics across different objects #16

Closed
jfbastien opened this issue Aug 27, 2015 · 2 comments
Closed

Atomics across different objects #16

jfbastien opened this issue Aug 27, 2015 · 2 comments

Comments

@jfbastien
Copy link
Contributor

The current proposal defines atomics within a single array buffer. It says nothing of ordering when atomic accesses are performed on distinct array buffers: one piece of code could be accessing two separate objects. Consider:

Setup:

var sab1 = new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * 1024);
var sab2 = new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * 1024);
var ia1 = new Int32Array(sab1);
var ia2 = new Int32Array(sab2);

Thread 1:

var count = 0;
while (true) {
  while (Atomics.load(ia1, 42) == 0) continue; // Wait on other.
  console.log("Thread 1: " + count);
  count++;
  Atomics.store(ia1, 42, 0); // Reset self.
  Atomics.store(ia2, 42, 1); // Unblock other.
}

Thread 2:

var count = 0;
while (true) {
  Atomics.store(ia2, 42, 0); // Reset self.
  Atomics.store(ia1, 42, 1); // Unblock other.
  while (Atomics.load(ia2, 42) == 0) continue; // Wait on other.
  console.log("Thread 2: " + count);
  count++;
}

Is this code guaranteed to always print out in locksteps? This sounds like a trivial "yes", but it's not obvious from the current spec that synchronization across distinct array buffers also works. I think this is especially important to spell out if we want a formal model of the JavaScript memory model: C++'s memory model is based on having a single heap.

I got onto this line of thinking because of WebAssembly/design#314, where we discuss having individual non-aliased objects being individually accessible atomically.

@lars-t-hansen
Copy link
Collaborator

Nice catch. The intent has always been that there is "one memory" even though there may be multiple shared memory regions within it. Will attempt to clarify that when I incorporate the improved wording for the memory model.

@lars-t-hansen
Copy link
Collaborator

I believe this is now fixed.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants