Author: trustin Date: Tue Dec 6 00:37:11 2005 New Revision: 354362 URL: http://svn.apache.org/viewcvs?rev=354362&view=rev Log: * Changed IoFuture so it can use a lock object other than 'this'. * BaseIoSession.closeFuture now uses IoSession as a lock so it is fully thread-safe. Modified: directory/network/trunk/src/java/org/apache/mina/common/CloseFuture.java directory/network/trunk/src/java/org/apache/mina/common/ConnectFuture.java directory/network/trunk/src/java/org/apache/mina/common/IoFuture.java directory/network/trunk/src/java/org/apache/mina/common/WriteFuture.java directory/network/trunk/src/java/org/apache/mina/common/support/BaseIoSession.java Modified: directory/network/trunk/src/java/org/apache/mina/common/CloseFuture.java URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/common/CloseFuture.java?rev=354362&r1=354361&r2=354362&view=diff ============================================================================== --- directory/network/trunk/src/java/org/apache/mina/common/CloseFuture.java (original) +++ directory/network/trunk/src/java/org/apache/mina/common/CloseFuture.java Tue Dec 6 00:37:11 2005 @@ -37,6 +37,21 @@ public class CloseFuture extends IoFuture { /** + * Creates a new instance. + */ + public CloseFuture() + { + } + + /** + * Creates a new instance which uses the specified object as a lock. + */ + public CloseFuture( Object lock ) + { + super( lock ); + } + + /** * Returns true if the close request is finished and the session is closed. */ public boolean isClosed() Modified: directory/network/trunk/src/java/org/apache/mina/common/ConnectFuture.java URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/common/ConnectFuture.java?rev=354362&r1=354361&r2=354362&view=diff ============================================================================== --- directory/network/trunk/src/java/org/apache/mina/common/ConnectFuture.java (original) +++ directory/network/trunk/src/java/org/apache/mina/common/ConnectFuture.java Tue Dec 6 00:37:11 2005 @@ -48,6 +48,18 @@ failedFuture.setException( exception ); return failedFuture; } + + public ConnectFuture() + { + } + + /** + * Creates a new instance which uses the specified object as a lock. + */ + public ConnectFuture( Object lock ) + { + super( lock ); + } /** * Returns {@link IoSession} which is the result of connect operation. Modified: directory/network/trunk/src/java/org/apache/mina/common/IoFuture.java URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/common/IoFuture.java?rev=354362&r1=354361&r2=354362&view=diff ============================================================================== --- directory/network/trunk/src/java/org/apache/mina/common/IoFuture.java (original) +++ directory/network/trunk/src/java/org/apache/mina/common/IoFuture.java Tue Dec 6 00:37:11 2005 @@ -27,6 +27,7 @@ */ public class IoFuture { + private final Object lock; private Object result; private Callback callback; private boolean ready; @@ -52,21 +53,37 @@ */ protected IoFuture() { + this.lock = this; + } + + /** + * Creates a new instance which uses the specified object as a lock. + */ + protected IoFuture( Object lock ) + { + if( lock == null ) + { + throw new NullPointerException( "lock" ); + } + this.lock = lock; } /** * Wait for the asynchronous operation to end. */ - public synchronized void join() + public void join() { - while( !ready ) + synchronized( lock ) { - try - { - wait(); - } - catch( InterruptedException e ) + while( !ready ) { + try + { + lock.wait(); + } + catch( InterruptedException e ) + { + } } } } @@ -76,38 +93,42 @@ * * @return true if the operation is finished. */ - public synchronized boolean join( long timeoutInMillis ) + public boolean join( long timeoutInMillis ) { long startTime = ( timeoutInMillis <= 0 ) ? 0 : System .currentTimeMillis(); long waitTime = timeoutInMillis; - if( ready ) - { - return ready; - } - else if( waitTime <= 0 ) + + synchronized( lock ) { - return ready; - } - - for( ;; ) - { - try + if( ready ) { - wait( waitTime ); + return ready; } - catch( InterruptedException e ) + else if( waitTime <= 0 ) { + return ready; } - if( ready ) - return true; - else + for( ;; ) { - waitTime = timeoutInMillis - ( System.currentTimeMillis() - startTime ); - if( waitTime <= 0 ) + try + { + lock.wait( waitTime ); + } + catch( InterruptedException e ) + { + } + + if( ready ) + return true; + else { - return ready; + waitTime = timeoutInMillis - ( System.currentTimeMillis() - startTime ); + if( waitTime <= 0 ) + { + return ready; + } } } } @@ -116,40 +137,52 @@ /** * Returns if the asynchronous operation is finished. */ - public synchronized boolean isReady() + public boolean isReady() { - return ready; + synchronized( lock ) + { + return ready; + } } /** * Sets the result of the asynchronous operation, and mark it as finished. */ - protected synchronized void setValue( Object newValue ) + protected void setValue( Object newValue ) { - result = newValue; - ready = true; - notifyAll(); - - if( callback != null ) + synchronized( lock ) { - invokeCallback(); + result = newValue; + ready = true; + lock.notifyAll(); + + if( callback != null ) + { + invokeCallback(); + } } } /** * Returns the result of the asynchronous operation. */ - protected synchronized Object getValue() + protected Object getValue() { - return result; + synchronized( lock ) + { + return result; + } } /** * Returns a {@link Callback} which is associated with this future. */ - public synchronized Callback getCallback() + public Callback getCallback() { - return callback; + synchronized( lock ) + { + return callback; + } } /** @@ -157,17 +190,20 @@ * becomes available. If tth result has already become obtained, * the specified callback is notified immediately */ - public synchronized void setCallback( Callback callback ) + public void setCallback( Callback callback ) { if( callback == null ) { throw new NullPointerException( "callback" ); } - this.callback = callback; - if( ready ) + synchronized( lock ) { - invokeCallback(); + this.callback = callback; + if( ready ) + { + invokeCallback(); + } } } Modified: directory/network/trunk/src/java/org/apache/mina/common/WriteFuture.java URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/common/WriteFuture.java?rev=354362&r1=354361&r2=354362&view=diff ============================================================================== --- directory/network/trunk/src/java/org/apache/mina/common/WriteFuture.java (original) +++ directory/network/trunk/src/java/org/apache/mina/common/WriteFuture.java Tue Dec 6 00:37:11 2005 @@ -64,6 +64,21 @@ } /** + * Creates a new instance. + */ + public WriteFuture() + { + } + + /** + * Creates a new instance which uses the specified object as a lock. + */ + public WriteFuture( Object lock ) + { + super( lock ); + } + + /** * Returns true if the write operation is finished successfully. */ public boolean isWritten() Modified: directory/network/trunk/src/java/org/apache/mina/common/support/BaseIoSession.java URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/common/support/BaseIoSession.java?rev=354362&r1=354361&r2=354362&view=diff ============================================================================== --- directory/network/trunk/src/java/org/apache/mina/common/support/BaseIoSession.java (original) +++ directory/network/trunk/src/java/org/apache/mina/common/support/BaseIoSession.java Tue Dec 6 00:37:11 2005 @@ -45,7 +45,7 @@ /** * A future that will be set 'closed' when the connection is closed. */ - private final CloseFuture closeFuture = new CloseFuture(); + private final CloseFuture closeFuture = new CloseFuture( this ); private boolean closing; // Configuration variables @@ -120,7 +120,7 @@ public synchronized WriteFuture write( Object message ) { WriteFuture future = new WriteFuture(); - if( isClosing() ) + if( isClosing() || !isConnected() ) { future.setWritten( false ); }