commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mt...@apache.org
Subject svn commit: r1129129 - in /commons/sandbox/runtime/trunk/src/main: java/org/apache/commons/runtime/net/ java/org/apache/commons/runtime/platform/unix/ native/os/unix/
Date Mon, 30 May 2011 11:25:36 GMT
Author: mturk
Date: Mon May 30 11:25:35 2011
New Revision: 1129129

URL: http://svn.apache.org/viewvc?rev=1129129&view=rev
Log:
Add autoCancel flag and closed selector exceptio

Added:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/ClosedSelectorException.java
  (with props)
Modified:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/IllegalSelectorException.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Selector.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/SocketSelectorImpl.java
    commons/sandbox/runtime/trunk/src/main/native/os/unix/poll.c

Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/ClosedSelectorException.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/ClosedSelectorException.java?rev=1129129&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/ClosedSelectorException.java
(added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/ClosedSelectorException.java
Mon May 30 11:25:35 2011
@@ -0,0 +1,39 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.commons.runtime.net;
+
+import java.io.IOException;
+
+/**
+ * An {@code ClosedSelectorException} is thrown when an operation is invoked
+ * on a closed selector.
+ *
+ * @since Runtime 1.0
+ */
+public class ClosedSelectorException extends IOException
+{
+
+    public ClosedSelectorException()
+    {
+        super();
+    }
+
+    public ClosedSelectorException(String msg)
+    {
+        super(msg);
+    }
+}

Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/ClosedSelectorException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/IllegalSelectorException.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/IllegalSelectorException.java?rev=1129129&r1=1129128&r2=1129129&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/IllegalSelectorException.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/IllegalSelectorException.java
Mon May 30 11:25:35 2011
@@ -17,8 +17,9 @@
 package org.apache.commons.runtime.net;
 
 /**
- * An {@code IllegalSelectorException} is thrown when an operation that
- * requires a specific selector is invoked on ainvalid selector.
+ * An {@code IllegalSelectorException} is thrown when an attempt is
+ * made to register a endpoint with a selector that was not used
+ * to create a selection key.
  *
  * @since Runtime 1.0
  */

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Selector.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Selector.java?rev=1129129&r1=1129128&r2=1129129&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Selector.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Selector.java
Mon May 30 11:25:35 2011
@@ -29,7 +29,12 @@ import org.apache.commons.runtime.Overfl
  */
 public abstract class Selector implements Closeable
 {
-    private int capacity;
+    private int         capacity;
+
+    /**
+     * Indicates the current auto-cancel mode for this {@code Selector} object.
+     */
+    protected boolean   autoCancel;
 
     private Selector()
     {
@@ -57,18 +62,18 @@ public abstract class Selector implement
      *
      * @return the current number of registered endpoints.
      *
-     * @throws IllegalSelectorException if this selector is closed.
+     * @throws ClosedSelectorException if this selector is closed.
      */
     public abstract int size()
-        throws IllegalSelectorException;
+        throws ClosedSelectorException;
 
     /**
      * Interrupt this pollset.
      *
-     * @throws IllegalSelectorException if this selector is closed.
+     * @throws ClosedSelectorException if this selector is closed.
      */
     public abstract void interrupt()
-        throws IllegalSelectorException;
+        throws ClosedSelectorException;
 
     /**
      * Registers the selection key with this selector.
@@ -77,28 +82,38 @@ public abstract class Selector implement
      *          it was already registered.
      *
      * @throws ClosedDescriptorException if the key's endpoint is closed.
-     * @throws IllegalSelectorException if this selector is closed.
+     * @throws ClosedSelectorException if this selector is closed.
+     * @throws IllegalSelectorException if the key was not created by the same
+     *          selector as this selector.
      * @throws OverflowException if the selector reached it's capacity.
      */
     protected abstract boolean register(SelectionKey key, int ops)
         throws ClosedDescriptorException,
                IllegalSelectorException,
+               ClosedSelectorException,
                OverflowException,
                IOException;
     /**
      * Unregister the selection key from this selector.
      *
-     * @throws IllegalSelectorException if this selector is closed.
+     * @throws ClosedSelectorException if this selector is closed.
+     * @throws IllegalSelectorException if the key was not created by the same
+     *          selector as this selector.
      */
     protected abstract void cancel(SelectionKey key)
-        throws IllegalSelectorException;
+        throws ClosedSelectorException,
+               IllegalSelectorException;
     
     /**
-     * Free the allocated resource by the Operating system.
+     * Closes this selector. Ongoing calls to the {@code select} methods of this
+     * selector will get interrupted. This interruption behaves as if the
+     * {@link #interrupt()} method of this selector is called. After this, all keys
+     * that are still valid are invalidated and their channels are unregistered.
+     * All resources held by this selector are released.
      * <p>
-     * Note that {@code Object.finalize()} method will call
-     * this function. However if the native code can block for
-     * long time explicit {@code close()} should be called.
+     * Any further attempt of using this selector after this method has been
+     * called (except calling {@link #close()} or {@link #interrupt()})
+     * results in a {@link ClosedSelectorException} being thrown.
      * </p>
      * @see java.io.Closeable#close()
      * @throws IOException if an I/O error occurs.
@@ -106,4 +121,16 @@ public abstract class Selector implement
     public abstract void close()
         throws IOException;
 
+    /**
+     * Sets this selector's auto-cancel mode to the given state.
+     * If the selector is in auto-cancel mode, then the keys are
+     * canceled when signaled.
+     *
+     * @param on {@code true} to enable auto-cancel mode; {@code false}
+     *           to disable it.
+     */
+    public void setAutoCancel(boolean on)
+    {
+        autoCancel = on;
+    }
 }

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/SocketSelectorImpl.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/SocketSelectorImpl.java?rev=1129129&r1=1129128&r2=1129129&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/SocketSelectorImpl.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/SocketSelectorImpl.java
Mon May 30 11:25:35 2011
@@ -21,6 +21,7 @@ import java.util.ArrayList;
 import java.util.HashSet;
 import org.apache.commons.runtime.io.ClosedDescriptorException;
 import org.apache.commons.runtime.net.Endpoint;
+import org.apache.commons.runtime.net.ClosedSelectorException;
 import org.apache.commons.runtime.net.IllegalSelectorException;
 import org.apache.commons.runtime.net.Poll;
 import org.apache.commons.runtime.net.SelectionKey;
@@ -52,13 +53,14 @@ final class SocketSelectorImpl extends S
     private static native long  create0(int size)
         throws OutOfMemoryError,
                SystemException;
-    private static native void  destroy0(long pollset);
     private static native void  wakeup0(long pollset);
+    private static native int   destroy0(long pollset);
     private static native int   size0(long pollset);
     private static native int   add0(long pollset, SelectionKeyImpl key, int fd, int events,
int ttl);
     private static native int   del0(long pollset, SelectionKeyImpl key, int fd);
     private static native int   clear0(long pollset, SelectionKeyImpl[] set);
-    private static native int   wait0(long pollset, SelectionKeyImpl[] set, short[] events,
int timeout, boolean remove);
+    private static native int   wait0(long pollset, SelectionKeyImpl[] set, short[] events,
+                                      int timeout, boolean autocancel);
 
     /*
      * Created from native
@@ -71,34 +73,44 @@ final class SocketSelectorImpl extends S
         keyset  = new SelectionKeyImpl[size];
     }
 
+    private void ensureValid()
+        throws ClosedSelectorException
+    {
+        if (pollset == 0L)
+            throw new ClosedSelectorException();
+    }
+
     @Override
     public int size()
-        throws IllegalSelectorException
+        throws ClosedSelectorException
     {
-        if (pollset == 0L)
-            throw new IllegalSelectorException();
+        ensureValid();
         return size0(pollset);
     }
 
     @Override
     public void interrupt()
-        throws IllegalSelectorException
+        throws ClosedSelectorException
     {
-        if (pollset == 0L)
-            throw new IllegalSelectorException();
+        ensureValid();
         wakeup0(pollset);
     }
 
     @Override
     public boolean register(SelectionKey key, int ops)
-        throws IllegalSelectorException, ClosedDescriptorException, OverflowException, IOException
+        throws ClosedSelectorException,
+               IllegalSelectorException,
+               ClosedDescriptorException,
+               OverflowException,
+               IOException
     {
+        ensureValid();
         SelectionKeyImpl skey = (SelectionKeyImpl)key;
+        if (skey.selector() != this)
+            throw new IllegalSelectorException();
         int fd  = skey.endpoint().descriptor().fd();
         if (fd == -1)
             throw new ClosedDescriptorException();
-        if (pollset == 0L)
-            throw new IllegalSelectorException();
         int rc = add0(pollset, skey, fd, ops, skey.timeout());
         if (rc != 0) {
             if (rc == Errno.EALREADY)
@@ -113,25 +125,32 @@ final class SocketSelectorImpl extends S
 
     @Override
     protected void cancel(SelectionKey key)
-        throws IllegalSelectorException
+        throws ClosedSelectorException,
+               IllegalSelectorException
     {
+        ensureValid();
+        if (key.selector() != this)
+            throw new IllegalSelectorException();
         SelectionKeyImpl skey = (SelectionKeyImpl)key;
         int fd  = skey.endpoint().descriptor().fd();
-        if (fd == -1)
-            return;
-        if (pollset == 0L)
-            throw new IllegalSelectorException();
-        del0(pollset, skey, fd);
+        if (fd != -1) {
+            // Remove selection key
+            del0(pollset, skey, fd);
+            skey.revents = 0;
+        }
     }
 
     @Override
     public void close()
         throws IOException
     {
-        if (pollset == 0L)
-            throw new ClosedDescriptorException();
-        destroy0(pollset);
-        pollset = 0L;
+        if (pollset != 0L) {
+            long ps = pollset;
+            pollset = 0L;
+            int rc  = destroy0(ps);
+            if (rc != 0)
+                throw new IOException(Status.describe(rc));
+        }
     }
 
 }

Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/poll.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/poll.c?rev=1129129&r1=1129128&r2=1129129&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/poll.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/poll.c Mon May 30 11:25:35 2011
@@ -215,9 +215,10 @@ cleanup:
     return 0;
 }
 
-ACR_UNX_EXPORT(void, SocketSelectorImpl, destroy0)(JNI_STDARGS, jlong pollset)
+ACR_UNX_EXPORT(int, SocketSelectorImpl, destroy0)(JNI_STDARGS, jlong pollset)
 {
     int i;
+    int rc = 0;
     acr_pollset_t *ps = J2P(pollset, acr_pollset_t *);
 
     pthread_mutex_lock(&ps->mutex);
@@ -232,10 +233,9 @@ ACR_UNX_EXPORT(void, SocketSelectorImpl,
          * Since we set the state to DESTROY
          * wait0 will return 0.
          */
-        if (pthread_cond_wait(&ps->wakeup, &ps->mutex) != 0) {
+        if ((rc = pthread_cond_wait(&ps->wakeup, &ps->mutex)) != 0) {
             pthread_mutex_unlock(&ps->mutex);
-            ACR_THROW(ACR_EX_EILLEGAL, 0);
-            return;
+            return rc;
         }
     }
     ps->state = PSS_DESTROY;
@@ -252,6 +252,7 @@ ACR_UNX_EXPORT(void, SocketSelectorImpl,
     AcrFree(ps->fdset);
     AcrFree(ps->ooset);
     AcrFree(ps);
+    return rc;
 }
 
 ACR_UNX_EXPORT(jint, SocketSelectorImpl, clear0)(JNI_STDARGS, jlong pollset,
@@ -319,7 +320,7 @@ ACR_UNX_EXPORT(jint, SocketSelectorImpl,
 
 ACR_UNX_EXPORT(jint, SocketSelectorImpl, wait0)(JNI_STDARGS, jlong pollset,
                                                 jobjectArray rs, jshortArray revents,
-                                                jint timeout, jboolean rmsignaled)
+                                                jint timeout, jboolean autocancel)
 {
     int i, ns, rc = 0;
     int rv = 0;
@@ -417,9 +418,8 @@ ACR_UNX_EXPORT(jint, SocketSelectorImpl,
             else {
                 pevents[rv] = reventt(ps->fdset[i].revents);
                 (*env)->SetObjectArrayElement(env, rs, rv++, ps->ooset[i].obj);
-                if (rmsignaled == JNI_FALSE && ps->ooset[i].ttl > 0) {
-                    /* Reset TTL if we are not going to remove
-                     * the descriptor from this pollset
+                if (ps->ooset[i].ttl > 0) {
+                    /* Reset TTL
                      */
                     if (now == 0)
                         now = AcrTimeNow();
@@ -443,7 +443,7 @@ ACR_UNX_EXPORT(jint, SocketSelectorImpl,
         }
     }
     RELEASE_CRITICAL(revents, pevents);
-    if (rmsignaled == JNI_TRUE && rv > 0) {
+    if (autocancel == JNI_TRUE && rv > 0) {
         /* Remove all descriptors with revents set except
          * the wakeup pipe at index zero.
          */



Mime
View raw message