directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From trus...@apache.org
Subject svn commit: r164519 - in /directory/network/trunk/src/java/org/apache/mina/util: BaseThreadPool.java Event.java
Date Mon, 25 Apr 2005 02:20:46 GMT
Author: trustin
Date: Sun Apr 24 19:20:46 2005
New Revision: 164519

URL: http://svn.apache.org/viewcvs?rev=164519&view=rev
Log:
Optimized BaseThreadPool by recent benchmark result.

Added:
    directory/network/trunk/src/java/org/apache/mina/util/Event.java   (with props)
Modified:
    directory/network/trunk/src/java/org/apache/mina/util/BaseThreadPool.java

Modified: directory/network/trunk/src/java/org/apache/mina/util/BaseThreadPool.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/util/BaseThreadPool.java?rev=164519&r1=164518&r2=164519&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/util/BaseThreadPool.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/util/BaseThreadPool.java Sun Apr 24 19:20:46
2005
@@ -49,15 +49,15 @@
 
     private static volatile int threadId = 0;
 
-    private Map buffers = new IdentityHashMap();
+    private final Map buffers = new IdentityHashMap();
 
-    private Stack followers = new Stack();
+    private final Stack followers = new Stack();
 
-    private Worker leader;
+    private final BlockingSet readySessionBuffers = new BlockingSet();
 
-    private BlockingSet readySessionBuffers = new BlockingSet();
+    private final Set busySessionBuffers = new HashSet();
 
-    private Set busySessionBuffers = new HashSet();
+    private Worker leader;
 
     private int maximumPoolSize = DEFAULT_MAXIMUM_POOL_SIZE;
 
@@ -173,12 +173,15 @@
     protected void fireEvent( Object nextFilter, Session session,
                               EventType type, Object data )
     {
-        SessionBuffer buf = getSessionBuffer( session );
+        final BlockingSet readySessionBuffers = this.readySessionBuffers;
+        final Set busySessionBuffers = this.busySessionBuffers;
+        final SessionBuffer buf = getSessionBuffer( session );
+        final Queue eventQueue = buf.eventQueue;
+        final Event event = new Event( type, nextFilter, data );
+
         synchronized( buf )
         {
-            buf.nextFilters.push( nextFilter );
-            buf.eventTypes.push( type );
-            buf.eventDatum.push( data );
+            eventQueue.push( event );
         }
 
         synchronized( readySessionBuffers )
@@ -199,6 +202,7 @@
 
     private SessionBuffer getSessionBuffer( Session session )
     {
+        final Map buffers = this.buffers;
         SessionBuffer buf = ( SessionBuffer ) buffers.get( session );
         if( buf == null )
         {
@@ -217,9 +221,11 @@
 
     private void removeSessionBuffer( SessionBuffer buf )
     {
+        final Map buffers = this.buffers;
+        final Session session = buf.session;
         synchronized( buffers )
         {
-            buffers.remove( buf.session );
+            buffers.remove( session );
         }
     }
 
@@ -227,11 +233,7 @@
     {
         private final Session session;
 
-        private final Queue nextFilters = new Queue();
-
-        private final Queue eventTypes = new Queue();
-
-        private final Queue eventDatum = new Queue();
+        private final Queue eventQueue = new Queue();
 
         private SessionBuffer( Session session )
         {
@@ -251,6 +253,7 @@
 
         public void lead()
         {
+            final Object promotionLock = this.promotionLock;
             synchronized( promotionLock )
             {
                 leader = this;
@@ -284,6 +287,7 @@
         private SessionBuffer fetchBuffer()
         {
             SessionBuffer buf = null;
+            BlockingSet readySessionBuffers = BaseThreadPool.this.readySessionBuffers;
             synchronized( readySessionBuffers )
             {
                 do
@@ -311,10 +315,10 @@
                         buf = ( SessionBuffer ) it.next();
                         it.remove();
                     }
-                    while( buf != null && buf.nextFilters.isEmpty()
+                    while( buf != null && buf.eventQueue.isEmpty()
                            && it.hasNext() );
                 }
-                while( buf != null && buf.nextFilters.isEmpty() );
+                while( buf != null && buf.eventQueue.isEmpty() );
             }
 
             return buf;
@@ -322,27 +326,26 @@
 
         private void processEvents( SessionBuffer buf )
         {
-            Session session = buf.session;
+            final Session session = buf.session;
+            final Queue eventQueue = buf.eventQueue;
             for( ;; )
             {
-                Object nextFilter;
-                EventType type;
-                Object data;
+                Event event;
                 synchronized( buf )
                 {
-                    nextFilter = buf.nextFilters.pop();
-                    if( nextFilter == null )
+                    event = ( Event ) eventQueue.pop();
+                    if( event == null )
                         break;
-
-                    type = ( EventType ) buf.eventTypes.pop();
-                    data = buf.eventDatum.pop();
                 }
-                processEvent( nextFilter, session, type, data );
+                processEvent( event.getNextFilter(), session,
+                              event.getType(), event.getData() );
             }
         }
 
         private void follow()
         {
+            final Object promotionLock = this.promotionLock;
+            final Stack followers = BaseThreadPool.this.followers;
             synchronized( promotionLock )
             {
                 if( this != leader )
@@ -357,10 +360,14 @@
 
         private void releaseBuffer( SessionBuffer buf )
         {
+            final BlockingSet readySessionBuffers = BaseThreadPool.this.readySessionBuffers;
+            final Set busySessionBuffers = BaseThreadPool.this.busySessionBuffers;
+            final Queue eventQueue = buf.eventQueue;
+
             synchronized( readySessionBuffers )
             {
                 busySessionBuffers.remove( buf );
-                if( buf.nextFilters.isEmpty() )
+                if( eventQueue.isEmpty() )
                 {
                     removeSessionBuffer( buf );
                 }
@@ -373,6 +380,8 @@
 
         private boolean waitForPromotion()
         {
+            final Object promotionLock = this.promotionLock;
+
             synchronized( promotionLock )
             {
                 if( this != leader )
@@ -411,6 +420,7 @@
 
         private void giveUpLead()
         {
+            final Stack followers = BaseThreadPool.this.followers;
             Worker worker;
             synchronized( followers )
             {

Added: directory/network/trunk/src/java/org/apache/mina/util/Event.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/util/Event.java?rev=164519&view=auto
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/util/Event.java (added)
+++ directory/network/trunk/src/java/org/apache/mina/util/Event.java Sun Apr 24 19:20:46 2005
@@ -0,0 +1,57 @@
+/*
+ *   @(#) $Id$
+ * 
+ *   Copyright 2004 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.mina.util;
+
+/**
+ * MINA Event used by {@link BaseThreadPool} internally.
+ * 
+ * @author The Apache Directory Project (dev@directory.apache.org)
+ * @author Trustin Lee (trustin@apache.org)
+ * @version $Rev$, $Date$
+ */
+class Event
+{
+    private final EventType type;
+    private final Object nextFilter;
+    private final Object data;
+    
+    public Event( EventType type, Object nextFilter, Object data )
+    {
+        this.type = type;
+        this.nextFilter = nextFilter;
+        this.data = data;
+    }
+
+    public Object getData()
+    {
+        return data;
+    }
+    
+
+    public Object getNextFilter()
+    {
+        return nextFilter;
+    }
+    
+
+    public EventType getType()
+    {
+        return type;
+    }
+}
\ No newline at end of file

Propchange: directory/network/trunk/src/java/org/apache/mina/util/Event.java
------------------------------------------------------------------------------
    svn:keywords = HeadURL Id LastChangedBy LastChangedDate LastChangedRevision



Mime
View raw message