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 | /* |
michael@0 | 4 | * Any copyright is dedicated to the Public Domain. |
michael@0 | 5 | * http://creativecommons.org/licenses/publicdomain/ |
michael@0 | 6 | * Contributor: Jason Orendorff |
michael@0 | 7 | */ |
michael@0 | 8 | |
michael@0 | 9 | //----------------------------------------------------------------------------- |
michael@0 | 10 | |
michael@0 | 11 | var summary = "Dekker's algorithm for mutual exclusion"; |
michael@0 | 12 | // Adapted from pseudocode in Wikipedia: |
michael@0 | 13 | // http://en.wikipedia.org/wiki/Dekker%27s_algorithm |
michael@0 | 14 | |
michael@0 | 15 | printStatus (summary); |
michael@0 | 16 | |
michael@0 | 17 | var N = 500; // number of iterations |
michael@0 | 18 | |
michael@0 | 19 | // the mutex mechanism |
michael@0 | 20 | var f = [false, false]; |
michael@0 | 21 | var turn = 0; |
michael@0 | 22 | |
michael@0 | 23 | // resource being protected |
michael@0 | 24 | var counter = 0; |
michael@0 | 25 | |
michael@0 | 26 | function worker(me) { |
michael@0 | 27 | let him = 1 - me; |
michael@0 | 28 | |
michael@0 | 29 | for (let i = 0; i < N; i++) { |
michael@0 | 30 | // enter the mutex |
michael@0 | 31 | f[me] = true; |
michael@0 | 32 | while (f[him]) { |
michael@0 | 33 | if (turn != me) { |
michael@0 | 34 | f[me] = false; |
michael@0 | 35 | while (turn != me) |
michael@0 | 36 | ; // busy wait |
michael@0 | 37 | f[me] = true; |
michael@0 | 38 | } |
michael@0 | 39 | } |
michael@0 | 40 | |
michael@0 | 41 | // critical section |
michael@0 | 42 | let x = counter; |
michael@0 | 43 | sleep(0.003); |
michael@0 | 44 | counter = x + 1; |
michael@0 | 45 | |
michael@0 | 46 | // leave the mutex |
michael@0 | 47 | turn = him; |
michael@0 | 48 | f[me] = false; |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | return 'ok'; |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | var expect; |
michael@0 | 55 | var actual; |
michael@0 | 56 | |
michael@0 | 57 | if (typeof scatter == 'undefined' || typeof sleep == 'undefined') { |
michael@0 | 58 | print('Test skipped. scatter or sleep not defined.'); |
michael@0 | 59 | expect = actual = 'Test skipped.'; |
michael@0 | 60 | } else { |
michael@0 | 61 | var results = scatter([function () { return worker(0); }, |
michael@0 | 62 | function () { return worker(1); }]); |
michael@0 | 63 | |
michael@0 | 64 | expect = "Thread status: [ok,ok], counter: " + (2 * N); |
michael@0 | 65 | actual = "Thread status: [" + results + "], counter: " + counter; |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | reportCompare(expect, actual, summary); |