OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Flags: --allow-natives-syntax --harmony-sharedarraybuffer | 5 // Flags: --allow-natives-syntax --harmony-sharedarraybuffer |
6 | 6 |
7 (function TestFailsWithNonSharedArray() { | 7 (function TestFailsWithNonSharedArray() { |
8 var ab = new ArrayBuffer(16); | 8 var ab = new ArrayBuffer(16); |
9 | 9 |
10 var i8a = new Int8Array(ab); | 10 var i8a = new Int8Array(ab); |
(...skipping 28 matching lines...) Expand all Loading... |
39 | 39 |
40 [i8a, i16a, ui8a, ui8ca, ui16a, ui32a, f32a, f64a].forEach(function( | 40 [i8a, i16a, ui8a, ui8ca, ui16a, ui32a, f32a, f64a].forEach(function( |
41 ta) { | 41 ta) { |
42 assertThrows(function() { Atomics.futexWait(ta, 0, 0); }); | 42 assertThrows(function() { Atomics.futexWait(ta, 0, 0); }); |
43 assertThrows(function() { Atomics.futexWake(ta, 0, 1); }); | 43 assertThrows(function() { Atomics.futexWake(ta, 0, 1); }); |
44 assertThrows(function() { Atomics.futexWakeOrRequeue(ta, 0, 1, 0, 0); }); | 44 assertThrows(function() { Atomics.futexWakeOrRequeue(ta, 0, 1, 0, 0); }); |
45 }); | 45 }); |
46 })(); | 46 })(); |
47 | 47 |
48 (function TestInvalidIndex() { | 48 (function TestInvalidIndex() { |
49 var i32a = new Int32Array(new SharedArrayBuffer(16)); | 49 var sab = new SharedArrayBuffer(16); |
| 50 var i32a = new Int32Array(sab); |
50 | 51 |
51 // Valid indexes are 0-3. | 52 // Valid indexes are 0-3. |
52 [-1, 4, 100].forEach(function(invalidIndex) { | 53 [-1, 4, 100].forEach(function(invalidIndex) { |
53 assertEquals(undefined, Atomics.futexWait(i32a, invalidIndex, 0)); | 54 assertEquals(undefined, Atomics.futexWait(i32a, invalidIndex, 0)); |
54 assertEquals(undefined, Atomics.futexWake(i32a, invalidIndex, 0)); | 55 assertEquals(undefined, Atomics.futexWake(i32a, invalidIndex, 0)); |
55 var validIndex = 0; | 56 var validIndex = 0; |
56 assertEquals(undefined, Atomics.futexWakeOrRequeue(i32a, invalidIndex, 0, 0, | 57 assertEquals(undefined, Atomics.futexWakeOrRequeue(i32a, invalidIndex, 0, 0, |
57 validIndex)); | 58 validIndex)); |
58 assertEquals(undefined, Atomics.futexWakeOrRequeue(i32a, validIndex, 0, 0, | 59 assertEquals(undefined, Atomics.futexWakeOrRequeue(i32a, validIndex, 0, 0, |
59 invalidIndex)); | 60 invalidIndex)); |
60 }); | 61 }); |
61 | 62 |
| 63 i32a = new Int32Array(sab, 8); |
| 64 [-1, 2, 100].forEach(function(invalidIndex) { |
| 65 assertEquals(undefined, Atomics.futexWait(i32a, invalidIndex, 0)); |
| 66 assertEquals(undefined, Atomics.futexWake(i32a, invalidIndex, 0)); |
| 67 var validIndex = 0; |
| 68 assertEquals(undefined, Atomics.futexWakeOrRequeue(i32a, invalidIndex, 0, 0, |
| 69 validIndex)); |
| 70 assertEquals(undefined, Atomics.futexWakeOrRequeue(i32a, validIndex, 0, 0, |
| 71 invalidIndex)); |
| 72 }); |
62 })(); | 73 })(); |
63 | 74 |
64 (function TestWaitTimeout() { | 75 (function TestWaitTimeout() { |
65 var i32a = new Int32Array(new SharedArrayBuffer(16)); | 76 var i32a = new Int32Array(new SharedArrayBuffer(16)); |
66 var waitMs = 100; | 77 var waitMs = 100; |
67 var startTime = new Date(); | 78 var startTime = new Date(); |
68 assertEquals(Atomics.TIMEDOUT, Atomics.futexWait(i32a, 0, 0, waitMs)); | 79 assertEquals(Atomics.TIMEDOUT, Atomics.futexWait(i32a, 0, 0, waitMs)); |
69 var endTime = new Date(); | 80 var endTime = new Date(); |
70 assertTrue(endTime - startTime >= waitMs); | 81 assertTrue(endTime - startTime >= waitMs); |
71 })(); | 82 })(); |
72 | 83 |
73 (function TestWaitNotEqual() { | 84 (function TestWaitNotEqual() { |
74 var i32a = new Int32Array(new SharedArrayBuffer(16)); | 85 var sab = new SharedArrayBuffer(16); |
| 86 var i32a = new Int32Array(sab); |
75 assertEquals(Atomics.NOTEQUAL, Atomics.futexWait(i32a, 0, 42)); | 87 assertEquals(Atomics.NOTEQUAL, Atomics.futexWait(i32a, 0, 42)); |
| 88 |
| 89 i32a = new Int32Array(sab, 8); |
| 90 i32a[0] = 1; |
| 91 assertEquals(Atomics.NOTEQUAL, Atomics.futexWait(i32a, 0, 0)); |
76 })(); | 92 })(); |
77 | 93 |
78 (function TestWaitNegativeTimeout() { | 94 (function TestWaitNegativeTimeout() { |
79 var i32a = new Int32Array(new SharedArrayBuffer(16)); | 95 var i32a = new Int32Array(new SharedArrayBuffer(16)); |
80 assertEquals(Atomics.TIMEDOUT, Atomics.futexWait(i32a, 0, 0, -1)); | 96 assertEquals(Atomics.TIMEDOUT, Atomics.futexWait(i32a, 0, 0, -1)); |
81 assertEquals(Atomics.TIMEDOUT, Atomics.futexWait(i32a, 0, 0, -Infinity)); | 97 assertEquals(Atomics.TIMEDOUT, Atomics.futexWait(i32a, 0, 0, -Infinity)); |
82 })(); | 98 })(); |
83 | 99 |
84 //// WORKER ONLY TESTS | 100 //// WORKER ONLY TESTS |
85 | 101 |
86 if (this.Worker) { | 102 if (this.Worker) { |
87 | 103 |
88 var TestWaitWithTimeout = function(timeout) { | 104 var TestWaitWithTimeout = function(timeout) { |
89 var sab = new SharedArrayBuffer(16); | 105 var sab = new SharedArrayBuffer(16); |
90 var i32a = new Int32Array(sab); | 106 var i32a = new Int32Array(sab); |
91 | 107 |
92 var workerScript = | 108 var workerScript = |
93 `onmessage = function(sab) { | 109 `onmessage = function(msg) { |
94 var i32a = new Int32Array(sab); | 110 var i32a = new Int32Array(msg.sab, msg.offset); |
95 var result = Atomics.futexWait(i32a, 0, 0, ${timeout}); | 111 var result = Atomics.futexWait(i32a, 0, 0, ${timeout}); |
96 postMessage(result); | 112 postMessage(result); |
97 };`; | 113 };`; |
98 | 114 |
99 var worker = new Worker(workerScript); | 115 var worker = new Worker(workerScript); |
100 worker.postMessage(sab, [sab]); | 116 worker.postMessage({sab: sab, offset: offset}, [sab]); |
101 | 117 |
102 // Spin until the worker is waiting on the futex. | 118 // Spin until the worker is waiting on the futex. |
103 while (%AtomicsFutexNumWaitersForTesting(i32a, 0) != 1) {} | 119 while (%AtomicsFutexNumWaitersForTesting(i32a, 0) != 1) {} |
104 | 120 |
105 Atomics.futexWake(i32a, 0, 1); | 121 Atomics.futexWake(i32a, 0, 1); |
106 assertEquals(Atomics.OK, worker.getMessage()); | 122 assertEquals(Atomics.OK, worker.getMessage()); |
107 worker.terminate(); | 123 worker.terminate(); |
| 124 |
| 125 var worker2 = new Worker(workerScript); |
| 126 var offset = 8; |
| 127 var i32a2 = new Int32Array(sab, offset); |
| 128 worker2.postMessage({sab: sab, offset: offset}, [sab]); |
| 129 |
| 130 // Spin until the worker is waiting on the futex. |
| 131 while (%AtomicsFutexNumWaitersForTesting(i32a2, 0) != 1) {} |
| 132 Atomics.futexWake(i32a2, 0, 1); |
| 133 assertEquals(Atomics.OK, worker2.getMessage()); |
| 134 worker2.terminate(); |
| 135 |
| 136 // Futex should work when index and buffer views are different, but |
| 137 // the real address is the same. |
| 138 var worker3 = new Worker(workerScript); |
| 139 i32a2 = new Int32Array(sab, 4); |
| 140 worker3.postMessage({sab: sab, offset: 8}, [sab]); |
| 141 |
| 142 // Spin until the worker is waiting on the futex. |
| 143 while (%AtomicsFutexNumWaitersForTesting(i32a2, 1) != 1) {} |
| 144 Atomics.futexWake(i32a2, 1, 1); |
| 145 assertEquals(Atomics.OK, worker3.getMessage()); |
| 146 worker3.terminate(); |
108 }; | 147 }; |
109 | 148 |
110 // Test various infinite timeouts | 149 // Test various infinite timeouts |
111 TestWaitWithTimeout(undefined); | 150 TestWaitWithTimeout(undefined); |
112 TestWaitWithTimeout(NaN); | 151 TestWaitWithTimeout(NaN); |
113 TestWaitWithTimeout(Infinity); | 152 TestWaitWithTimeout(Infinity); |
114 | 153 |
115 | 154 |
116 (function TestWakeMulti() { | 155 (function TestWakeMulti() { |
117 var sab = new SharedArrayBuffer(20); | 156 var sab = new SharedArrayBuffer(20); |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
259 wokenCount++; | 298 wokenCount++; |
260 } | 299 } |
261 } | 300 } |
262 } | 301 } |
263 | 302 |
264 assertEquals(0, %AtomicsFutexNumWaitersForTesting(i32a, index1)); | 303 assertEquals(0, %AtomicsFutexNumWaitersForTesting(i32a, index1)); |
265 assertEquals(0, %AtomicsFutexNumWaitersForTesting(i32a, index2)); | 304 assertEquals(0, %AtomicsFutexNumWaitersForTesting(i32a, index2)); |
266 | 305 |
267 for (id = 0; id < 4; ++id) { | 306 for (id = 0; id < 4; ++id) { |
268 assertEquals(Atomics.OK, workers[id].getMessage()); | 307 assertEquals(Atomics.OK, workers[id].getMessage()); |
| 308 } |
| 309 |
| 310 // Test futexWakeOrRequeue on offset typed array |
| 311 var offset = 16; |
| 312 sab = new SharedArrayBuffer(24); |
| 313 i32a = new Int32Array(sab); |
| 314 var i32a2 = new Int32Array(sab, offset); |
| 315 |
| 316 for (id = 0; id < 4; id++) { |
| 317 workers[id].postMessage({sab: sab, id: id}, [sab]); |
| 318 } |
| 319 |
| 320 while (%AtomicsFutexNumWaitersForTesting(i32a2, 0) != 4) { } |
| 321 |
| 322 index1 = 0; |
| 323 index2 = 1; |
| 324 assertEquals(4, %AtomicsFutexNumWaitersForTesting(i32a2, index1)); |
| 325 assertEquals(0, %AtomicsFutexNumWaitersForTesting(i32a2, index2)); |
| 326 |
| 327 assertEquals(2, Atomics.futexWakeOrRequeue(i32a2, index1, 2, 0, index2)); |
| 328 assertEquals(2, %AtomicsFutexNumWaitersForTesting(i32a2, index2)); |
| 329 assertEquals(0, %AtomicsFutexNumWaitersForTesting(i32a2, index1)); |
| 330 |
| 331 assertEquals(2, Atomics.futexWake(i32a2, index2, 2)); |
| 332 |
| 333 for (id = 0; id < 4; ++id) { |
| 334 assertEquals(Atomics.OK, workers[id].getMessage()); |
269 workers[id].terminate(); | 335 workers[id].terminate(); |
270 } | 336 } |
271 | 337 |
272 })(); | 338 })(); |
273 | 339 |
274 } | 340 } |
OLD | NEW |