|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | + <head> |
| 4 | + <title>Atomic wait wake - AssemblyScript</title> |
| 5 | + <link rel="icon" href="http://assemblyscript.org/favicon.ico" type="image/x-icon" /> |
| 6 | + <meta name="viewport" content="user-scalable=0" /> |
| 7 | + <style> |
| 8 | + html, |
| 9 | + body { |
| 10 | + height: 100%; |
| 11 | + margin: 0; |
| 12 | + overflow: hidden; |
| 13 | + color: #111; |
| 14 | + background: #fff; |
| 15 | + font-family: sans-serif; |
| 16 | + } |
| 17 | + |
| 18 | + body { |
| 19 | + border-top: 2px solid #070809; |
| 20 | + } |
| 21 | + |
| 22 | + h1 { |
| 23 | + padding: 18px 20px 20px; |
| 24 | + font-size: 12pt; |
| 25 | + margin: 0; |
| 26 | + } |
| 27 | + |
| 28 | + a { |
| 29 | + color: #111; |
| 30 | + text-decoration: none; |
| 31 | + } |
| 32 | + |
| 33 | + a:hover { |
| 34 | + color: #efbd03; |
| 35 | + text-decoration: underline; |
| 36 | + } |
| 37 | + |
| 38 | + canvas { |
| 39 | + position: absolute; |
| 40 | + top: 60px; |
| 41 | + left: 20px; |
| 42 | + width: calc(100% - 40px); |
| 43 | + height: calc(100% - 80px); |
| 44 | + background: #070809; |
| 45 | + } |
| 46 | + .info { |
| 47 | + padding: 2rem; |
| 48 | + } |
| 49 | + </style> |
| 50 | + </head> |
| 51 | + |
| 52 | + <body> |
| 53 | + <h1> |
| 54 | + <a href="https://github.com/WebAssembly/threads">Atomic wait wake</a> in |
| 55 | + <a href="http://assemblyscript.org">AssemblyScript</a> ( |
| 56 | + <a |
| 57 | + href="https://github.com/AssemblyScript/assemblyscript/blob/master/examples/shared-memory/assembly/index.ts" |
| 58 | + >source</a |
| 59 | + > |
| 60 | + ) |
| 61 | + <br> |
| 62 | + </h1> |
| 63 | + <div class="info">Open console to results</div> |
| 64 | + <script> |
| 65 | + "use strict"; |
| 66 | + |
| 67 | + const memory = new WebAssembly.Memory({ |
| 68 | + initial: 256, |
| 69 | + shared: true, |
| 70 | + maximum: 256, |
| 71 | + }); |
| 72 | + const memoryView = new DataView(memory.buffer); |
| 73 | + let _exports = {}; |
| 74 | + const workers = []; |
| 75 | + async function init() { |
| 76 | + const res = await fetch("build/optimized.wasm"); |
| 77 | + const buffer = await res.arrayBuffer(); |
| 78 | + const wasm = await WebAssembly.compile(buffer); |
| 79 | + let worker1 = new Worker("./js/worker1.js"); |
| 80 | + let worker2 = new Worker("./js/worker1.js"); |
| 81 | + worker1.onmessage = handleMessage; |
| 82 | + worker2.onmessage = handleMessage; |
| 83 | + workers.push(worker1) |
| 84 | + workers.push(worker2); |
| 85 | + worker1.postMessage({ command: "init", id:1, memory, wasm }); |
| 86 | + worker2.postMessage({ command: "init", id:2, memory, wasm }); |
| 87 | + } |
| 88 | + let readyCount = 0; |
| 89 | + function handleMessage(event) { |
| 90 | + switch(event.data.command){ |
| 91 | + case "inited":{ |
| 92 | + readyCount++; |
| 93 | + if(readyCount === 2) { |
| 94 | + workers[0].postMessage({ command: "wait", value: 1 }); |
| 95 | + setTimeout(() => { |
| 96 | + workers[1].postMessage({ command: "wake", value: 1 }); |
| 97 | + }, 1000) |
| 98 | + |
| 99 | + setTimeout(() => { |
| 100 | + workers[0].postMessage({ command: "wait_js", value: 1 }); |
| 101 | + }, 1500) |
| 102 | + setTimeout(() => { |
| 103 | + workers[1].postMessage({ command: "wake", value: 2 }); |
| 104 | + }, 3000) |
| 105 | + // setTimeout(() => { |
| 106 | + // workers[0].postMessage({ command: "wait_i64", value: 1 }); |
| 107 | + // }, 0) |
| 108 | + // setTimeout(() => { |
| 109 | + // workers[1].postMessage({ command: "wake_i64", value: 2 }); |
| 110 | + // }, 1000) |
| 111 | + setTimeout(() => { |
| 112 | + workers[0].postMessage({ command: "wait", value: 1 }); |
| 113 | + }, 3500) |
| 114 | + setTimeout(() => { |
| 115 | + workers[1].postMessage({ command: "wake_js", value: 2 }); |
| 116 | + }, 5000) |
| 117 | + } |
| 118 | + break; |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + init(); |
| 123 | + </script> |
| 124 | + </body> |
| 125 | +</html> |
0 commit comments