summaryrefslogtreecommitdiffstats
path: root/js/src/tests/js1_8/extensions/lamport.js
diff options
context:
space:
mode:
authorMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
committerMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
commit5f8de423f190bbb79a62f804151bc24824fa32d8 (patch)
tree10027f336435511475e392454359edea8e25895d /js/src/tests/js1_8/extensions/lamport.js
parent49ee0794b5d912db1f95dce6eb52d781dc210db5 (diff)
downloadUXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.gz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.lz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.xz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.zip
Add m-esr52 at 52.6.0
Diffstat (limited to 'js/src/tests/js1_8/extensions/lamport.js')
-rw-r--r--js/src/tests/js1_8/extensions/lamport.js93
1 files changed, 93 insertions, 0 deletions
diff --git a/js/src/tests/js1_8/extensions/lamport.js b/js/src/tests/js1_8/extensions/lamport.js
new file mode 100644
index 000000000..d9e9d6636
--- /dev/null
+++ b/js/src/tests/js1_8/extensions/lamport.js
@@ -0,0 +1,93 @@
+// |reftest| skip
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+//-----------------------------------------------------------------------
+
+var summary = "Lamport Bakery's algorithm for mutual exclusion";
+// Adapted from pseudocode in Wikipedia:
+// http://en.wikipedia.org/wiki/Lamport%27s_bakery_algorithm
+
+printStatus(summary);
+
+var N = 15; // Number of threads.
+var LOOP_COUNT = 10; // Number of times each thread should loop
+
+function range(n) {
+ for (let i = 0; i < n; i++)
+ yield i;
+}
+
+function max(a) {
+ let x = Number.NEGATIVE_INFINITY;
+ for each (let i in a)
+ if (i > x)
+ x = i;
+ return x;
+}
+
+// the mutex mechanism
+var entering = [false for (i in range(N))];
+var ticket = [0 for (i in range(N))];
+
+// the object being protected
+var counter = 0;
+
+function lock(i)
+{
+ entering[i] = true;
+ ticket[i] = 1 + max(ticket);
+ entering[i] = false;
+
+ for (let j = 0; j < N; j++) {
+ // If thread j is in the middle of getting a ticket, wait for that to
+ // finish.
+ while (entering[j])
+ ;
+
+ // Wait until all threads with smaller ticket numbers or with the same
+ // ticket number, but with higher priority, finish their work.
+ while ((ticket[j] != 0) && ((ticket[j] < ticket[i]) ||
+ ((ticket[j] == ticket[i]) && (i < j))))
+ ;
+ }
+}
+
+function unlock(i) {
+ ticket[i] = 0;
+}
+
+function worker(i) {
+ for (let k = 0; k < LOOP_COUNT; k++) {
+ lock(i);
+
+ // The critical section
+ let x = counter;
+ sleep(0.003);
+ counter = x + 1;
+
+ unlock(i);
+ }
+ return 'ok';
+}
+
+function makeWorker(id) {
+ return function () { return worker(id); };
+}
+
+var expect;
+var actual;
+
+if (typeof scatter == 'undefined' || typeof sleep == 'undefined') {
+ print('Test skipped. scatter or sleep not defined.');
+ expect = actual = 'Test skipped.';
+} else {
+ scatter([makeWorker(i) for (i in range(N))]);
+
+ expect = "counter: " + (N * LOOP_COUNT);
+ actual = "counter: " + counter;
+}
+
+reportCompare(expect, actual, summary);