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/15 17:31:15 UTC

svn commit: r1092742 - in /commons/sandbox/runtime/trunk/src/main: java/org/apache/commons/runtime/platform/windows/ native/include/acr/ native/os/win32/ native/shared/

Author: mturk
Date: Fri Apr 15 15:31:15 2011
New Revision: 1092742

URL: http://svn.apache.org/viewvc?rev=1092742&view=rev
Log:
Implement windows semaphore

Modified:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/Win32.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsSemaphore.java
    commons/sandbox/runtime/trunk/src/main/native/include/acr/jnidefs.h
    commons/sandbox/runtime/trunk/src/main/native/os/win32/semaphore.c
    commons/sandbox/runtime/trunk/src/main/native/os/win32/winapi.c
    commons/sandbox/runtime/trunk/src/main/native/shared/error.c

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/Win32.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/Win32.java?rev=1092742&r1=1092741&r2=1092742&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/Win32.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/Win32.java Fri Apr 15 15:31:15 2011
@@ -54,7 +54,9 @@ final class Win32
         throws SystemException;
     public static native long       ConvertStringSecurityDescriptorToSecurityDescriptor(String securityDescriptor, int revision)
         throws SystemException;
+    public static native long       GetNullDacl()
+        throws OutOfMemoryError, SystemException;
     public static native long       GetSaWithNullDacl(boolean inherit)
-        throws SystemException;
+        throws OutOfMemoryError, SystemException;
     public static native int        WaitForSingleObject(long handle, int timeout);
 }

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsSemaphore.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsSemaphore.java?rev=1092742&r1=1092741&r2=1092742&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsSemaphore.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsSemaphore.java Fri Apr 15 15:31:15 2011
@@ -20,6 +20,7 @@ import org.apache.commons.runtime.Alread
 import org.apache.commons.runtime.NoSuchObjectException;
 import org.apache.commons.runtime.ClosedDescriptorException;
 import org.apache.commons.runtime.SystemException;
+import org.apache.commons.runtime.Status;
 
 /**
  * WindowsSemaphore class.
@@ -38,7 +39,19 @@ final class WindowsSemaphore extends Sem
 
     // OS semaphore handle
     private long    sd;
-    
+    private long    sa;
+
+    private static native long create0(String name, int value, long sd)
+        throws IllegalAccessException,
+               AlreadyExistsException,
+               SystemException;
+    private static native long open0(String name)
+        throws IllegalAccessException,
+               NoSuchObjectException,
+               SystemException;
+    private static native int  try0(long sema);
+    private static native int  release0(long sema);
+
     public WindowsSemaphore(final String name, int value)
         throws IllegalAccessException,
                IllegalArgumentException,
@@ -47,8 +60,9 @@ final class WindowsSemaphore extends Sem
     {
         if (name == null)
             throw new NullPointerException();
-        this.name = "\\\\Global\\" + name.replace('\\', '_');
-
+        this.name = "Global\\" + name.replace('\\', '_');
+        sa = Win32.GetNullDacl();
+        sd = create0(this.name, value, sa);
         owner = true;
     }
 
@@ -60,8 +74,8 @@ final class WindowsSemaphore extends Sem
     {
         if (name == null)
             throw new NullPointerException();
-        this.name = "\\\\Global\\" + name.replace('\\', '_');
-
+        this.name = "Global\\" + name.replace('\\', '_');
+        sd = open0(this.name);
         owner = false;
     }
 
@@ -70,6 +84,9 @@ final class WindowsSemaphore extends Sem
     {
         if (sd == 0L)
             throw new ClosedDescriptorException();
+        int rc = Win32.WaitForSingleObject(sd, Win32.INFINITE);
+        if (rc != 0)
+            throw new SystemException(Status.describe(rc));
     }
 
     public boolean tryAcquire()
@@ -77,7 +94,12 @@ final class WindowsSemaphore extends Sem
     {
         if (sd == 0L)
             throw new ClosedDescriptorException();
-        return false;
+        int rc = try0(sd);
+        if (rc == 0)
+            return true;
+        if (Status.IS_EBUSY(rc))
+            return false;
+        throw new SystemException(Status.describe(rc));
     }
 
     public void release()
@@ -85,11 +107,34 @@ final class WindowsSemaphore extends Sem
     {
         if (sd == 0L)
             throw new ClosedDescriptorException();
+        int rc = release0(sd);
+        if (rc != 0)
+            throw new SystemException(Status.describe(rc));
     }
 
     public void clear()
         throws SystemException
     {
+        boolean once = false;
+
+        if (sd == 0L)
+            throw new ClosedDescriptorException();
+        while (true) {
+            int rc = try0(sd);
+            if (rc == 0) {
+                once = true;
+            }
+            else {
+                if (once) {
+                    // We already obtained a lock once.
+                    break;
+                }
+                else {
+                    // Not an owner
+                    throw new SystemException(Status.describe(rc));
+                }
+            }
+        }
     }
 
     public void close()
@@ -97,9 +142,12 @@ final class WindowsSemaphore extends Sem
     {
         if (sd == 0L)
             throw new ClosedDescriptorException();
+        int rc = Win32.CloseHandle(sd);
+        if (rc != 0)
+            throw new SystemException(Status.describe(rc));
         sd = 0L;
     }
-    
+
     /**
      * Called by the garbage collector when the object is destroyed.
      * The class will free internal resources allocated by the Operating system.
@@ -110,6 +158,8 @@ final class WindowsSemaphore extends Sem
     protected final void finalize()
         throws Throwable
     {
+        if (sd != 0L)
+            Win32.CloseHandle(sd);
     }
 
 }

Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr/jnidefs.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr/jnidefs.h?rev=1092742&r1=1092741&r2=1092742&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr/jnidefs.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr/jnidefs.h Fri Apr 15 15:31:15 2011
@@ -25,7 +25,6 @@
 #include "acr/stddefs.h"
 
 #define ACR_CLASS_PATH          "org/apache/commons/runtime/"
-#define ACR_EXCEPTION_CP        ACR_CLASS_PATH "exception/"
 #define ACR_IO_CP               ACR_CLASS_PATH "io/"
 #define ACR_NET_CP              ACR_CLASS_PATH "net/"
 #define ACR_UTIL_CP             ACR_CLASS_PATH "util/"

Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/semaphore.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/semaphore.c?rev=1092742&r1=1092741&r2=1092742&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/semaphore.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/semaphore.c Fri Apr 15 15:31:15 2011
@@ -36,14 +36,14 @@ J_DECLARE_M_ID(0000) = {
 ACR_JNI_EXPORT(jobject, Semaphore, impl0)(JNI_STDARGS)
 {
     if (_clazzn.u == 1)
-        return (*env)->NewObject(env, _clazzn.i, J4MID(0000));    
+        return (*env)->NewObject(env, _clazzn.i, J4MID(0000));
     if (AcrLoadClass(env, &_clazzn, 0) != 0) {
         ACR_THROW_MSG(ACR_EX_EINSTANCE, "WindowsSemaphoreImpl not initialized");
         return 0;
     }
     R_LOAD_METHOD(0000, 0);
     _clazzn.u = 1;
-    return (*env)->NewObject(env, _clazzn.i, J4MID(0000));    
+    return (*env)->NewObject(env, _clazzn.i, J4MID(0000));
 }
 
 ACR_JNI_EXPORT(jboolean, Semaphore, unlink0)(JNI_STDARGS, jstring name)
@@ -125,7 +125,7 @@ ACR_WIN_EXPORT(jint, WindowsSemaphore, t
         return ACR_GET_OS_ERROR();
 }
 
-ACR_WIN_EXPORT(jint, PosixSemaphore, release0)(JNI_STDARGS, jlong sem)
+ACR_WIN_EXPORT(jint, WindowsSemaphore, release0)(JNI_STDARGS, jlong sem)
 {
     if (ReleaseSemaphore(J2P(sem, HANDLE), 1, 0))
         return 0;

Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/winapi.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/winapi.c?rev=1092742&r1=1092741&r2=1092742&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/winapi.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/winapi.c Fri Apr 15 15:31:15 2011
@@ -84,6 +84,15 @@ cleanup:
     return _null_SA[si];
 }
 
+ACR_WIN_EXPORT(jlong, Win32, GetNullDacl)(JNI_STDARGS)
+{
+    PSECURITY_ATTRIBUTES pSA = GetSaWithNullDacl(env, JNI_FALSE);
+    if (pSA != 0)
+        return P2J(pSA->lpSecurityDescriptor);
+    else
+        return 0;
+}
+
 ACR_WIN_EXPORT(jlong, Win32, GetSaWithNullDacl)(JNI_STDARGS, jboolean inherit)
 {
     return P2J(GetSaWithNullDacl(env, inherit));
@@ -160,5 +169,5 @@ ACR_WIN_EXPORT(jint, Win32, WaitForSingl
     else if (ws == WAIT_TIMEOUT)
         return ACR_EBUSY;
     else
-        return ACR_GET_OS_ERROR();    
+        return ACR_GET_OS_ERROR();
 }

Modified: commons/sandbox/runtime/trunk/src/main/native/shared/error.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/error.c?rev=1092742&r1=1092741&r2=1092742&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/error.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/error.c Fri Apr 15 15:31:15 2011
@@ -50,11 +50,11 @@ static struct {
     { 0, "java/io/FileNotFoundException"                        },
     { 0, "java/io/SyncFailedException"                          },
     { 0, "java/net/SocketException"                             },
-    { 0, ACR_EXCEPTION_CP "AlreadyExistsException"              },    
-    { 0, ACR_EXCEPTION_CP "NoSuchObjectException"               },
-    { 0, ACR_EXCEPTION_CP "SystemException"                     },
-    { 0, ACR_EXCEPTION_CP "TimeoutException"                    },
-    { 0, ACR_EXCEPTION_CP "UnsupportedOperatingSystemException" }
+    { 0, ACR_CLASS_PATH "AlreadyExistsException"                },
+    { 0, ACR_CLASS_PATH "NoSuchObjectException"                 },
+    { 0, ACR_CLASS_PATH "SystemException"                       },
+    { 0, ACR_CLASS_PATH "TimeoutException"                      },
+    { 0, ACR_CLASS_PATH "UnsupportedOperatingSystemException"   }
 };
 
 static const char *const _canon_errors[] = {