You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by re...@apache.org on 2019/12/10 17:21:01 UTC

[tomcat] branch master updated: Simple improvement to avoid using unneeded objects

This is an automated email from the ASF dual-hosted git repository.

remm pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/master by this push:
     new f2af194  Simple improvement to avoid using unneeded objects
f2af194 is described below

commit f2af19479b1373350952ed501a4c51bf90939364
Author: remm <re...@apache.org>
AuthorDate: Tue Dec 10 18:20:44 2019 +0100

    Simple improvement to avoid using unneeded objects
---
 java/org/apache/tomcat/util/net/NioEndpoint.java | 61 +++++++++++-------------
 1 file changed, 29 insertions(+), 32 deletions(-)

diff --git a/java/org/apache/tomcat/util/net/NioEndpoint.java b/java/org/apache/tomcat/util/net/NioEndpoint.java
index 7d4104a..1352ef5 100644
--- a/java/org/apache/tomcat/util/net/NioEndpoint.java
+++ b/java/org/apache/tomcat/util/net/NioEndpoint.java
@@ -40,7 +40,6 @@ import java.util.Iterator;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.Semaphore;
 import java.util.concurrent.TimeUnit;
-import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicLong;
 
 import javax.net.ssl.SSLEngine;
@@ -136,7 +135,7 @@ public class NioEndpoint extends AbstractJsseEndpoint<NioChannel,SocketChannel>
     public boolean getUseInheritedChannel() { return useInheritedChannel; }
 
     /**
-     * Priority of the poller threads.
+     * Priority of the poller thread.
      */
     private int pollerThreadPriority = Thread.NORM_PRIORITY;
     public void setPollerThreadPriority(int pollerThreadPriority) { this.pollerThreadPriority = pollerThreadPriority; }
@@ -774,11 +773,10 @@ public class NioEndpoint extends AbstractJsseEndpoint<NioChannel,SocketChannel>
                                     if (!socketWrapper.readOperation.process()) {
                                         closeSocket = true;
                                     }
-                                } else if (socketWrapper.blockReadDone != null) {
-                                    if (socketWrapper.blockReadDone.compareAndSet(false, true)) {
-                                        synchronized (socketWrapper.blockReadDone) {
-                                            socketWrapper.blockReadDone.notify();
-                                        }
+                                } else if (socketWrapper.readBlocking) {
+                                    synchronized (socketWrapper.readLock) {
+                                        socketWrapper.readBlocking = false;
+                                        socketWrapper.readLock.notify();
                                     }
                                 } else if (!processSocket(socketWrapper, SocketEvent.OPEN_READ, true)) {
                                     closeSocket = true;
@@ -789,11 +787,10 @@ public class NioEndpoint extends AbstractJsseEndpoint<NioChannel,SocketChannel>
                                     if (!socketWrapper.writeOperation.process()) {
                                         closeSocket = true;
                                     }
-                                } else if (socketWrapper.blockWriteDone != null) {
-                                    if (socketWrapper.blockWriteDone.compareAndSet(false, true)) {
-                                        synchronized (socketWrapper.blockWriteDone) {
-                                            socketWrapper.blockWriteDone.notify();
-                                        }
+                                } else if (socketWrapper.writeBlocking) {
+                                    synchronized (socketWrapper.writeLock) {
+                                        socketWrapper.writeBlocking = false;
+                                        socketWrapper.writeLock.notify();
                                     }
                                 } else if (!processSocket(socketWrapper, SocketEvent.OPEN_WRITE, true)) {
                                     closeSocket = true;
@@ -1038,8 +1035,10 @@ public class NioEndpoint extends AbstractJsseEndpoint<NioChannel,SocketChannel>
         private volatile long lastRead = System.currentTimeMillis();
         private volatile long lastWrite = lastRead;
 
-        private AtomicBoolean blockReadDone = null;
-        private AtomicBoolean blockWriteDone = null;
+        private final Object readLock;
+        private volatile boolean readBlocking = false;
+        private final Object writeLock;
+        private volatile boolean writeBlocking = false;
 
         public NioSocketWrapper(NioChannel channel, NioEndpoint endpoint) {
             super(channel, endpoint);
@@ -1047,6 +1046,8 @@ public class NioEndpoint extends AbstractJsseEndpoint<NioChannel,SocketChannel>
             nioChannels = endpoint.getNioChannels();
             poller = endpoint.getPoller();
             socketBufferHandler = channel.getBufHandler();
+            readLock = (readPending == null) ? new Object() : readPending;
+            writeLock = (writePending == null) ? new Object() : writePending;
         }
 
         public Poller getPoller() { return poller; }
@@ -1238,20 +1239,20 @@ public class NioEndpoint extends AbstractJsseEndpoint<NioChannel,SocketChannel>
             if (block && nRead == 0) {
                 long timeout = getReadTimeout();
                 try {
-                    blockReadDone = new AtomicBoolean(false);
+                    readBlocking = true;
                     registerReadInterest();
-                    synchronized (blockReadDone) {
-                        if (!blockReadDone.get()) {
+                    synchronized (readLock) {
+                        if (readBlocking) {
                             try {
                                 if (timeout > 0) {
-                                    blockReadDone.wait(timeout);
+                                    readLock.wait(timeout);
                                 } else {
-                                    blockReadDone.wait();
+                                    readLock.wait();
                                 }
                             } catch (InterruptedException e) {
                                 // Continue ...
                             }
-                            if (!blockReadDone.get()) {
+                            if (readBlocking) {
                                 throw new SocketTimeoutException();
                             }
                         }
@@ -1261,7 +1262,7 @@ public class NioEndpoint extends AbstractJsseEndpoint<NioChannel,SocketChannel>
                         throw new EOFException();
                     }
                 } finally {
-                    blockReadDone = null;
+                    readBlocking = false;
                 }
             }
             return nRead;
@@ -1284,24 +1285,20 @@ public class NioEndpoint extends AbstractJsseEndpoint<NioChannel,SocketChannel>
                             throw new EOFException();
                         }
                         if (n == 0) {
-                            if (blockWriteDone == null) {
-                                blockWriteDone = new AtomicBoolean(false);
-                            } else {
-                                blockWriteDone.set(false);
-                            }
+                            writeBlocking = true;
                             registerWriteInterest();
-                            synchronized (blockWriteDone) {
-                                if (!blockWriteDone.get()) {
+                            synchronized (writeLock) {
+                                if (writeBlocking) {
                                     try {
                                         if (timeout > 0) {
-                                            blockWriteDone.wait(timeout);
+                                            writeLock.wait(timeout);
                                         } else {
-                                            blockWriteDone.wait();
+                                            writeLock.wait();
                                         }
                                     } catch (InterruptedException e) {
                                         // Continue ...
                                     }
-                                    if (!blockWriteDone.get()) {
+                                    if (writeBlocking) {
                                         throw new SocketTimeoutException();
                                     }
                                 }
@@ -1309,7 +1306,7 @@ public class NioEndpoint extends AbstractJsseEndpoint<NioChannel,SocketChannel>
                         }
                     } while (from.hasRemaining());
                 } finally {
-                    blockWriteDone = null;
+                    writeBlocking = false;
                 }
                 // If there is data left in the buffer the socket will be registered for
                 // write further up the stack. This is to ensure the socket is only


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org