Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/wasm2js.h
Original file line number Diff line number Diff line change
Expand Up @@ -2856,9 +2856,14 @@ void Wasm2JSGlue::emitSpecialSupport() {
} else if (import->base == ABI::wasm2js::ATOMIC_WAIT_I32) {
out << R"(
function wasm2js_atomic_wait_i32(ptr, expected, timeoutLow, timeoutHigh) {
if (timeoutLow != -1 || timeoutHigh != -1) throw 'unsupported timeout';
var timeout = Infinity;
if (timeoutHigh >= 0) {
// Convert from nanoseconds to milliseconds
// Taken from convertI32PairToI53 in emscripten's library_int53.js
timeout = ((timeoutLow / 1e6) >>> 0) + timeoutHigh * (4294967296 / 1e6);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think maybe (timeoutLow / 1e6) >>> 0 should be (timeoutLow >>> 0) / 1e6. That would allow timeouts of less than 1ms as the current code rounds the output.

Copy link
Contributor

@MaxGraey MaxGraey Dec 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought of that too at first, but it looks like the timeout cannot be sub-millisecond according to the specification. timeout is rational () value:

  1. Let q be ? ToNumber(timeout).
  2. If q is NaN or +∞𝔽, let t be +∞; else if q is -∞𝔽, let t be 0; else let t be max(ℝ(q), 0).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, maybe this differs between countries, but isn't ℝ the set of real numbers, not rational? See wikipedia, The rational numbers (Q) are included in the real numbers (R) (link)

JS does a ToNumber, so it converts to a double, and sub-millisecond values are allowed IIUC

Copy link
Contributor

@MaxGraey MaxGraey Dec 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, right! I got confused with natural (ℕ).

}
var view = new Int32Array(bufferView.buffer); // TODO cache
var result = Atomics.wait(view, ptr, expected);
var result = Atomics.wait(view, ptr >> 2, expected, timeout);
if (result == 'ok') return 0;
if (result == 'not-equal') return 1;
if (result == 'timed-out') return 2;
Expand Down
9 changes: 7 additions & 2 deletions test/wasm2js/atomics_32.2asm.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,14 @@ memorySegments[1] = base64DecodeToExistingUint8Array(new Uint8Array(6), 0, "d29y
var f64ScratchView = new Float64Array(scratchBuffer);

function wasm2js_atomic_wait_i32(ptr, expected, timeoutLow, timeoutHigh) {
if (timeoutLow != -1 || timeoutHigh != -1) throw 'unsupported timeout';
var timeout = Infinity;
if (timeoutHigh >= 0) {
// Convert from nanoseconds to milliseconds
// Taken from convertI32PairToI53 in emscripten's library_int53.js
timeout = ((timeoutLow / 1e6) >>> 0) + timeoutHigh * (4294967296 / 1e6);
}
var view = new Int32Array(bufferView.buffer); // TODO cache
var result = Atomics.wait(view, ptr, expected);
var result = Atomics.wait(view, ptr >> 2, expected, timeout);
if (result == 'ok') return 0;
if (result == 'not-equal') return 1;
if (result == 'timed-out') return 2;
Expand Down
9 changes: 7 additions & 2 deletions test/wasm2js/atomics_32.2asm.js.opt
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,14 @@ memorySegments[1] = base64DecodeToExistingUint8Array(new Uint8Array(6), 0, "d29y
var f64ScratchView = new Float64Array(scratchBuffer);

function wasm2js_atomic_wait_i32(ptr, expected, timeoutLow, timeoutHigh) {
if (timeoutLow != -1 || timeoutHigh != -1) throw 'unsupported timeout';
var timeout = Infinity;
if (timeoutHigh >= 0) {
// Convert from nanoseconds to milliseconds
// Taken from convertI32PairToI53 in emscripten's library_int53.js
timeout = ((timeoutLow / 1e6) >>> 0) + timeoutHigh * (4294967296 / 1e6);
}
var view = new Int32Array(bufferView.buffer); // TODO cache
var result = Atomics.wait(view, ptr, expected);
var result = Atomics.wait(view, ptr >> 2, expected, timeout);
if (result == 'ok') return 0;
if (result == 'not-equal') return 1;
if (result == 'timed-out') return 2;
Expand Down