directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From trus...@apache.org
Subject svn commit: r379350 - /directory/trunks/mina/core/src/main/java/org/apache/mina/common/support/IoFilterLifeCycleManager.java
Date Tue, 21 Feb 2006 05:36:05 GMT
Author: trustin
Date: Mon Feb 20 21:35:59 2006
New Revision: 379350

URL: http://svn.apache.org/viewcvs?rev=379350&view=rev
Log:
Resolved issue: DIRMINA-169 (Deadlock in ThreadPoolFilter)
* Changed IoFilterLifeCycleManager to exit the synchronize block as soon as possible.

Modified:
    directory/trunks/mina/core/src/main/java/org/apache/mina/common/support/IoFilterLifeCycleManager.java

Modified: directory/trunks/mina/core/src/main/java/org/apache/mina/common/support/IoFilterLifeCycleManager.java
URL: http://svn.apache.org/viewcvs/directory/trunks/mina/core/src/main/java/org/apache/mina/common/support/IoFilterLifeCycleManager.java?rev=379350&r1=379349&r2=379350&view=diff
==============================================================================
--- directory/trunks/mina/core/src/main/java/org/apache/mina/common/support/IoFilterLifeCycleManager.java
(original)
+++ directory/trunks/mina/core/src/main/java/org/apache/mina/common/support/IoFilterLifeCycleManager.java
Mon Feb 20 21:35:59 2006
@@ -47,13 +47,23 @@
     {
     }
     
-    public synchronized void callInitIfNecessary( IoFilter filter )
+    public void callInitIfNecessary( IoFilter filter )
     {
-        ReferenceCount count = ( ReferenceCount ) counts.get( filter );
-        if( count == null )
+        boolean callInit = false;
+        
+        synchronized( this )
+        {
+            ReferenceCount count = ( ReferenceCount ) counts.get( filter );
+            if( count == null )
+            {
+                count = new ReferenceCount();
+                counts.put( filter, count );
+                callInit = true;
+            }
+        }
+        
+        if( callInit )
         {
-            count = new ReferenceCount();
-            counts.put( filter, count );
             try
             {
                 filter.init();
@@ -66,16 +76,19 @@
         }
     }
     
-    public synchronized void callOnPreAdd( IoFilterChain chain, String name, IoFilter filter,
NextFilter nextFilter )
+    public void callOnPreAdd( IoFilterChain chain, String name, IoFilter filter, NextFilter
nextFilter )
     {
-        ReferenceCount count = ( ReferenceCount ) counts.get( filter );
-        if( count == null )
+        synchronized( this )
         {
-            throw new IllegalStateException();
+            ReferenceCount count = ( ReferenceCount ) counts.get( filter );
+            if( count == null )
+            {
+                throw new IllegalStateException();
+            }
+            
+            count.increase();
         }
         
-        count.increase();
-
         try
         {
             filter.onPreAdd( chain, name, nextFilter );
@@ -88,17 +101,20 @@
         }
     }
 
-    public synchronized void callOnPreRemove( IoFilterChain chain, String name, IoFilter
filter, NextFilter nextFilter )
+    public void callOnPreRemove( IoFilterChain chain, String name, IoFilter filter, NextFilter
nextFilter )
     {
-        ReferenceCount count = ( ReferenceCount ) counts.get( filter );
-        if( count == null || count.get() == 0 )
+        synchronized( this )
         {
-            return;
+            ReferenceCount count = ( ReferenceCount ) counts.get( filter );
+            if( count == null || count.get() == 0 )
+            {
+                return;
+            }
         }
 
         try
         {
-            filter.onPreRemove( chain, name, nextFilter);
+            filter.onPreRemove( chain, name, nextFilter );
         }
         catch( Throwable t )
         {
@@ -108,12 +124,15 @@
         }
     }
     
-    public synchronized void callOnPostAdd( IoFilterChain chain, String name, IoFilter filter,
NextFilter nextFilter )
+    public void callOnPostAdd( IoFilterChain chain, String name, IoFilter filter, NextFilter
nextFilter )
     {
-        ReferenceCount count = ( ReferenceCount ) counts.get( filter );
-        if( count == null )
+        synchronized( this )
         {
-            throw new IllegalStateException();
+            ReferenceCount count = ( ReferenceCount ) counts.get( filter );
+            if( count == null )
+            {
+                throw new IllegalStateException();
+            }
         }
         
         try
@@ -123,7 +142,7 @@
         catch( Throwable t )
         {
             // Revert back the reference count.
-            count.decrease();
+            decreaseCountSafely( filter );
 
             throw new IoFilterLifeCycleException(
                     "onPostAdd(): " + name + ':' + filter + " in " +
@@ -131,12 +150,15 @@
         }
     }
 
-    public synchronized void callOnPostRemove( IoFilterChain chain, String name, IoFilter
filter, NextFilter nextFilter )
+    public void callOnPostRemove( IoFilterChain chain, String name, IoFilter filter, NextFilter
nextFilter )
     {
-        ReferenceCount count = ( ReferenceCount ) counts.get( filter );
-        if( count == null || count.get() == 0 )
+        synchronized( this )
         {
-            return;
+            ReferenceCount count = ( ReferenceCount ) counts.get( filter );
+            if( count == null || count.get() == 0 )
+            {
+                return;
+            }
         }
 
         try
@@ -151,21 +173,42 @@
         }
         finally
         {
-            count.decrease();
+            decreaseCountSafely( filter );
         }
     }
 
-    public synchronized void callDestroyIfNecessary( IoFilter filter )
+    private synchronized void decreaseCountSafely( IoFilter filter )
     {
         ReferenceCount count = ( ReferenceCount ) counts.get( filter );
         if( count == null )
         {
-            return;
+            throw new IllegalStateException();
         }
         
-        if( count.get() == 0 )
+        count.decrease();
+    }
+
+    public synchronized void callDestroyIfNecessary( IoFilter filter )
+    {
+        boolean callDestroy = false;
+        
+        synchronized( this )
+        {
+            ReferenceCount count = ( ReferenceCount ) counts.get( filter );
+            if( count == null )
+            {
+                return;
+            }
+            
+            if( count.get() == 0 )
+            {
+                counts.remove( filter );
+                callDestroy = true;
+            }
+        }
+
+        if( callDestroy )
         {
-            counts.remove( filter );
             try
             {
                 filter.destroy();



Mime
View raw message