Author: ozeigermann Date: Sat Jul 28 10:50:40 2007 New Revision: 560582 URL: http://svn.apache.org/viewvc?view=rev&rev=560582 Log: Ported concurrent helper classes from 1.x
Added: jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/util/RendezvousBarrier.java jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/util/TurnBarrier.java Added: jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/util/RendezvousBarrier.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/util/RendezvousBarrier.java?view=auto&rev=560582 ============================================================================== --- jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/util/RendezvousBarrier.java (added) +++ jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/util/RendezvousBarrier.java Sat Jul 28 10:50:40 2007 @@ -0,0 +1,120 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.commons.transaction.util; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +/** + * Simple barrier that blocks until all parties have either called or have + * arrived at the meeting point. Very useful for testing or other purposes that + * require to make concurrent settings deterministic. + * + * @version $Id: RendezvousBarrier.java 493628 2007-01-07 01:42:48Z joerg $ + */ +public class RendezvousBarrier { + + public static final int DEFAULT_TIMEOUT = 20000; + + private Log logger = LogFactory.getLog(getClass()); + + protected final int parties; + + protected final String name; + + protected int count = 0; + + protected long timeout; + + public RendezvousBarrier(String name) { + this(name, DEFAULT_TIMEOUT); + } + + public RendezvousBarrier(String name, long timeout) { + this(name, 2, timeout); + } + + public RendezvousBarrier(String name, int parties, long timeout) { + this.parties = parties; + this.name = name; + this.timeout = timeout; + } + + /** + * Notify the barrier that you (the current thread) will not come to the + * meeting point. Same thing as [EMAIL PROTECTED] #meet()}, but does not not let you + * wait. + */ + public synchronized void call() { + count++; + if (count >= parties) { + if (logger.isTraceEnabled()) + logger.trace("Thread " + Thread.currentThread().getName() + + " by CALL COMPLETING barrier " + name); + notifyAll(); + } + } + + /** + * Meet at this barrier. The current thread will either block when there are + * missing parties for this barrier or it is the last one to complete this + * meeting and the barrier will release its block. In this case all other + * waiting threads will be notified. + * + * @throws InterruptedException + * if the current thread is interrupted while waiting + */ + public synchronized void meet() throws InterruptedException { + count++; + if (count >= parties) { + if (logger.isTraceEnabled()) + logger.trace("Thread " + Thread.currentThread().getName() + + " by MEET COMPLETING barrier " + name); + notifyAll(); + } else { + if (logger.isTraceEnabled()) { + logger.trace("At barrier " + name + " thread " + Thread.currentThread().getName() + + " WAITING for " + (parties - count) + " of " + parties + " parties"); + } + wait(timeout); + if (count == 0) { + // means the barrier has been reset + } else if (count >= parties) { + if (logger.isTraceEnabled()) + logger.trace("Thread " + Thread.currentThread().getName() + + " CONTINUING at barrier " + name); + } else { + if (logger.isTraceEnabled()) + logger.trace("Thread " + Thread.currentThread().getName() + + " FAILING at barrier " + name); + notifyAll(); + } + } + } + + /** + * Releases all waiting threads and resets the number of parties already + * arrived. + */ + public synchronized void reset() { + if (logger.isTraceEnabled()) + logger.trace("Resetting barrier " + name); + count = 0; + notifyAll(); + } + +} \ No newline at end of file Added: jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/util/TurnBarrier.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/util/TurnBarrier.java?view=auto&rev=560582 ============================================================================== --- jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/util/TurnBarrier.java (added) +++ jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/util/TurnBarrier.java Sat Jul 28 10:50:40 2007 @@ -0,0 +1,135 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.commons.transaction.util; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +/** + * Simple turn based barrier to make a sequence of calls from different threads + * deterministic. This is very useful for testing where you want to have a + * continuous flow throughout different threads. The idea is to have an ordered + * sequence of steps where step n can not be executed before n-1. + * + * @version $Id: TurnBarrier.java 493628 2007-01-07 01:42:48Z joerg $ + */ +public class TurnBarrier { + + public static final long DEFAULT_TIMEOUT = Long.MAX_VALUE; + + private Log logger = LogFactory.getLog(getClass()); + + protected final String name; + + protected int currentNumber; + + protected final int startNumber; + + protected final long timeout; + + /** + * Creates a new turn barrier starting with turn 0 with an unlimited + * timeout. + * + * @param name + * the name of the barrier + * @param logger + * logger for debug output + */ + public TurnBarrier(String name) { + this(name, DEFAULT_TIMEOUT); + } + + /** + * Creates a new turn barrier starting with turn 0. + * + * @param name + * the name of the barrier + * @param timeout + * timeout for threads to wait for their turn + * @param logger + * logger for debug output + */ + public TurnBarrier(String name, long timeout) { + this(name, timeout, 0); + } + + /** + * Creates a new turn barrier. + * + * @param name + * the name of the barrier + * @param timeout + * timeout for threads to wait for their turn + * @param logger + * logger for debug output + * @param startTurn + * the turn to start with + */ + public TurnBarrier(String name, long timeout, int startTurn) { + this.name = name; + this.timeout = timeout; + this.startNumber = startTurn; + this.currentNumber = startTurn; + } + + /** + * Blockingly waits for the given turn. If a timeout occurs a runtime + * exception will be thrown. + * + * @param turnNumber + * the turn number to wait for + * @throws InterruptedException + * thrown if the thread is interrupted while waiting + * @throws RuntimeException + * thrown when timed out + */ + public synchronized void waitForTurn(int turnNumber) throws InterruptedException, + RuntimeException { + if (turnNumber > currentNumber) { + long started = System.currentTimeMillis(); + for (long remaining = timeout; remaining > 0 && turnNumber > currentNumber; remaining = timeout + - (System.currentTimeMillis() - started)) { + wait(remaining); + } + } + if (turnNumber > currentNumber) { + throw new RuntimeException("Timed out while waiting for our turn"); + } + } + + /** + * Signals the next turn. Any thread waiting for the turn will be allowed to + * continue. + * + * @param turnNumber + * the next turn number + */ + public synchronized void signalTurn(int turnNumber) { + currentNumber = turnNumber; + notifyAll(); + } + + /** + * Starts the barrier over again. The next turn will be the one the barrier + * was started with. + * + */ + public synchronized void reset() { + signalTurn(startNumber); + } +} \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]