ozeigermann    2004/05/25 06:51:57

  Modified:    transaction/src/test/org/apache/commons/transaction/file
                        FileResourceManagerTest.java
  Added:       transaction/src/java/org/apache/commons/transaction/util
                        RendezvousBarrier.java
  Log:
  Moved barrier from test into util package to have it generally available
  
  Revision  Changes    Path
  1.2       +6 -67     
jakarta-commons-sandbox/transaction/src/test/org/apache/commons/transaction/file/FileResourceManagerTest.java
  
  Index: FileResourceManagerTest.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/transaction/src/test/org/apache/commons/transaction/file/FileResourceManagerTest.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- FileResourceManagerTest.java      17 May 2004 13:26:42 -0000      1.1
  +++ FileResourceManagerTest.java      25 May 2004 13:51:57 -0000      1.2
  @@ -31,6 +31,7 @@
   
   import javax.transaction.Status;
   
  +import org.apache.commons.transaction.util.*;
   import org.apache.commons.transaction.util.FileHelper;
   import org.apache.commons.transaction.util.Jdk14Logger;
   import org.apache.commons.transaction.util.LoggerFacade;
  @@ -344,9 +345,9 @@
           
           rm.start();
   
  -        final RendezvousBarrier shutdownBarrier = new RendezvousBarrier("Shutdown", 
3);
  -        final RendezvousBarrier start2Barrier = new RendezvousBarrier("Start2");
  -        final RendezvousBarrier commit1Barrier = new RendezvousBarrier("Commit1");
  +        final RendezvousBarrier shutdownBarrier = new RendezvousBarrier("Shutdown", 
3, BARRIER_TIMEOUT, sLogger);
  +        final RendezvousBarrier start2Barrier = new RendezvousBarrier("Start2", 
BARRIER_TIMEOUT, sLogger);
  +        final RendezvousBarrier commit1Barrier = new RendezvousBarrier("Commit1", 
BARRIER_TIMEOUT, sLogger);
   
           final Object txId1 = "Create";
   
  @@ -615,66 +616,4 @@
           checkIsEmpty(WORK);
       }
   
  -    public static class RendezvousBarrier {
  -
  -        private final int parties;
  -        private final String name;
  -        private int count = 0;
  -        private long timeout;
  -
  -        public RendezvousBarrier(String name) {
  -            this(name, 2);
  -        }
  -
  -        public RendezvousBarrier(String name, int parties) {
  -            this(name, parties, BARRIER_TIMEOUT);
  -        }
  -
  -        public RendezvousBarrier(String name, int parties, long timeout) {
  -            this.parties = parties;
  -            this.name = name;
  -            this.timeout = timeout;
  -        }
  -
  -        public synchronized void call() throws InterruptedException {
  -            count++;
  -            if (count >= parties) {
  -                logger.fine("Thread " + Thread.currentThread().getName() + " by 
CALL COMLETING barrier " + name);
  -                notifyAll();
  -            }
  -        }
  -
  -        public synchronized void meet() throws InterruptedException {
  -            count++;
  -            if (count >= parties) {
  -                logger.fine("Thread " + Thread.currentThread().getName() + " by 
MEET COMLETING barrier " + name);
  -                notifyAll();
  -            } else {
  -                logger.fine(
  -                    "At barrier "
  -                        + name
  -                        + " thread "
  -                        + Thread.currentThread().getName()
  -                        + " WAITING for "
  -                        + (parties - count)
  -                        + " of "
  -                        + parties
  -                        + " parties");
  -                wait(timeout);
  -                if (count >= parties) {
  -                    logger.fine("Thread " + Thread.currentThread().getName() + " 
CONTINUING at barrier " + name);
  -                } else {
  -                    logger.fine("Thread " + Thread.currentThread().getName() + " 
FAILING at barrier " + name);
  -                    notifyAll();
  -                }
  -            }
  -        }
  -
  -        public synchronized void reset() {
  -            count = 0;
  -            notifyAll();
  -        }
  -
  -    }
  -    
   }
  
  
  
  1.1                  
jakarta-commons-sandbox/transaction/src/java/org/apache/commons/transaction/util/RendezvousBarrier.java
  
  Index: RendezvousBarrier.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-commons-sandbox/transaction/src/java/org/apache/commons/transaction/util/RendezvousBarrier.java,v
 1.1 2004/05/25 13:51:57 ozeigermann Exp $
   * $Revision: 1.1 $
   * $Date: 2004/05/25 13:51:57 $
   *
   * ====================================================================
   *
   * 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 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.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]">Oliver Zeigermann</a>
   */
  public class RendezvousBarrier {
  
      public static final int DEFAULT_TIMEOUT = 2000;
  
      protected final int parties;
      protected final String name;
      protected int count = 0;
      protected long timeout;
      protected LoggerFacade logger;
  
      public RendezvousBarrier(String name, LoggerFacade logger) {
          this(name, DEFAULT_TIMEOUT, logger);
      }
  
      public RendezvousBarrier(String name, long timeout, LoggerFacade logger) {
          this(name, 2, timeout, logger);
      }
  
      public RendezvousBarrier(String name, int parties, long timeout, LoggerFacade 
logger) {
          this.parties = parties;
          this.name = name;
          this.timeout = timeout;
          this.logger = logger;
      }
  
      /**
       * 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) {
              logger.logFine("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) {
              logger.logFine("Thread " + Thread.currentThread().getName() + " by MEET 
COMPLETING barrier " + name);
              notifyAll();
          } else {
              logger.logFine(
                  "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) {
                  logger.logFine("Thread " + Thread.currentThread().getName() + " 
CONTINUING at barrier " + name);
              } else {
                  logger.logFine("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() {
          logger.logFine("Resetting barrier " + name);
          count = 0;
          notifyAll();
      }
  
  }
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to