This repository has been archived by the owner on Jun 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathexample.html
57 lines (48 loc) · 1.5 KB
/
example.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title> Example use of Atomics.waitAsync </title>
</head>
<body>
<pre>
<div id=scrool>
</div>
</pre>
<script src="polyfill.js"></script>
<script>
var sab = new SharedArrayBuffer(4096);
var ia = new Int32Array(sab);
ia[37] = 0x1337;
test1();
function test1() {
Atomics.waitAsync(ia, 37, 0x1337, 1000).then(function (r) { log("Resolved: " + r); test2(); });
}
var code = `
var ia = null;
onmessage = function (ev) {
if (!ia) {
postMessage("Aux worker is running");
ia = new Int32Array(ev.data);
}
postMessage("Aux worker is sleeping for a little bit");
setTimeout(function () { postMessage("Aux worker is waking"); Atomics.notify(ia, 37); }, 1000);
}`;
function test2() {
var w = new Worker("data:application/javascript," + encodeURIComponent(code));
w.onmessage = function (ev) { log(ev.data) };
w.postMessage(sab);
Atomics.waitAsync(ia, 37, 0x1337).then(function (r) { log("Resolved: " + r); test3(w); });
}
function test3(w) {
w.postMessage(false);
Atomics.waitAsync(ia, 37, 0x1337).then(function (r) { log("Resolved 1: " + r); });
Atomics.waitAsync(ia, 37, 0x1337).then(function (r) { log("Resolved 2: " + r); });
Atomics.waitAsync(ia, 37, 0x1337).then(function (r) { log("Resolved 3: " + r); });
}
function log(msg) {
document.getElementById("scrool").innerHTML += String(msg) + "\n";
}
</script>
</body>
</html>