db-jdo-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mz...@apache.org
Subject svn commit: r384047 - in /db/jdo/trunk/tck20/src/java/org/apache/jdo/tck/util: RogueBarrier.java ThreadExceptionHandler.java
Date Tue, 07 Mar 2006 23:05:54 GMT
Author: mzaun
Date: Tue Mar  7 15:05:53 2006
New Revision: 384047

URL: http://svn.apache.org/viewcvs?rev=384047&view=rev
Log:
JDO-309: minor extensions to tck20 test utilities

Added:
    db/jdo/trunk/tck20/src/java/org/apache/jdo/tck/util/RogueBarrier.java   (with props)
Modified:
    db/jdo/trunk/tck20/src/java/org/apache/jdo/tck/util/ThreadExceptionHandler.java

Added: db/jdo/trunk/tck20/src/java/org/apache/jdo/tck/util/RogueBarrier.java
URL: http://svn.apache.org/viewcvs/db/jdo/trunk/tck20/src/java/org/apache/jdo/tck/util/RogueBarrier.java?rev=384047&view=auto
==============================================================================
--- db/jdo/trunk/tck20/src/java/org/apache/jdo/tck/util/RogueBarrier.java (added)
+++ db/jdo/trunk/tck20/src/java/org/apache/jdo/tck/util/RogueBarrier.java Tue Mar  7 15:05:53
2006
@@ -0,0 +1,165 @@
+/*
+ * Copyright 2005 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at 
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software 
+ * distributed under the License is distributed on an "AS IS" BASIS, 
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
+ * See the License for the specific language governing permissions and 
+ * limitations under the License.
+ */
+
+package org.apache.jdo.tck.util;
+
+/**
+ * Allows a set of threads to wait for all to reach a common barrier
+ * point.
+ *
+ * <p>The <tt>RogueBarrier</tt> uses a rigorous breakage model:
+ * No thread can leave a barrier point prematurely.  All attempts to
+ * interrupt an awaiting thread via an {@link InterruptedException}
+ * are being ignored.
+ *
+ * @author Martin Zaun
+ */
+public class RogueBarrier {
+
+    /** The internal synchronization object */
+    private final Object lock = new Object();
+    
+    /** The number of parties needed to trip the barrier */
+    private final int parties;
+
+    /** The current generation's trip condition (need a boolean holder) */
+    private boolean[] tripped = new boolean[1];
+
+    /** The number of parties still waiting for */
+    private int missing;
+
+    /**
+     * Creates a new <tt>RogueBarrier</tt> that will trip when the
+     * given number of parties (threads) are waiting upon it.
+     *
+     * @param parties the number of threads that must invoke {@link #await}
+     * before the barrier is tripped.
+     *
+     * @throws IllegalArgumentException if <tt>parties</tt> is less than 1.
+     */
+    public RogueBarrier(int parties) {
+        if (parties <= 0) {
+            throw new IllegalArgumentException("Argument for parties must be positive.");
+        }
+        this.parties = parties;
+        missing = parties;
+    }
+
+    /**
+     * Waits until all parties have invoked <tt>await</tt> on this barrier.
+     *
+     * @return the arrival index of the current thread, where index
+     *  <tt>parties - 1</tt> indicates the first to arrive and
+     *  zero indicates the last to arrive.
+     */
+    public int await() {
+        synchronized (lock) {
+            //assert (parties > 0);
+            //System.out.println("thread = " + Thread.currentThread().getId());
+            
+            // if tripping, reset barrier and notify waiting threads
+            final int index = --missing;
+            if (index == 0) {
+                tripped[0] = true; // notify current generation
+                lock.notifyAll();
+                tripped = new boolean[1]; // start new generation
+                missing = parties;
+                return 0;
+            }
+            
+            // wait until tripped
+            final boolean[] myTripped = tripped; // my generation's condition
+            do {
+                try {
+                    lock.wait();
+                } catch (InterruptedException ie) { // swallow
+                }
+            } while (!myTripped[0]);
+            return index;
+        }
+    }
+
+    static public class Test extends Thread {
+        static private final int parties = 1000;
+        static private final RogueBarrier b = new RogueBarrier(parties);
+        static private int count;
+
+        public void run() {
+            final int i0 = b.await();
+            if (i0 == 0) {
+                System.out.println("    incrementing count to " + parties
+                                   + " ...");
+            }
+            synchronized (Test.class) {
+                count++;
+            }
+
+            final int i1 = b.await();
+            synchronized (Test.class) {
+                if (i1 == 0) {
+                    System.out.println("    count = " + count);
+                }
+                //assert (count == parties+1);
+            }
+
+            final int i2 = b.await();
+            if (i2 == 0) {
+                System.out.println("    decrementing count to zero ...");
+            }
+            synchronized (Test.class) {
+                count--;
+            }
+
+            final int i3 = b.await();
+            synchronized (Test.class) {
+                if (i3 == 0) {
+                    System.out.println("    count = " + count);
+                }
+                //assert (count == 0);
+            }
+
+            b.await();
+        }
+
+        static public void test() {
+            System.out.println("--> RogueBarrier$Test.test()");
+
+            // start threads
+            final Thread threads[] = new Test[parties];
+            for (int i = 0; i < parties; i++) {
+                threads[i] = new Test();
+                threads[i].start();
+            }
+
+            // wait for all threads to finish
+            for (int i = 0; i < parties; i++) {
+                while (true) {
+                    try {
+                        threads[i].join();
+                        break;
+                    } catch (InterruptedException e) {
+                    }
+                }
+            }
+
+            System.out.println("<-- RogueBarrier$Test.test()");
+        }
+    
+        static public void main(String[] args) {
+            Test.test();
+        }
+    }
+}

Propchange: db/jdo/trunk/tck20/src/java/org/apache/jdo/tck/util/RogueBarrier.java
------------------------------------------------------------------------------
    svn:executable = *

Modified: db/jdo/trunk/tck20/src/java/org/apache/jdo/tck/util/ThreadExceptionHandler.java
URL: http://svn.apache.org/viewcvs/db/jdo/trunk/tck20/src/java/org/apache/jdo/tck/util/ThreadExceptionHandler.java?rev=384047&r1=384046&r2=384047&view=diff
==============================================================================
--- db/jdo/trunk/tck20/src/java/org/apache/jdo/tck/util/ThreadExceptionHandler.java (original)
+++ db/jdo/trunk/tck20/src/java/org/apache/jdo/tck/util/ThreadExceptionHandler.java Tue Mar
 7 15:05:53 2006
@@ -32,7 +32,7 @@
      * Map of uncaught exceptions. The thread is the key and the uncaught
      * Throwable is the value in the map. 
      */
-    private Map uncaughtExceptions = new HashMap();
+    private final Map uncaughtExceptions = new HashMap();
     
     /** Constructor. */
     public ThreadExceptionHandler() {
@@ -53,11 +53,18 @@
     }
 
     /** 
-     * Returns all uncaught exception stored in this ThreadGroup. 
+     * Returns all uncaught exceptions stored in this ThreadGroup. 
      * Each element in the returned set is a Map.Entry with the 
      * thread as the key and the uncaught Throwable is the value.
      */
     public Set getAllUncaughtExceptions() {
         return uncaughtExceptions.entrySet();
+    }
+
+    /** 
+     * Clears all exceptions in this ThreadGroup. 
+     */
+    public void clear() {
+        uncaughtExceptions.clear();
     }
 }



Mime
View raw message