You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mt...@apache.org on 2011/04/19 20:21:03 UTC

svn commit: r1095155 - in /commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform: unix/FcntlMutex.java unix/PosixMutex.java unix/SysVMutex.java windows/WindowsMutex.java windows/WindowsMutexImpl.java

Author: mturk
Date: Tue Apr 19 18:21:03 2011
New Revision: 1095155

URL: http://svn.apache.org/viewvc?rev=1095155&view=rev
Log:
Implement posix and fcntl mutexes

Modified:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/FcntlMutex.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/PosixMutex.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/SysVMutex.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsMutex.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsMutexImpl.java

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/FcntlMutex.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/FcntlMutex.java?rev=1095155&r1=1095154&r2=1095155&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/FcntlMutex.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/FcntlMutex.java Tue Apr 19 18:21:03 2011
@@ -36,15 +36,108 @@ final class FcntlMutex extends Mutex
     {
         // No Instance
     }
+    private static native int  close0(int fd);
+    private static native int  create0(String name)
+        throws IllegalAccessException,
+               IllegalArgumentException,
+               AlreadyExistsException,
+               SystemException;
+    private static native int  open0(String name)
+        throws IllegalAccessException,
+               IllegalArgumentException,
+               NoSuchObjectException,
+               SystemException;
+
+    private static native int  wait0(int fd);
+    private static native int  try0(int fd);
+    private static native int  release0(int fd);
+
+    // OS mutex descriptor
+    private int    fd;
 
     public FcntlMutex(final String name, boolean owner)
         throws IllegalAccessException,
                IllegalArgumentException,
                AlreadyExistsException,
+               NoSuchObjectException,
                UnsupportedOperationException,
                SystemException
     {
-        this.owner = true;
+        if (name == null)
+            throw new NullPointerException();
+        this.name = name;
+        if (owner)
+            fd = create0(this.name);
+        else
+            fd = open0(this.name);
+        this.owner = owner;
+    }
+
+    public void acquire()
+        throws SystemException
+    {
+        if (fd == -1)
+            throw new ClosedDescriptorException();
+        int rc = wait0(fd);
+        if (rc != 0)
+            throw new SystemException(Status.describe(rc));
+    }
+
+    public boolean tryAcquire()
+        throws SystemException
+    {
+        if (fd == -1)
+            throw new ClosedDescriptorException();
+        int rc = try0(fd);
+        if (rc == 0)
+            return true;
+        if (Status.IS_EBUSY(rc))
+            return false;
+        throw new SystemException(Status.describe(rc));
+    }
+
+    public void release()
+        throws SystemException
+    {
+        if (fd == -1)
+            throw new ClosedDescriptorException();
+        int rc = release0(fd);
+        if (rc != 0)
+            throw new SystemException(Status.describe(rc));
+    }
+
+    public void close()
+        throws SystemException
+    {
+        if (fd == -1)
+            throw new ClosedDescriptorException();
+        if (owner) {
+            // Unlink if we are the semaphore owner.
+            Posix.unlink(name);
+        }
+        int rc = Posix.close(fd);
+        if (rc != 0)
+            throw new SystemException(Status.describe(rc));
+        fd = -1;
+    }
+
+    /**
+     * Called by the garbage collector when the object is destroyed.
+     * The class will free internal resources allocated by the Operating system.
+     * @see Object#finalize()
+     * @throws Throwable the {@code Exception} raised by this method.
+     */
+    @Override
+    protected final void finalize()
+        throws Throwable
+    {
+        if (fd != -1) {
+            if (owner) {
+                // Unlink if we are the semaphore owner.
+                Posix.unlink(name);
+            }
+            Posix.close(fd);
+        }
     }
 
 }

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/PosixMutex.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/PosixMutex.java?rev=1095155&r1=1095154&r2=1095155&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/PosixMutex.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/PosixMutex.java Tue Apr 19 18:21:03 2011
@@ -16,8 +16,9 @@
 package org.apache.commons.runtime.platform.unix;
 
 import org.apache.commons.runtime.Mutex;
-import org.apache.commons.runtime.Status;
+import org.apache.commons.runtime.Semaphore;
 import org.apache.commons.runtime.AlreadyExistsException;
+import org.apache.commons.runtime.NoSuchObjectException;
 import org.apache.commons.runtime.SystemException;
 
 /**
@@ -35,14 +36,46 @@ final class PosixMutex extends Mutex
         // No Instance
     }
 
+    PosixSemaphore mutex;
+
     public PosixMutex(final String name, boolean owner)
         throws IllegalAccessException,
                IllegalArgumentException,
                AlreadyExistsException,
+               NoSuchObjectException,
                UnsupportedOperationException,
                SystemException
     {
-        this.owner = true;
+        if (owner)
+            mutex = new PosixSemaphore(name, 1);
+        else
+            mutex = new PosixSemaphore(name);
+        this.name  = mutex.getCanonicalName();
+        this.owner = owner;
+    }
+
+    public void acquire()
+        throws SystemException
+    {
+        mutex.acquire();
+    }
+
+    public boolean tryAcquire()
+        throws SystemException
+    {
+        return mutex.tryAcquire();
+    }
+
+    public void release()
+        throws SystemException
+    {
+        mutex.release();
     }
 
+    public void close()
+        throws SystemException
+    {
+        mutex.close();
+    }
+    
 }

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/SysVMutex.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/SysVMutex.java?rev=1095155&r1=1095154&r2=1095155&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/SysVMutex.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/SysVMutex.java Tue Apr 19 18:21:03 2011
@@ -109,14 +109,12 @@ final class SysVMutex extends Mutex
     public void close()
         throws SystemException
     {
-        int rc;
         if (fd == -1)
             throw new ClosedDescriptorException();
-        release0(fd);
         if (owner) {
             // Unlink if we are the semaphore owner.
             Posix.unlink(name);
-            rc = close0(fd);
+            int rc = close0(fd);
             if (rc != 0)
                 throw new SystemException(Status.describe(rc));
         }
@@ -134,7 +132,6 @@ final class SysVMutex extends Mutex
         throws Throwable
     {
         if (fd != -1) {
-            release0(fd);
             if (owner) {
                 // Unlink if we are the semaphore owner.
                 Posix.unlink(name);

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsMutex.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsMutex.java?rev=1095155&r1=1095154&r2=1095155&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsMutex.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsMutex.java Tue Apr 19 18:21:03 2011
@@ -50,33 +50,26 @@ final class WindowsMutex extends Mutex
                SystemException;
     private static native int  release0(long mtx);
 
-    public WindowsMutex(final String name, boolean create)
+    public WindowsMutex(final String name, boolean owner)
         throws IllegalAccessException,
                IllegalArgumentException,
                AlreadyExistsException,
-               SystemException
-    {
-        if (name == null)
-            throw new NullPointerException();
-        this.name = "Global\\" + name.replace('\\', '_');
-        long sa = Security.stdSecurityDescriptor(Win32.MUTEX_ALL_ACCESS   | Win32.GENERIC_RWX,
-                                                 Win32.MUTEX_ALL_ACCESS   | Win32.GENERIC_RWX,
-                                                 Win32.MUTEX_MODIFY_STATE | Win32.GENERIC_RWR);
-        handle = create0(this.name, sa);
-        owner = true;
-    }
-
-    public WindowsMutex(final String name)
-        throws IllegalAccessException,
-               IllegalArgumentException,
                NoSuchObjectException,
                SystemException
     {
         if (name == null)
             throw new NullPointerException();
         this.name = "Global\\" + name.replace('\\', '_');
-        handle = open0(this.name);
-        owner = false;
+        if (owner) {
+            long s = Security.stdSecurityDescriptor(Win32.MUTEX_ALL_ACCESS   | Win32.GENERIC_RWX,
+                                                    Win32.MUTEX_ALL_ACCESS   | Win32.GENERIC_RWX,
+                                                    Win32.MUTEX_MODIFY_STATE | Win32.GENERIC_RWR);
+            handle = create0(this.name, s);
+        }
+        else {
+            handle = open0(this.name);
+        }
+        this.owner = owner;
     }
 
     public void acquire()

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsMutexImpl.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsMutexImpl.java?rev=1095155&r1=1095154&r2=1095155&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsMutexImpl.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsMutexImpl.java Tue Apr 19 18:21:03 2011
@@ -60,7 +60,7 @@ final class WindowsMutexImpl extends Mut
     {
         switch (type) {
             case DEFAULT:
-                return new WindowsMutex(name);
+                return new WindowsMutex(name, false);
         }
         throw new UnsupportedOperationException();
     }