Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(19)

Unified Diff: test/mjsunit/d8-worker-sharedarraybuffer.js

Issue 1215233004: Fix bug when transferring SharedArrayBuffer to multiple Workers. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: internalize transferred ArrayBuffer Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« src/d8.cc ('K') | « src/d8.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/mjsunit/d8-worker-sharedarraybuffer.js
diff --git a/test/mjsunit/d8-worker-sharedarraybuffer.js b/test/mjsunit/d8-worker-sharedarraybuffer.js
index 791529fc89402cfe8b8afcfc3965d7c3bbcd8711..d432f970748486837bd745092bda7e59c7d2bb8e 100644
--- a/test/mjsunit/d8-worker-sharedarraybuffer.js
+++ b/test/mjsunit/d8-worker-sharedarraybuffer.js
@@ -27,42 +27,80 @@
// Flags: --harmony-sharedarraybuffer --harmony-atomics
-var workerScript =
- `onmessage = function(m) {
- var sab = m;
- var ta = new Uint32Array(sab);
- if (sab.byteLength !== 16) {
- throw new Error('SharedArrayBuffer transfer byteLength');
- }
- for (var i = 0; i < 4; ++i) {
- if (ta[i] !== i) {
- throw new Error('SharedArrayBuffer transfer value ' + i);
- }
- }
- // Atomically update ta[0]
- Atomics.store(ta, 0, 100);
- };`;
-
if (this.Worker) {
- var w = new Worker(workerScript);
- var sab = new SharedArrayBuffer(16);
- var ta = new Uint32Array(sab);
- for (var i = 0; i < 4; ++i) {
- ta[i] = i;
- }
+ (function TestTransfer() {
+ var workerScript =
+ `onmessage = function(m) {
+ var sab = m;
+ var ta = new Uint32Array(sab);
+ if (sab.byteLength !== 16) {
+ throw new Error('SharedArrayBuffer transfer byteLength');
+ }
+ for (var i = 0; i < 4; ++i) {
+ if (ta[i] !== i) {
+ throw new Error('SharedArrayBuffer transfer value ' + i);
+ }
+ }
+ // Atomically update ta[0]
+ Atomics.store(ta, 0, 100);
+ };`;
+
+ var w = new Worker(workerScript);
+
+ var sab = new SharedArrayBuffer(16);
+ var ta = new Uint32Array(sab);
+ for (var i = 0; i < 4; ++i) {
+ ta[i] = i;
+ }
+
+ // Transfer SharedArrayBuffer
+ w.postMessage(sab, [sab]);
+ assertEquals(16, sab.byteLength); // ArrayBuffer should not be neutered.
+
+ // Spinwait for the worker to update ta[0]
+ var ta0;
+ while ((ta0 = Atomics.load(ta, 0)) == 0) {}
+
+ assertEquals(100, ta0);
+
+ w.terminate();
+
+ assertEquals(16, sab.byteLength); // Still not neutered.
+ })();
- // Transfer SharedArrayBuffer
- w.postMessage(sab, [sab]);
- assertEquals(16, sab.byteLength); // ArrayBuffer should not be neutered.
+ (function TestTransferMulti() {
+ var workerScript =
+ `onmessage = function(msg) {
+ var sab = msg.sab;
+ var id = msg.id;
+ var ta = new Uint32Array(sab);
+ Atomics.store(ta, id, 1);
+ postMessage(id);
+ };`;
- // Spinwait for the worker to update ta[0]
- var ta0;
- while ((ta0 = Atomics.load(ta, 0)) == 0) {}
+ var sab = new SharedArrayBuffer(16);
+ var ta = new Uint32Array(sab);
- assertEquals(100, ta0);
+ var id;
+ var workers = [];
+ for (id = 0; id < 4; ++id) {
+ workers[id] = new Worker(workerScript);
+ workers[id].postMessage({sab: sab, id: id}, [sab]);
+ }
- w.terminate();
+ // Spinwait for each worker to update ta[id]
+ var count = 0;
+ while (count < 4) {
+ for (id = 0; id < 4; ++id) {
+ if (Atomics.compareExchange(ta, id, 1, -1) == 1) {
+ // Worker is finished.
+ assertEquals(id, workers[id].getMessage());
+ workers[id].terminate();
+ count++;
+ }
+ }
+ }
+ })();
- assertEquals(16, sab.byteLength); // Still not neutered.
}
« src/d8.cc ('K') | « src/d8.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698