2015-07-17 17:11:32 +00:00
|
|
|
// Copyright 2015 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2015-09-01 18:33:51 +00:00
|
|
|
// Flags: --allow-natives-syntax --harmony-sharedarraybuffer
|
2015-07-17 17:11:32 +00:00
|
|
|
|
|
|
|
(function TestFailsWithNonSharedArray() {
|
|
|
|
var ab = new ArrayBuffer(16);
|
|
|
|
|
|
|
|
var i8a = new Int8Array(ab);
|
|
|
|
var i16a = new Int16Array(ab);
|
|
|
|
var i32a = new Int32Array(ab);
|
|
|
|
var ui8a = new Uint8Array(ab);
|
|
|
|
var ui8ca = new Uint8ClampedArray(ab);
|
|
|
|
var ui16a = new Uint16Array(ab);
|
|
|
|
var ui32a = new Uint32Array(ab);
|
|
|
|
var f32a = new Float32Array(ab);
|
|
|
|
var f64a = new Float64Array(ab);
|
|
|
|
|
|
|
|
[i8a, i16a, i32a, ui8a, ui8ca, ui16a, ui32a, f32a, f64a].forEach(function(
|
|
|
|
ta) {
|
2016-07-13 18:29:48 +00:00
|
|
|
assertThrows(function() { Atomics.wait(ta, 0, 0); });
|
|
|
|
assertThrows(function() { Atomics.wake(ta, 0, 1); });
|
2015-07-17 17:11:32 +00:00
|
|
|
});
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestFailsWithNonSharedInt32Array() {
|
|
|
|
var sab = new SharedArrayBuffer(16);
|
|
|
|
|
|
|
|
var i8a = new Int8Array(sab);
|
|
|
|
var i16a = new Int16Array(sab);
|
|
|
|
var ui8a = new Uint8Array(sab);
|
|
|
|
var ui8ca = new Uint8ClampedArray(sab);
|
|
|
|
var ui16a = new Uint16Array(sab);
|
|
|
|
var ui32a = new Uint32Array(sab);
|
|
|
|
var f32a = new Float32Array(sab);
|
|
|
|
var f64a = new Float64Array(sab);
|
|
|
|
|
|
|
|
[i8a, i16a, ui8a, ui8ca, ui16a, ui32a, f32a, f64a].forEach(function(
|
|
|
|
ta) {
|
2016-07-13 18:29:48 +00:00
|
|
|
assertThrows(function() { Atomics.wait(ta, 0, 0); });
|
|
|
|
assertThrows(function() { Atomics.wake(ta, 0, 1); });
|
2015-07-17 17:11:32 +00:00
|
|
|
});
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestInvalidIndex() {
|
2015-11-20 11:39:25 +00:00
|
|
|
var sab = new SharedArrayBuffer(16);
|
|
|
|
var i32a = new Int32Array(sab);
|
2015-07-17 17:11:32 +00:00
|
|
|
|
|
|
|
// Valid indexes are 0-3.
|
2017-05-25 00:53:02 +00:00
|
|
|
[-1, 4, 100, 0xffffffff].forEach(function(invalidIndex) {
|
2016-03-25 21:52:15 +00:00
|
|
|
assertThrows(function() {
|
2016-07-13 18:29:48 +00:00
|
|
|
Atomics.wait(i32a, invalidIndex, 0);
|
2016-03-25 21:52:15 +00:00
|
|
|
}, RangeError);
|
|
|
|
assertThrows(function() {
|
2016-07-13 18:29:48 +00:00
|
|
|
Atomics.wake(i32a, invalidIndex, 0);
|
2016-03-25 21:52:15 +00:00
|
|
|
}, RangeError);
|
2015-07-17 17:11:32 +00:00
|
|
|
var validIndex = 0;
|
|
|
|
});
|
|
|
|
|
2015-11-20 11:39:25 +00:00
|
|
|
i32a = new Int32Array(sab, 8);
|
2017-05-25 00:53:02 +00:00
|
|
|
[-1, 2, 100, 0xffffffff].forEach(function(invalidIndex) {
|
2016-03-25 21:52:15 +00:00
|
|
|
assertThrows(function() {
|
2016-07-13 18:29:48 +00:00
|
|
|
Atomics.wait(i32a, invalidIndex, 0);
|
2016-03-25 21:52:15 +00:00
|
|
|
}, RangeError);
|
|
|
|
assertThrows(function() {
|
2016-07-13 18:29:48 +00:00
|
|
|
Atomics.wake(i32a, invalidIndex, 0);
|
2016-03-25 21:52:15 +00:00
|
|
|
}, RangeError);
|
2015-11-20 11:39:25 +00:00
|
|
|
var validIndex = 0;
|
|
|
|
});
|
2015-07-17 17:11:32 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestWaitTimeout() {
|
|
|
|
var i32a = new Int32Array(new SharedArrayBuffer(16));
|
|
|
|
var waitMs = 100;
|
|
|
|
var startTime = new Date();
|
2016-07-13 18:29:48 +00:00
|
|
|
assertEquals("timed-out", Atomics.wait(i32a, 0, 0, waitMs));
|
2015-07-17 17:11:32 +00:00
|
|
|
var endTime = new Date();
|
|
|
|
assertTrue(endTime - startTime >= waitMs);
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestWaitNotEqual() {
|
2015-11-20 11:39:25 +00:00
|
|
|
var sab = new SharedArrayBuffer(16);
|
|
|
|
var i32a = new Int32Array(sab);
|
2016-07-13 18:29:48 +00:00
|
|
|
assertEquals("not-equal", Atomics.wait(i32a, 0, 42));
|
2015-11-20 11:39:25 +00:00
|
|
|
|
|
|
|
i32a = new Int32Array(sab, 8);
|
|
|
|
i32a[0] = 1;
|
2016-07-13 18:29:48 +00:00
|
|
|
assertEquals("not-equal", Atomics.wait(i32a, 0, 0));
|
2015-07-17 17:11:32 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestWaitNegativeTimeout() {
|
|
|
|
var i32a = new Int32Array(new SharedArrayBuffer(16));
|
2016-07-13 18:29:48 +00:00
|
|
|
assertEquals("timed-out", Atomics.wait(i32a, 0, 0, -1));
|
|
|
|
assertEquals("timed-out", Atomics.wait(i32a, 0, 0, -Infinity));
|
2015-07-17 17:11:32 +00:00
|
|
|
})();
|
|
|
|
|
2017-01-23 22:06:12 +00:00
|
|
|
(function TestWaitNotAllowed() {
|
|
|
|
%SetAllowAtomicsWait(false);
|
|
|
|
var i32a = new Int32Array(new SharedArrayBuffer(16));
|
|
|
|
assertThrows(function() {
|
|
|
|
Atomics.wait(i32a, 0, 0, -1);
|
|
|
|
});
|
|
|
|
%SetAllowAtomicsWait(true);
|
|
|
|
})();
|
|
|
|
|
2017-02-01 21:47:22 +00:00
|
|
|
(function TestWakePositiveInfinity() {
|
|
|
|
var i32a = new Int32Array(new SharedArrayBuffer(16));
|
|
|
|
Atomics.wake(i32a, 0, Number.POSITIVE_INFINITY);
|
2018-07-18 18:19:55 +00:00
|
|
|
Atomics.notify(i32a, 0, Number.POSITIVE_INFINITY);
|
2017-02-01 21:47:22 +00:00
|
|
|
})();
|
|
|
|
|
2017-03-13 20:59:15 +00:00
|
|
|
// In a previous version, this test caused a check failure
|
|
|
|
(function TestObjectWaitValue() {
|
|
|
|
var sab = new SharedArrayBuffer(16);
|
|
|
|
var i32a = new Int32Array(sab);
|
|
|
|
assertEquals("timed-out", Atomics.wait(i32a, 0, Math, 0));
|
|
|
|
})();
|
|
|
|
|
|
|
|
|
2015-07-17 17:11:32 +00:00
|
|
|
//// WORKER ONLY TESTS
|
|
|
|
|
|
|
|
if (this.Worker) {
|
|
|
|
|
2018-07-18 18:19:55 +00:00
|
|
|
var TestWaitWithTimeout = function(notify, timeout) {
|
2015-07-17 17:11:32 +00:00
|
|
|
var sab = new SharedArrayBuffer(16);
|
|
|
|
var i32a = new Int32Array(sab);
|
|
|
|
|
|
|
|
var workerScript =
|
2015-11-20 11:39:25 +00:00
|
|
|
`onmessage = function(msg) {
|
|
|
|
var i32a = new Int32Array(msg.sab, msg.offset);
|
2016-07-13 18:29:48 +00:00
|
|
|
var result = Atomics.wait(i32a, 0, 0, ${timeout});
|
2015-07-17 17:11:32 +00:00
|
|
|
postMessage(result);
|
|
|
|
};`;
|
|
|
|
|
2018-09-19 23:30:04 +00:00
|
|
|
var worker = new Worker(workerScript, {type: 'string'});
|
2017-01-27 20:15:37 +00:00
|
|
|
worker.postMessage({sab: sab, offset: offset});
|
2015-07-17 17:11:32 +00:00
|
|
|
|
|
|
|
// Spin until the worker is waiting on the futex.
|
2016-07-13 18:29:48 +00:00
|
|
|
while (%AtomicsNumWaitersForTesting(i32a, 0) != 1) {}
|
2015-07-17 17:11:32 +00:00
|
|
|
|
2018-07-18 18:19:55 +00:00
|
|
|
notify(i32a, 0, 1);
|
2016-07-13 18:29:48 +00:00
|
|
|
assertEquals("ok", worker.getMessage());
|
2015-07-17 17:11:32 +00:00
|
|
|
worker.terminate();
|
2015-11-20 11:39:25 +00:00
|
|
|
|
2018-09-19 23:30:04 +00:00
|
|
|
var worker2 = new Worker(workerScript, {type: 'string'});
|
2015-11-20 11:39:25 +00:00
|
|
|
var offset = 8;
|
|
|
|
var i32a2 = new Int32Array(sab, offset);
|
2017-01-27 20:15:37 +00:00
|
|
|
worker2.postMessage({sab: sab, offset: offset});
|
2015-11-20 11:39:25 +00:00
|
|
|
|
|
|
|
// Spin until the worker is waiting on the futex.
|
2016-07-13 18:29:48 +00:00
|
|
|
while (%AtomicsNumWaitersForTesting(i32a2, 0) != 1) {}
|
2018-07-18 18:19:55 +00:00
|
|
|
notify(i32a2, 0, 1);
|
2016-07-13 18:29:48 +00:00
|
|
|
assertEquals("ok", worker2.getMessage());
|
2015-11-20 11:39:25 +00:00
|
|
|
worker2.terminate();
|
|
|
|
|
|
|
|
// Futex should work when index and buffer views are different, but
|
|
|
|
// the real address is the same.
|
2018-09-19 23:30:04 +00:00
|
|
|
var worker3 = new Worker(workerScript, {type: 'string'});
|
2015-11-20 11:39:25 +00:00
|
|
|
i32a2 = new Int32Array(sab, 4);
|
2017-01-27 20:15:37 +00:00
|
|
|
worker3.postMessage({sab: sab, offset: 8});
|
2015-11-20 11:39:25 +00:00
|
|
|
|
|
|
|
// Spin until the worker is waiting on the futex.
|
2016-07-13 18:29:48 +00:00
|
|
|
while (%AtomicsNumWaitersForTesting(i32a2, 1) != 1) {}
|
2018-07-18 18:19:55 +00:00
|
|
|
notify(i32a2, 1, 1);
|
2016-07-13 18:29:48 +00:00
|
|
|
assertEquals("ok", worker3.getMessage());
|
2015-11-20 11:39:25 +00:00
|
|
|
worker3.terminate();
|
2015-07-17 17:11:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Test various infinite timeouts
|
2018-07-18 18:19:55 +00:00
|
|
|
TestWaitWithTimeout(Atomics.wake, undefined);
|
|
|
|
TestWaitWithTimeout(Atomics.wake, NaN);
|
|
|
|
TestWaitWithTimeout(Atomics.wake, Infinity);
|
|
|
|
TestWaitWithTimeout(Atomics.notify, undefined);
|
|
|
|
TestWaitWithTimeout(Atomics.notify, NaN);
|
|
|
|
TestWaitWithTimeout(Atomics.notify, Infinity);
|
|
|
|
|
|
|
|
var TestWakeMulti = function(notify) {
|
2015-07-17 17:11:32 +00:00
|
|
|
var sab = new SharedArrayBuffer(20);
|
|
|
|
var i32a = new Int32Array(sab);
|
|
|
|
|
|
|
|
// SAB values:
|
|
|
|
// i32a[id], where id in range [0, 3]:
|
|
|
|
// 0 => Worker |id| is still waiting on the futex
|
|
|
|
// 1 => Worker |id| is not waiting on futex, but has not be reaped by the
|
|
|
|
// main thread.
|
|
|
|
// 2 => Worker |id| has been reaped.
|
|
|
|
//
|
|
|
|
// i32a[4]:
|
|
|
|
// always 0. Each worker is waiting on this index.
|
|
|
|
|
|
|
|
var workerScript =
|
|
|
|
`onmessage = function(msg) {
|
|
|
|
var id = msg.id;
|
|
|
|
var i32a = new Int32Array(msg.sab);
|
|
|
|
|
|
|
|
// Wait on i32a[4] (should be zero).
|
2016-07-13 18:29:48 +00:00
|
|
|
var result = Atomics.wait(i32a, 4, 0);
|
2015-07-17 17:11:32 +00:00
|
|
|
// Set i32a[id] to 1 to notify the main thread which workers were
|
|
|
|
// woken up.
|
|
|
|
Atomics.store(i32a, id, 1);
|
|
|
|
postMessage(result);
|
|
|
|
};`;
|
|
|
|
|
|
|
|
var id;
|
|
|
|
var workers = [];
|
|
|
|
for (id = 0; id < 4; id++) {
|
2018-09-19 23:30:04 +00:00
|
|
|
workers[id] = new Worker(workerScript, {type: 'string'});
|
2017-01-27 20:15:37 +00:00
|
|
|
workers[id].postMessage({sab: sab, id: id});
|
2015-07-17 17:11:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Spin until all workers are waiting on the futex.
|
2016-07-13 18:29:48 +00:00
|
|
|
while (%AtomicsNumWaitersForTesting(i32a, 4) != 4) {}
|
2015-07-17 17:11:32 +00:00
|
|
|
|
|
|
|
// Wake up three waiters.
|
2018-07-18 18:19:55 +00:00
|
|
|
assertEquals(3, notify(i32a, 4, 3));
|
2015-07-17 17:11:32 +00:00
|
|
|
|
|
|
|
var wokenCount = 0;
|
|
|
|
var waitingId = 0 + 1 + 2 + 3;
|
|
|
|
while (wokenCount < 3) {
|
|
|
|
for (id = 0; id < 4; id++) {
|
|
|
|
// Look for workers that have not yet been reaped. Set i32a[id] to 2
|
|
|
|
// when they've been processed so we don't look at them again.
|
|
|
|
if (Atomics.compareExchange(i32a, id, 1, 2) == 1) {
|
2016-07-13 18:29:48 +00:00
|
|
|
assertEquals("ok", workers[id].getMessage());
|
2015-07-17 17:11:32 +00:00
|
|
|
workers[id].terminate();
|
|
|
|
waitingId -= id;
|
|
|
|
wokenCount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assertEquals(3, wokenCount);
|
|
|
|
assertEquals(0, Atomics.load(i32a, waitingId));
|
2016-07-13 18:29:48 +00:00
|
|
|
assertEquals(1, %AtomicsNumWaitersForTesting(i32a, 4));
|
2015-07-17 17:11:32 +00:00
|
|
|
|
|
|
|
// Finally wake the last waiter.
|
2018-07-18 18:19:55 +00:00
|
|
|
assertEquals(1, notify(i32a, 4, 1));
|
2016-07-13 18:29:48 +00:00
|
|
|
assertEquals("ok", workers[waitingId].getMessage());
|
2015-07-17 17:11:32 +00:00
|
|
|
workers[waitingId].terminate();
|
|
|
|
|
2016-07-13 18:29:48 +00:00
|
|
|
assertEquals(0, %AtomicsNumWaitersForTesting(i32a, 4));
|
2015-07-17 17:11:32 +00:00
|
|
|
|
2018-07-18 18:19:55 +00:00
|
|
|
};
|
2015-07-17 17:11:32 +00:00
|
|
|
|
2018-07-18 18:19:55 +00:00
|
|
|
TestWakeMulti(Atomics.wake);
|
2018-07-19 02:07:38 +00:00
|
|
|
// TODO(binji): This is hitting d8's max worker count when run with multiple
|
|
|
|
// isolates. Re-enable when workers are cleaned up after termination.
|
|
|
|
// TestWakeMulti(Atomics.notify);
|
2015-07-17 17:11:32 +00:00
|
|
|
}
|