remm        2005/05/11 04:23:26

  Modified:    util/java/org/apache/tomcat/util/net AprEndpoint.java
               http11/src/java/org/apache/coyote/http11
                        Http11AprProcessor.java
  Log:
  - Should fix thread safety issue reported by Bill (needs testing).
  
  Revision  Changes    Path
  1.25      +63 -32    
jakarta-tomcat-connectors/util/java/org/apache/tomcat/util/net/AprEndpoint.java
  
  Index: AprEndpoint.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat-connectors/util/java/org/apache/tomcat/util/net/AprEndpoint.java,v
  retrieving revision 1.24
  retrieving revision 1.25
  diff -u -r1.24 -r1.25
  --- AprEndpoint.java  3 May 2005 09:36:58 -0000       1.24
  +++ AprEndpoint.java  11 May 2005 11:23:26 -0000      1.25
  @@ -17,6 +17,7 @@
   package org.apache.tomcat.util.net;
   
   import java.net.InetAddress;
  +import java.util.ArrayList;
   import java.util.HashMap;
   import java.util.Stack;
   
  @@ -715,6 +716,10 @@
           protected long pool = 0;
           protected long[] desc;
   
  +        protected long[] addS;
  +        protected long[] addP;
  +        protected int addCount = 0;
  +
           protected synchronized void init() {
               pool = Pool.create(serverSockPool);
               try {
  @@ -735,6 +740,8 @@
               }
               desc = new long[pollerSize * 4];
               keepAliveCount = 0;
  +            addS = new long[pollerSize];
  +            addP = new long[pollerSize];
           }
   
           protected void destroy() {
  @@ -742,23 +749,17 @@
           }
   
           public void add(long socket, long pool) {
  -            synchronized (this) {
  -                int rv = Poll.add(serverPollset, socket, pool, 
Poll.APR_POLLIN);
  -                if (rv == Status.APR_SUCCESS) {
  -                    keepAliveCount++;
  -                } else {
  +            synchronized (addS) {
  +                // Add socket to the list. Newly added sockets will wait 
  +                // at most for pollTime before being polled
  +                if (addCount >= addS.length) {
                       // Can't do anything: close the socket right away
                       Pool.destroy(pool);
  +                    return;
                   }
  -            }
  -        }
  -
  -        public void remove(long socket) {
  -            synchronized (this) {
  -                int rv = Poll.remove(serverPollset, socket);
  -                if (rv == Status.APR_SUCCESS) {
  -                    keepAliveCount--;
  -                }
  +                addS[addCount] = socket;
  +                addP[addCount] = pool;
  +                addCount++;
               }
           }
   
  @@ -780,7 +781,7 @@
                       }
                   }
   
  -                while (keepAliveCount < 1) {
  +                while (keepAliveCount < 1 && addCount < 1) {
                       try {
                           Thread.sleep(10);
                       } catch (InterruptedException e) {
  @@ -789,6 +790,22 @@
                   }
   
                   try {
  +                    // Add sockets which are waiting to the poller
  +                    if (addCount > 0) {
  +                        synchronized (addS) {
  +                            for (int i = 0; i < addCount; i++) {
  +                                int rv = Poll.add
  +                                    (serverPollset, addS[i], addP[i], 
Poll.APR_POLLIN);
  +                                if (rv == Status.APR_SUCCESS) {
  +                                    keepAliveCount++;
  +                                } else {
  +                                    // Can't do anything: close the socket 
right away
  +                                    Pool.destroy(pool);
  +                                }
  +                            }
  +                            addCount = 0;
  +                        }
  +                    }
                       // Pool for the specified interval
                       int rv = Poll.poll(serverPollset, pollTime, desc, true);
                       if (rv > 0) {
  @@ -971,7 +988,8 @@
           // Range information
           public long start;
           public long end;
  -        // Socket pool
  +        // Socket and socket pool
  +        public long socket;
           public long pool;
           // Position
           public long pos;
  @@ -990,6 +1008,9 @@
           protected long pool = 0;
           protected long[] desc;
           protected HashMap sendfileData;
  +
  +        protected ArrayList addS;
  +
           protected void init() {
               pool = Pool.create(serverSockPool);
               try {
  @@ -1010,6 +1031,7 @@
               }
               desc = new long[sendfileSize * 4];
               sendfileData = new HashMap(sendfileSize);
  +            addS = new ArrayList();
           }
   
           protected void destroy() {
  @@ -1017,7 +1039,7 @@
               Pool.destroy(pool);
           }
   
  -        public boolean add(long socket, SendfileData data) {
  +        public boolean add(SendfileData data) {
               // Initialize fd from data given
               try {
                   data.fdpool = Pool.create(data.pool);
  @@ -1027,9 +1049,9 @@
                        0, data.fdpool);
                   data.pos = data.start;
                   // Set the socket to nonblocking mode
  -                Socket.optSet(socket, Socket.APR_SO_NONBLOCK, 1);
  +                Socket.optSet(data.socket, Socket.APR_SO_NONBLOCK, 1);
                   while (true) {
  -                    long nw = Socket.sendfile(socket, data.fd, null, null,
  +                    long nw = Socket.sendfile(data.socket, data.fd, null, 
null,
                                                data.pos, data.end, 0);
                       if (nw < 0) {
                           if (!Status.APR_STATUS_IS_EAGAIN((int) -nw)) {
  @@ -1045,7 +1067,7 @@
                               // Entire file has been send
                               Poll.destroy(data.fdpool);
                               // Set back socket to blocking mode
  -                            Socket.optSet(socket, Socket.APR_SO_NONBLOCK, 0);
  +                            Socket.optSet(data.socket, 
Socket.APR_SO_NONBLOCK, 0);
                               return true;
                           }
                       }
  @@ -1054,17 +1076,10 @@
                   log.error(sm.getString("endpoint.sendfile.error"), e);
                   return false;
               }
  -            synchronized (this) {
  -                // Add socket to the poller
  -                sendfileData.put(new Long(socket), data);
  -                int rv = Poll.add(sendfilePollset, socket, 0, 
Poll.APR_POLLOUT);
  -                if (rv == Status.APR_SUCCESS) {
  -                    sendfileCount++;
  -                } else {
  -                    log.warn(sm.getString("endpoint.sendfile.addfail", "" + 
rv));
  -                    // Can't do anything: close the socket right away
  -                    Pool.destroy(data.pool);
  -                }
  +            // Add socket to the list. Newly added sockets will wait 
  +            // at most for pollTime before being polled
  +            synchronized (addS) {
  +                addS.add(sendfileData);
               }
               return false;
           }
  @@ -1099,7 +1114,7 @@
                       }
                   }
   
  -                while (sendfileCount < 1) {
  +                while (sendfileCount < 1 && addS.size() < 1) {
                       try {
                           Thread.sleep(10);
                       } catch (InterruptedException e) {
  @@ -1108,6 +1123,22 @@
                   }
   
                   try {
  +                    // Add socket to the poller
  +                    if (addS.size() > 0) {
  +                        for (int i = 0; i < addS.size(); i++) {
  +                            SendfileData data = (SendfileData) addS.get(i);
  +                            int rv = Poll.add(sendfilePollset, data.socket, 
0, Poll.APR_POLLOUT);
  +                            if (rv == Status.APR_SUCCESS) {
  +                                sendfileData.put(new Long(data.socket), 
data);
  +                                sendfileCount++;
  +                            } else {
  +                                
log.warn(sm.getString("endpoint.sendfile.addfail", "" + rv));
  +                                // Can't do anything: close the socket right 
away
  +                                Pool.destroy(data.pool);
  +                            }
  +                        }
  +                        addS.clear();
  +                    }
                       // Pool for the specified interval
                       int rv = Poll.poll(sendfilePollset, pollTime, desc, 
false);
                       if (rv > 0) {
  
  
  
  1.11      +2 -1      
jakarta-tomcat-connectors/http11/src/java/org/apache/coyote/http11/Http11AprProcessor.java
  
  Index: Http11AprProcessor.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat-connectors/http11/src/java/org/apache/coyote/http11/Http11AprProcessor.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- Http11AprProcessor.java   28 Apr 2005 12:30:38 -0000      1.10
  +++ Http11AprProcessor.java   11 May 2005 11:23:26 -0000      1.11
  @@ -872,8 +872,9 @@
   
               // Do sendfile as needed: add socket to sendfile and end
               if (sendfileData != null) {
  +                sendfileData.socket = socket;
                   sendfileData.pool = pool;
  -                if (!endpoint.getSendfile().add(socket, sendfileData)) {
  +                if (!endpoint.getSendfile().add(sendfileData)) {
                       keepAlive = false;
                   }
               }
  
  
  

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

Reply via email to