You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by te...@apache.org on 2007/08/24 17:14:23 UTC

svn commit: r569416 - /harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/SelectorImpl.java

Author: tellison
Date: Fri Aug 24 08:14:23 2007
New Revision: 569416

URL: http://svn.apache.org/viewvc?rev=569416&view=rev
Log:
Closing the selector should acquire appropriate locks first.
Tidy up some code to help diagnose Jetty failure.

Modified:
    harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/SelectorImpl.java

Modified: harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/SelectorImpl.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/SelectorImpl.java?rev=569416&r1=569415&r2=569416&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/SelectorImpl.java (original)
+++ harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/SelectorImpl.java Fri Aug 24 08:14:23 2007
@@ -85,10 +85,6 @@
 
     private SelectionKey[] writableChannels;
 
-    private List<FileDescriptor> readableFDs = new ArrayList<FileDescriptor>();
-
-    private List<FileDescriptor> writableFDs = new ArrayList<FileDescriptor>();
-
     private FileDescriptor[] readable;
 
     private FileDescriptor[] writable;
@@ -110,11 +106,17 @@
      * @see java.nio.channels.spi.AbstractSelector#implCloseSelector()
      */
     protected void implCloseSelector() throws IOException {
-        doCancel();
-        for (SelectionKey sk : keys) {
-            deregister((AbstractSelectionKey) sk);
+        synchronized (this) {
+            synchronized (keys) {
+                synchronized (selectedKeys) {
+                    doCancel();
+                    for (SelectionKey sk : keys) {
+                        deregister((AbstractSelectionKey) sk);
+                    }
+                    wakeup();
+                }
+            }
         }
-        wakeup();
     }
 
     /*
@@ -189,9 +191,6 @@
                         }
                         readyChannels = Platform.getNetworkSystem().select(readable, writable, timeout);
                     } finally {
-                        // clear results for next select
-                        readableFDs.clear();
-                        writableFDs.clear();                        
                         if (isBlock) {
                             end();
                         }
@@ -212,13 +211,20 @@
 
     // Prepares and adds channels to list for selection
     private void prepareChannels() {
-        readableFDs.add(sourcefd);        
-        List<SelectionKey> readChannelList = new ArrayList<SelectionKey>();
+        int sizeGuess = keys.size();
+        List<SelectionKey> readChannelList = new ArrayList<SelectionKey>(sizeGuess + 1);
+        List<FileDescriptor> readableFDs = new ArrayList<FileDescriptor>(sizeGuess + 1);
+
+        List<SelectionKey> writeChannelList = new ArrayList<SelectionKey>(sizeGuess);
+        List<FileDescriptor> writableFDs = new ArrayList<FileDescriptor>(sizeGuess);
+
+        // Always add in the "wake-up" channel 
         readChannelList.add(source.keyFor(this));
-        List<SelectionKey> writeChannelList = new ArrayList<SelectionKey>();
+        readableFDs.add(sourcefd);
+
         synchronized (keysLock) {
-            for (Iterator<SelectionKey> i = keys.iterator(); i.hasNext();) {
-                SelectionKeyImpl key = (SelectionKeyImpl) i.next();
+            for (SelectionKey skey : keys) {
+                SelectionKeyImpl key = (SelectionKeyImpl) skey;
                 key.oldInterestOps = key.interestOps();
                 boolean isReadableChannel = ((SelectionKey.OP_ACCEPT | SelectionKey.OP_READ) & key.oldInterestOps) != 0;
                 boolean isWritableChannel = ((SelectionKey.OP_CONNECT | SelectionKey.OP_WRITE) & key.oldInterestOps) != 0;
@@ -233,14 +239,18 @@
                 }
             }
         }
-        readableChannels = readChannelList.toArray(new SelectionKey[0]);
-        writableChannels = writeChannelList.toArray(new SelectionKey[0]);
-        readable = readableFDs.toArray(new FileDescriptor[0]);
-        writable = writableFDs.toArray(new FileDescriptor[0]);
+        readableChannels = readChannelList.toArray(new SelectionKey[readChannelList.size()]);
+        writableChannels = writeChannelList.toArray(new SelectionKey[writeChannelList.size()]);
+        readable = readableFDs.toArray(new FileDescriptor[readableFDs.size()]);
+        writable = writableFDs.toArray(new FileDescriptor[writableFDs.size()]);
     }
 
-    // Analyses selected channels and adds keys of ready channels to
-    // selectedKeys list
+    /* Analyses selected channels and adds keys of ready channels to
+     * selectedKeys list.
+     * 
+     * readyChannels are encoded as concatenated array of flags for
+     * readable channels followed by writable channels. 
+     */
     private int processSelectResult(int[] readyChannels) throws IOException {
         if (0 == readyChannels.length) {
             return 0;