michael@0: // |reftest| skip michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: //----------------------------------------------------------------------- michael@0: michael@0: var summary = "Lamport Bakery's algorithm for mutual exclusion"; michael@0: // Adapted from pseudocode in Wikipedia: michael@0: // http://en.wikipedia.org/wiki/Lamport%27s_bakery_algorithm michael@0: michael@0: printStatus(summary); michael@0: michael@0: var N = 15; // Number of threads. michael@0: var LOOP_COUNT = 10; // Number of times each thread should loop michael@0: michael@0: function range(n) { michael@0: for (let i = 0; i < n; i++) michael@0: yield i; michael@0: } michael@0: michael@0: function max(a) { michael@0: let x = Number.NEGATIVE_INFINITY; michael@0: for each (let i in a) michael@0: if (i > x) michael@0: x = i; michael@0: return x; michael@0: } michael@0: michael@0: // the mutex mechanism michael@0: var entering = [false for (i in range(N))]; michael@0: var ticket = [0 for (i in range(N))]; michael@0: michael@0: // the object being protected michael@0: var counter = 0; michael@0: michael@0: function lock(i) michael@0: { michael@0: entering[i] = true; michael@0: ticket[i] = 1 + max(ticket); michael@0: entering[i] = false; michael@0: michael@0: for (let j = 0; j < N; j++) { michael@0: // If thread j is in the middle of getting a ticket, wait for that to michael@0: // finish. michael@0: while (entering[j]) michael@0: ; michael@0: michael@0: // Wait until all threads with smaller ticket numbers or with the same michael@0: // ticket number, but with higher priority, finish their work. michael@0: while ((ticket[j] != 0) && ((ticket[j] < ticket[i]) || michael@0: ((ticket[j] == ticket[i]) && (i < j)))) michael@0: ; michael@0: } michael@0: } michael@0: michael@0: function unlock(i) { michael@0: ticket[i] = 0; michael@0: } michael@0: michael@0: function worker(i) { michael@0: for (let k = 0; k < LOOP_COUNT; k++) { michael@0: lock(i); michael@0: michael@0: // The critical section michael@0: let x = counter; michael@0: sleep(0.003); michael@0: counter = x + 1; michael@0: michael@0: unlock(i); michael@0: } michael@0: return 'ok'; michael@0: } michael@0: michael@0: function makeWorker(id) { michael@0: return function () { return worker(id); }; michael@0: } michael@0: michael@0: var expect; michael@0: var actual; michael@0: michael@0: if (typeof scatter == 'undefined' || typeof sleep == 'undefined') { michael@0: print('Test skipped. scatter or sleep not defined.'); michael@0: expect = actual = 'Test skipped.'; michael@0: } else { michael@0: scatter([makeWorker(i) for (i in range(N))]); michael@0: michael@0: expect = "counter: " + (N * LOOP_COUNT); michael@0: actual = "counter: " + counter; michael@0: } michael@0: michael@0: reportCompare(expect, actual, summary);