Bug 469739 - Add support for displaying Vista UAC shield icon; r=joe sr=vladimir
[wine-gecko.git] / js / tests / js1_8 / extensions / peterson.js
blobaaf0a163edf6d8f22deea62f8b2a5ecf4ff68631
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is JavaScript Engine testing utilities.
17 * The Initial Developer of the Original Code is
18 * Mozilla Foundation.
19 * Portions created by the Initial Developer are Copyright (C) 2005
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s): Guoxin Fan <gfan@sta.samsung.com>
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 var gTestfile = 'peterson.js';
39 //-----------------------------------------------------------------------
41 var summary = "Peterson's algorithm for mutual exclusion";
43 printStatus(summary);
45 var N = 500; // number of iterations
47 // the mutex mechanism
48 var f = [false, false];
49 var turn = 0;
51 // the resource being protected
52 var counter = 0;
54 function worker(me) {
55 let him = 1 - me;
57 for (let i = 0; i < N; i++) {
58 // enter the mutex
59 f[me] = true;
60 turn = him;
61 while (f[him] && turn == him)
62 ; // busy wait
64 // critical section
65 let x = counter;
66 sleep(0.003);
67 counter = x+1;
69 // leave the mutex
70 f[me] = false;
73 return 'ok';
76 var expect;
77 var actual;
79 if (typeof scatter == 'undefined' || typeof sleep == 'undefined') {
80 print('Test skipped. scatter or sleep not defined.');
81 expect = actual = 'Test skipped.';
82 } else {
83 var results = scatter ([function() { return worker(0); },
84 function() { return worker(1); }]);
85 expect = "Thread status: [ok,ok], counter: " + (2 * N);
86 actual = "Thread status: [" + results + "], counter: " + counter;
89 reportCompare(expect, actual, summary);