commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mt...@apache.org
Subject svn commit: r1129997 - in /commons/sandbox/runtime/trunk/src/main: java/org/apache/commons/runtime/io/ java/org/apache/commons/runtime/net/ native/include/acr/ native/os/unix/
Date Wed, 01 Jun 2011 04:48:29 GMT
Author: mturk
Date: Wed Jun  1 04:48:29 2011
New Revision: 1129997

URL: http://svn.apache.org/viewvc?rev=1129997&view=rev
Log:
Use separate object instead this for locking

Modified:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Descriptor.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalSocketEndpoint.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SelectionKey.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SelectionKeyImpl.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SelectionOps.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketEndpoint.java
    commons/sandbox/runtime/trunk/src/main/native/include/acr/iodefs.h
    commons/sandbox/runtime/trunk/src/main/native/os/unix/selector.c

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Descriptor.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Descriptor.java?rev=1129997&r1=1129996&r2=1129997&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Descriptor.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Descriptor.java
Wed Jun  1 04:48:29 2011
@@ -124,7 +124,7 @@ public abstract class Descriptor impleme
     }
 
     /**
-     * Check if the underlying Operating system descriptor is valid.
+     * Check if the underlying Operating system descriptor is closed.
      *
      * @return {@code true} if descriptor is closed; {@code false} otherwise.
      */

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalSocketEndpoint.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalSocketEndpoint.java?rev=1129997&r1=1129996&r2=1129997&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalSocketEndpoint.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalSocketEndpoint.java
Wed Jun  1 04:48:29 2011
@@ -40,6 +40,8 @@ public class LocalSocketEndpoint extends
     private LocalSocketDescriptor  sd;
     private SelectionKeyImpl       key;
     private boolean                blocking = false;
+    // Lock for registration and configureBlocking operations
+    private final Object           synclock = new Object();
 
     private static native int nonblock0(int fd, boolean block);
 
@@ -58,6 +60,8 @@ public class LocalSocketEndpoint extends
     public LocalSocketEndpoint(LocalSocketDescriptor sd)
     {
         super(EndpointType.LOCAL);
+        if (sd == null)
+            throw new NullPointerException();
         this.sd = sd;
     }
 
@@ -68,35 +72,18 @@ public class LocalSocketEndpoint extends
     }
 
     @Override
-    public Descriptor configureBlocking(boolean block)
-        throws ClosedDescriptorException,
-               IllegalBlockingModeException,
-               IOException
-    {
-        if (sd.closed())
-            throw new ClosedDescriptorException();
-        if (isRegistered())
-            throw new IllegalBlockingModeException();
-        if (blocking == block)
-            return sd;
-        int rc = nonblock0(sd.fd(), block);
-        if (rc != 0)
-            throw new SocketException(Status.describe(rc));
-        blocking = block;
-        return sd;
-    }
-
-    @Override
     public boolean isBlocking()
         throws IOException
     {
-        return blocking;
+        synchronized (synclock) {
+            return blocking;
+        }
     }
 
     @Override
     public boolean isRegistered()
     {
-        synchronized(this) {
+        synchronized (synclock) {
             if (key != null && key.selected)
                 return true;
             else
@@ -105,10 +92,32 @@ public class LocalSocketEndpoint extends
     }
 
     @Override
+    public Descriptor configureBlocking(boolean block)
+        throws ClosedDescriptorException,
+               IllegalBlockingModeException,
+               IOException
+    {
+        if (sd.closed())
+            throw new ClosedDescriptorException();
+        synchronized (synclock) {
+            if (key != null && key.selected)
+                throw new IllegalBlockingModeException();
+            if (blocking == block)
+                return sd;
+            int rc = nonblock0(sd.fd(), block);
+            if (rc != 0)
+                throw new SocketException(Status.describe(rc));
+            blocking = block;
+        }
+        return sd;
+    }
+
+
+    @Override
     public void close()
         throws IOException
     {
-        synchronized(this) {
+        synchronized (synclock) {
             if (sd.valid()) {
                 if (key != null && key.selected) {
                     try {
@@ -129,7 +138,7 @@ public class LocalSocketEndpoint extends
     @Override
     public SelectionKey keyFor(Selector selector)
     {
-        synchronized(this) {
+        synchronized (synclock) {
             if (key != null && key.selector == selector)
                 return key;
             else
@@ -145,20 +154,23 @@ public class LocalSocketEndpoint extends
                OverflowException,
                IOException
     {
+        ops = ops & 0x000f;
+        if (sd.closed())
+            throw new ClosedDescriptorException();
         AbstractSelector sel = (AbstractSelector)selector;
-        synchronized(this) {
-            if (sd.closed())
-                throw new ClosedDescriptorException();
+        synchronized (synclock) {
             if (key == null)
-                key = new SelectionKeyImpl(sel, this, ops);
+                key = new SelectionKeyImpl(sel, this);
             if (key.selector != sel)
                 throw new IllegalSelectorException();
             if (att != null)
                 key.attach(att);
             if (key.selected)
                 return key;
+            if (ops != 0)
+                key.ievents = ops;
+            return sel.queue(key, sd);
         }
-        return sel.queue(key, sd);
     }
 
 }

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SelectionKey.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SelectionKey.java?rev=1129997&r1=1129996&r2=1129997&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SelectionKey.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SelectionKey.java
Wed Jun  1 04:48:29 2011
@@ -48,8 +48,8 @@ public abstract class SelectionKey
      * <p>
      * </p>
      */
-    public static final int OP_WRITE    = 0x0010;
-    
+    public static final int OP_WRITE    = 0x0008;
+
     /**
      * Constructs a new {@code SelectionKey} for given selector and enpoint
      */

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SelectionKeyImpl.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SelectionKeyImpl.java?rev=1129997&r1=1129996&r2=1129997&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SelectionKeyImpl.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SelectionKeyImpl.java
Wed Jun  1 04:48:29 2011
@@ -45,21 +45,15 @@ final class SelectionKeyImpl extends Sel
         init0();
     }
 
-    public SelectionKeyImpl(AbstractSelector selector, Endpoint endpoint,
-                            int ievents)
+    public SelectionKeyImpl(AbstractSelector selector, Endpoint endpoint)
     {
         this.selector = selector;
         this.endpoint = endpoint;
-        this.ievents  = ievents;
+        this.ievents  = 0;
         this.revents  = 0;
         this.selected = false;
     }
 
-    public SelectionKeyImpl(AbstractSelector selector, Endpoint endpoint)
-    {
-        this(selector, endpoint, 0);
-    }
-
     @Override
     public Endpoint endpoint()
     {
@@ -69,7 +63,9 @@ final class SelectionKeyImpl extends Sel
     @Override
     public int events()
     {
-        return revents;
+        // Mask out the revents with the ievents.
+        //
+        return (revents & 0x0ff0) | (revents & ievents);
     }
 
     @Override
@@ -83,6 +79,8 @@ final class SelectionKeyImpl extends Sel
     public void cancel()
         throws ClosedSelectorException
     {
+        if (selector == null)
+            throw new ClosedSelectorException();
         selector.cancel(this);
     }
 

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SelectionOps.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SelectionOps.java?rev=1129997&r1=1129996&r2=1129997&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SelectionOps.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SelectionOps.java
Wed Jun  1 04:48:29 2011
@@ -42,16 +42,16 @@ final class SelectionOps
      * </p>
      */
     public static final int READ        = 0x0004;
-    /** There is urgent data to read.
+    /** Write operation.
      * <p>
      * </p>
      */
-    public static final int PRI         = 0x0008;
-    /** Write operation.
+    public static final int WRITE       = 0x0008;
+    /** There is urgent data to read.
      * <p>
      * </p>
      */
-    public static final int WRITE       = 0x0010;
+    public static final int PRI         = 0x0010;
     /** Read hang up operation.
      * <p>
      * If present in output it signals that

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketEndpoint.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketEndpoint.java?rev=1129997&r1=1129996&r2=1129997&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketEndpoint.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketEndpoint.java
Wed Jun  1 04:48:29 2011
@@ -36,6 +36,8 @@ public class SocketEndpoint extends Endp
     private SocketDescriptor  sd;
     private SelectionKeyImpl  key;
     private boolean           blocking = false;
+    // Lock for registration and configureBlocking operations
+    private final Object      synclock = new Object();
 
     private static native int nonblock0(int fd, boolean block);
     
@@ -54,6 +56,8 @@ public class SocketEndpoint extends Endp
     public SocketEndpoint(SocketDescriptor sd)
     {
         super(EndpointType.SOCKET);
+        if (sd == null)
+            throw new NullPointerException();
         this.sd = sd;
     }
 
@@ -66,7 +70,7 @@ public class SocketEndpoint extends Endp
     @Override
     public boolean isRegistered()
     {
-        synchronized(this) {
+        synchronized (synclock) {
             if (key != null && key.selected)
                 return true;
             else
@@ -82,14 +86,16 @@ public class SocketEndpoint extends Endp
     {
         if (sd.closed())
             throw new ClosedDescriptorException();
-        if (isRegistered())
-            throw new IllegalBlockingModeException();
-        if (blocking == block)
-            return sd;
-        int rc = nonblock0(sd.fd(), block);
-        if (rc != 0)
-            throw new SocketException(Status.describe(rc));
-        blocking = block;
+        synchronized (synclock) {
+            if (key != null && key.selected)
+                throw new IllegalBlockingModeException();
+            if (blocking == block)
+                return sd;
+            int rc = nonblock0(sd.fd(), block);
+            if (rc != 0)
+                throw new SocketException(Status.describe(rc));
+            blocking = block;
+        }
         return sd;
     }
 
@@ -97,14 +103,16 @@ public class SocketEndpoint extends Endp
     public boolean isBlocking()
         throws IOException
     {
-        return blocking;
+        synchronized (synclock) {
+            return blocking;
+        }
     }
 
     @Override
     public void close()
         throws IOException
     {
-        synchronized(this) {
+        synchronized (synclock) {
             if (sd.valid()) {
                 if (key != null && key.selected) {
                     try {
@@ -125,7 +133,7 @@ public class SocketEndpoint extends Endp
     @Override
     public SelectionKey keyFor(Selector selector)
     {
-        synchronized(this) {
+        synchronized (synclock) {
             if (key != null && key.selector == selector)
                 return key;
             else
@@ -141,20 +149,23 @@ public class SocketEndpoint extends Endp
                OverflowException,
                IOException
     {
+        ops = ops & 0x000f;
+        if (sd.closed())
+            throw new ClosedDescriptorException();
         AbstractSelector sel = (AbstractSelector)selector;
-        synchronized(this) {
-            if (sd.closed())
-                throw new ClosedDescriptorException();
+        synchronized (synclock) {
             if (key == null)
-                key = new SelectionKeyImpl(sel, this, ops);
+                key = new SelectionKeyImpl(sel, this);
             if (key.selector != sel)
                 throw new IllegalSelectorException();
             if (att != null)
                 key.attach(att);
             if (key.selected)
                 return key;
+            if (ops != 0)
+                key.ievents = ops;
+            return sel.queue(key, sd);
         }
-        return sel.queue(key, sd);
     }
 
 }

Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr/iodefs.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr/iodefs.h?rev=1129997&r1=1129996&r2=1129997&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr/iodefs.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr/iodefs.h Wed Jun  1 04:48:29
2011
@@ -38,9 +38,9 @@
 #define ACR_OP_ACCEPT        0x0001
 #define ACR_OP_CONNECT       0x0002
 #define ACR_OP_READ          0x0004
-#define ACR_OP_INP           0x0007
-#define ACR_OP_PRI           0x0008
-#define ACR_OP_WRITE         0x0010
+#define ACR_OP_INP           0x0007    /**< ACCEPT|CONNECT|READ */
+#define ACR_OP_WRITE         0x0008
+#define ACR_OP_PRI           0x0010
 #define ACR_OP_RDHUP         0x0020
 #define ACR_OP_HANGUP        0x0040
 #define ACR_OP_ERROR         0x0100

Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/selector.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/selector.c?rev=1129997&r1=1129996&r2=1129997&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/selector.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/selector.c Wed Jun  1 04:48:29 2011
@@ -56,10 +56,10 @@ static short ieventt(int event)
 
     if (event & ACR_OP_INP)
         rv |= POLLIN;
-    if (event & ACR_OP_PRI)
-        rv |= POLLPRI;
     if (event & ACR_OP_WRITE)
         rv |= POLLOUT;
+    if (event & ACR_OP_PRI)
+        rv |= POLLPRI;
     /* POLLERR, POLLHUP, and POLLNVAL aren't valid as requested events
      */
     return rv;
@@ -71,10 +71,10 @@ static short reventt(short event)
 
     if (event & POLLIN)
         rv |= ACR_OP_INP;
-    if (event & POLLPRI)
-        rv |= ACR_OP_PRI;
     if (event & POLLOUT)
         rv |= ACR_OP_WRITE;
+    if (event & POLLPRI)
+        rv |= ACR_OP_PRI;
     if (event & POLLERR)
         rv |= ACR_OP_ERROR;
     if (event & POLLHUP)



Mime
View raw message