js/src/tests/js1_8/extensions/dekker.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

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

mercurial