ozeigermann 2005/01/07 04:41:58 Modified: transaction/src/test/org/apache/commons/transaction/locking GenericLockTest.java Added: transaction/src/java/org/apache/commons/transaction/util TurnBarrier.java Removed: transaction/src/java/org/apache/commons/transaction/util SequenceBarrier.java Log: Removed sequence barrier in favor of a more untuitive turn based barrier. Revision Changes Path 1.1 jakarta-commons/transaction/src/java/org/apache/commons/transaction/util/TurnBarrier.java Index: TurnBarrier.java =================================================================== /* * $Header: /home/cvs/jakarta-commons/transaction/src/java/org/apache/commons/transaction/util/TurnBarrier.java,v 1.1 2005/01/07 12:41:58 ozeigermann Exp $ * $Revision: 1.1 $ * $Date: 2005/01/07 12:41:58 $ * * ==================================================================== * * Copyright 1999-2002 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.commons.transaction.util; /** * 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 contious 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 $Revision: 1.1 $ */ public class TurnBarrier { public static final long DEFAULT_TIMEOUT = Long.MAX_VALUE; protected final String name; protected int currentNumber; protected final int startNumber; protected final long timeout; protected LoggerFacade logger; /** * 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, LoggerFacade logger) { this(name, DEFAULT_TIMEOUT, logger); } /** * 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, LoggerFacade logger) { this(name, timeout, logger, 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, LoggerFacade logger, int startTurn) { this.name = name; this.timeout = timeout; this.logger = logger; 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); } } 1.10 +35 -56 jakarta-commons/transaction/src/test/org/apache/commons/transaction/locking/GenericLockTest.java Index: GenericLockTest.java =================================================================== RCS file: /home/cvs/jakarta-commons/transaction/src/test/org/apache/commons/transaction/locking/GenericLockTest.java,v retrieving revision 1.9 retrieving revision 1.10 diff -u -r1.9 -r1.10 --- GenericLockTest.java 28 Dec 2004 21:11:12 -0000 1.9 +++ GenericLockTest.java 7 Jan 2005 12:41:58 -0000 1.10 @@ -29,10 +29,10 @@ import junit.framework.TestCase; import junit.framework.TestSuite; -import org.apache.commons.transaction.util.SequenceBarrier; import org.apache.commons.transaction.util.LoggerFacade; import org.apache.commons.transaction.util.PrintWriterLogger; import org.apache.commons.transaction.util.RendezvousBarrier; +import org.apache.commons.transaction.util.TurnBarrier; /** * Tests for generic locks. @@ -268,21 +268,21 @@ final RendezvousBarrier restart = new RendezvousBarrier("restart", 3, TIMEOUT, sLogger); - final SequenceBarrier cb = new SequenceBarrier("cb1", TIMEOUT, sLogger); + final TurnBarrier cb = new TurnBarrier("cb1", TIMEOUT, sLogger, 1); for (int i = 0; i < CONCURRENT_TESTS; i++) { -// System.out.print("."); + System.out.print("."); Thread t1 = new Thread(new Runnable() { public void run() { try { - cb.count(2); + cb.waitForTurn(2); manager.upgradeLock(owner2, res1); - cb.count(3); - cb.count(6); + cb.signalTurn(3); + cb.waitForTurn(5); synchronized (manager.getLock(res1)) { - cb.count(7); + cb.signalTurn(6); manager.writeLock(owner2, res1); } // we must always be first as we will be preferred over @@ -292,9 +292,7 @@ if (first == null) first = owner2; } - cb.count(12); manager.releaseAll(owner2); - cb.count(13); synchronized (restart) { restart.meet(); restart.reset(); @@ -315,9 +313,9 @@ // next to get the lock as it is preferred over the main // thread // that only waits for a read lock - cb.count(8); + cb.waitForTurn(6); synchronized (manager.getLock(res1)) { - cb.count(9); + cb.signalTurn(7); manager.readLock(owner3, res1); } synchronized (this) { @@ -336,18 +334,17 @@ t2.start(); - cb.count(0); + cb.waitForTurn(1); manager.readLock(owner1, res1); - cb.count(1); - cb.count(4); + cb.signalTurn(2); + cb.waitForTurn(3); manager.release(owner1, res1); manager.readLock(owner1, res1); - cb.count(5); - cb.count(10); + cb.signalTurn(5); + cb.waitForTurn(7); synchronized (manager.getLock(res1)) { manager.releaseAll(owner1); } - cb.count(11); synchronized (restart) { restart.meet(); restart.reset(); @@ -385,19 +382,6 @@ * 8 released or released * * - * In SequenceBarrier notation this looks like - * - * Owner Owner Owner - * #1 #2 #3 - * 0read1 - * 2(write3) - * 4(write5) - * 6(release)7 - * 8release9 8release9 - * - * Round brackets mean atomic execution - * - * */ public void testPreference() throws Throwable { @@ -413,24 +397,22 @@ final RendezvousBarrier restart = new RendezvousBarrier("restart", 3, TIMEOUT, sLogger); - final SequenceBarrier cb = new SequenceBarrier("cb1", TIMEOUT, sLogger); + final TurnBarrier cb = new TurnBarrier("cb1", TIMEOUT, sLogger, 1); for (int i = 0; i < CONCURRENT_TESTS; i++) { -// System.out.print("."); + System.out.print("."); Thread t1 = new Thread(new Runnable() { public void run() { try { - cb.count(2); + cb.waitForTurn(2); synchronized (lock) { - cb.count(3); + cb.signalTurn(3); lock.acquire(owner2, ReadWriteLock.WRITE_LOCK, true, GenericLock.COMPATIBILITY_REENTRANT, true, TIMEOUT); } - cb.count(8); lock.release(owner2); - cb.count(9); synchronized (restart) { restart.meet(); restart.reset(); @@ -445,15 +427,13 @@ Thread t2 = new Thread(new Runnable() { public void run() { try { - cb.count(4); + cb.waitForTurn(3); synchronized (lock) { - cb.count(5); + cb.signalTurn(4); lock.acquire(owner3, ReadWriteLock.WRITE_LOCK, true, GenericLock.COMPATIBILITY_REENTRANT, true, TIMEOUT); } - cb.count(8); lock.release(owner3); - cb.count(9); synchronized (restart) { restart.meet(); restart.reset(); @@ -465,14 +445,13 @@ t2.start(); - cb.count(0); + cb.waitForTurn(1); lock.acquireRead(owner1, TIMEOUT); - cb.count(1); - cb.count(6); + cb.signalTurn(2); + cb.waitForTurn(4); synchronized (lock) { lock.release(owner1); } - cb.count(7); synchronized (restart) { restart.meet(); restart.reset(); @@ -496,7 +475,7 @@ final RendezvousBarrier restart = new RendezvousBarrier("restart", 2, TIMEOUT, sLogger); - final SequenceBarrier cb = new SequenceBarrier("cb1", TIMEOUT, sLogger); + final TurnBarrier cb = new TurnBarrier("cb1", TIMEOUT, sLogger, 1); for (int i = 0; i < CONCURRENT_TESTS; i++) { @@ -505,9 +484,9 @@ Thread t1 = new Thread(new Runnable() { public void run() { try { - cb.count(2); + cb.waitForTurn(2); manager.lock(owner2, res1, 1, true); - cb.count(3); + cb.signalTurn(3); manager.releaseAll(owner2); synchronized (restart) { restart.meet(); @@ -520,11 +499,11 @@ t1.start(); - cb.count(0); - manager.setGlobalTimeout(owner1, 500); + cb.waitForTurn(1); manager.lock(owner1, res1, 1, true); - cb.count(1); - cb.count(4); + manager.setGlobalTimeout(owner1, 500); + cb.signalTurn(2); + cb.waitForTurn(3); boolean failed = false; try { manager.tryLock(owner1, res1, 1, true); @@ -709,4 +688,4 @@ } } -} \ No newline at end of file +}
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]