This repository has been archived by the owner on Jul 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
browser-main.js
67 lines (53 loc) · 1.96 KB
/
browser-main.js
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
58
59
60
61
62
63
64
65
66
67
// Example and test case (for browser) for simple locks and condition variables.
//
// We fork off four workers that run long-running loops whose bodies are
// critical sections that update a shared variable. We also run a loop in a
// fifth worker, but for a shorter time. The fifth worker waits on a condition
// variable for the other workers to finish, and then posts a message to the
// main thread that it is done - this message contains the word "proxy".
//
// We can't do the waiting on the main thread because the main thread is not
// allowed to perform blocking waits in a browser. The fifth thread therefore
// serves as a proxy for the main thread. This is a pretty common pattern.
//
// The attempt here is to stress-test locking, more than anything.
// LOAD THESE FILES FIRST:
//
// lock.js
// browser-common-defs.js
// Create shared memory
var sab = new SharedArrayBuffer(4096);
// Initialize shared memory - happens only here
Lock.initialize(sab, lockLoc);
Cond.initialize(sab, condLoc);
// Create our local views on shared memory.
var lock = new Lock(sab, lockLoc);
var cond = new Cond(lock, condLoc);
var i32 = new Int32Array(sab, iLoc, 1);
var msg = new Int32Array(sab, msgLoc, 1);
// Create the workers, and share memory with them
for ( let i=0; i < numworkers ; i++ ) {
let w = new Worker("browser-worker.js");
w.postMessage([sab, i]);
w.onmessage = function (ev) { message(ev.data); }
}
// Create the proxy, and share memory with it
{
let w = new Worker("browser-proxy.js");
w.postMessage([sab, "proxy"]);
w.onmessage = function (ev) { message(ev.data); }
}
// This is the result we expect
let sum = outer * 3;
for ( let i=0 ; i < numworkers ; i++ )
sum += inner * (5+i);
function message(m) {
m = String(m);
document.getElementById("scrool").innerHTML += m + "<br>";
if (m.match("proxy")) {
if (i32[0] == sum)
message("DONE: " + i32[0]);
else
message("FAILED: got " + i32[0] + ", expected " + sum);
}
}