Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // |reftest| skip |
michael@0 | 2 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | //----------------------------------------------------------------------- |
michael@0 | 8 | |
michael@0 | 9 | var summary = "Peterson's algorithm for mutual exclusion"; |
michael@0 | 10 | |
michael@0 | 11 | printStatus(summary); |
michael@0 | 12 | |
michael@0 | 13 | var N = 500; // number of iterations |
michael@0 | 14 | |
michael@0 | 15 | // the mutex mechanism |
michael@0 | 16 | var f = [false, false]; |
michael@0 | 17 | var turn = 0; |
michael@0 | 18 | |
michael@0 | 19 | // the resource being protected |
michael@0 | 20 | var counter = 0; |
michael@0 | 21 | |
michael@0 | 22 | function worker(me) { |
michael@0 | 23 | let him = 1 - me; |
michael@0 | 24 | |
michael@0 | 25 | for (let i = 0; i < N; i++) { |
michael@0 | 26 | // enter the mutex |
michael@0 | 27 | f[me] = true; |
michael@0 | 28 | turn = him; |
michael@0 | 29 | while (f[him] && turn == him) |
michael@0 | 30 | ; // busy wait |
michael@0 | 31 | |
michael@0 | 32 | // critical section |
michael@0 | 33 | let x = counter; |
michael@0 | 34 | sleep(0.003); |
michael@0 | 35 | counter = x+1; |
michael@0 | 36 | |
michael@0 | 37 | // leave the mutex |
michael@0 | 38 | f[me] = false; |
michael@0 | 39 | } |
michael@0 | 40 | |
michael@0 | 41 | return 'ok'; |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | var expect; |
michael@0 | 45 | var actual; |
michael@0 | 46 | |
michael@0 | 47 | if (typeof scatter == 'undefined' || typeof sleep == 'undefined') { |
michael@0 | 48 | print('Test skipped. scatter or sleep not defined.'); |
michael@0 | 49 | expect = actual = 'Test skipped.'; |
michael@0 | 50 | } else { |
michael@0 | 51 | var results = scatter ([function() { return worker(0); }, |
michael@0 | 52 | function() { return worker(1); }]); |
michael@0 | 53 | expect = "Thread status: [ok,ok], counter: " + (2 * N); |
michael@0 | 54 | actual = "Thread status: [" + results + "], counter: " + counter; |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | reportCompare(expect, actual, summary); |