// $Archive:: $ // $Date: Wed, 10 Jan 2001 02:20:47 -0800 $ // $Revision: 1.1 $ // Description: Titanium synchronization tester // Copyright 2000, Dan Bonachea class Flag { private volatile int val; public Flag(int init) { val = init; } public Flag() { this(-1); } public void set(int val) { this.val = val; } public void set() { set(Ti.thisProc()); } public int get() { return val; } public void check(int val) { if (this.val != val) throw new InternalError("flag mismatch - illegal synchronization detected"); } public void check() { check(Ti.thisProc()); } } //------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------ public class synctest { private static Flag flag; //------------------------------------------------------------------------------------ // test class synchronized method public static void test1(int iters) { for (int i=0; i < iters; i++) { test1sub(); } } private static synchronized void test1sub() { flag.set(); for (int i=0;i < 10;i++) { doWork(); flag.check(); } } //------------------------------------------------------------------------------------ public static void main(String single [] single args) { if (Ti.thisProc() == 0) System.out.println("Beginning static method synchronization test (this could take awhile)."); if (Ti.numProcs() < 4 && Ti.thisProc() == 0) System.out.println("WARNING: Ti.numProcs() == " + Ti.numProcs() + ", too few for an accurate test!!"); int single iters = 100; Flag success = broadcast new Flag(1) from 0; flag = broadcast new Flag() from 0; success.set(1); Ti.barrier(); try { test1(iters); } catch (InternalError exn) { System.out.println("P#"+Ti.thisProc()+"/"+Ti.numProcs()+": Test1 FAILED: " + exn.getMessage()); success.set(0); } Ti.barrier(); if (Ti.thisProc() == 0) { if (success.get() != 0) System.out.println("Test1 PASSED."); else System.out.println("Test1 FAILED"); } Ti.barrier(); if (Ti.thisProc() == 0) System.out.println("Tests complete"); } //------------------------------------------------------------------------------------ private static double junk; private static java.util.Random rand = broadcast new java.util.Random() from 0; public static void doWork() { double x = 1.00001; int [] junkarr = new int[16535]; try { Thread.sleep((int)(Ti.thisProc()*0.1)); } catch (InterruptedException exn) { throwInternalError("thread was interrupted during a sleep in doWork"); } for (int i = 0; i < 100000; i++) { x = x * 0.99999 / 0.9999; } try { Thread.sleep(rand.nextDouble() > 0.5 ? 1 : 0); // for platforms w/o usleep } catch (InterruptedException exn) { throwInternalError("thread was interrupted during a sleep in doWork"); } junk = x; Thread.yield(); } public static void throwInternalError(String s) { throw new InternalError(s); } }