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 10:06:09 UTC

svn commit: r1092621 - in /commons/sandbox/runtime/trunk: ./ src/main/java/org/apache/commons/runtime/ src/main/native/os/unix/ src/main/native/os/win32/ src/main/test/org/apache/commons/runtime/

Author: mturk
Date: Fri Apr 15 08:06:09 2011
New Revision: 1092621

URL: http://svn.apache.org/viewvc?rev=1092621&view=rev
Log:
Add static Semaphore.remove. Usable for stalled semaphores

Modified:
    commons/sandbox/runtime/trunk/build.xml
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Semaphore.java
    commons/sandbox/runtime/trunk/src/main/native/os/unix/semaphore.c
    commons/sandbox/runtime/trunk/src/main/native/os/win32/semaphore.c
    commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestSemaphore.java

Modified: commons/sandbox/runtime/trunk/build.xml
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/build.xml?rev=1092621&r1=1092620&r2=1092621&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/build.xml (original)
+++ commons/sandbox/runtime/trunk/build.xml Fri Apr 15 08:06:09 2011
@@ -403,7 +403,7 @@ The Apache Software Foundation (http://w
                 <runtest groups="init,semaphore.parent" name="Semaphore Parent"/>
             </sequential>
             <sequential>
-                <sleep seconds="1" />
+                <sleep milliseconds="100" />
                 <runtest groups="init,semaphore.child" name="Semaphore Child"/>
             </sequential>
         </parallel>            

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Semaphore.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Semaphore.java?rev=1092621&r1=1092620&r2=1092621&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Semaphore.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Semaphore.java Fri Apr 15 08:06:09 2011
@@ -48,6 +48,22 @@ public abstract class Semaphore
         return impl;
     }
 
+    private static native boolean unlink0(String name);
+    /**
+     * Removes the named semaphore.
+     * The semaphore is destroyed once all other processes that
+     * have the semaphore open close it
+     *
+     * @param name Semaphore to destroy.
+     */
+    public static boolean remove(String name)
+        throws NullPointerException
+    {
+        if (name == null)
+            throw new NullPointerException();
+        return unlink0(name);
+    }
+
     /**
      * Acquires a permit from this semaphore, blocking until
      * one is available.

Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/semaphore.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/semaphore.c?rev=1092621&r1=1092620&r2=1092621&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/semaphore.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/semaphore.c Fri Apr 15 08:06:09 2011
@@ -69,6 +69,24 @@ ACR_JNI_EXPORT(jobject, Semaphore, getIm
     return (*env)->NewObject(env, _clazzn.i, J4MID(0000));
 }
 
+ACR_JNI_EXPORT(jboolean, Semaphore, unlink0)(JNI_STDARGS, jstring name)
+{
+    jboolean rc = JNI_FALSE;
+    WITH_CSTR(name) {
+        char *p;
+        char buf[PATH_MAX] = "/";
+        
+        strlcat(buf, J2S(name), PATH_MAX);
+        for (p = &buf[1]; *p != '\0'; p++) {
+            if (*p == '/')
+                *p = '_';
+        }
+        if (sem_unlink(buf) == 0)
+            rc = JNI_TRUE;
+    } DONE_WITH_STR(name);
+    return rc;
+}
+
 ACR_JNI_EXPORT(jint, PosixSemaphore, unlink0)(JNI_STDARGS, jstring name)
 {
     int rc = 0;

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=1092621&r1=1092620&r2=1092621&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 08:06:09 2011
@@ -53,6 +53,13 @@ ACR_JNI_EXPORT(jobject, Semaphore, getIm
     return (*env)->NewObject(env, _clazzn.i, J4MID(0000));
 }
 
+ACR_JNI_EXPORT(jboolean, Semaphore, unlink0)(JNI_STDARGS, jstring name)
+{
+    /* Windows semaphore is not kernel bound.
+     */
+    return JNI_FALSE;
+}
+
 ACR_WIN_EXPORT(jlong, WindowsSemaphore, create0)(JNI_STDARGS,
                                                  jstring name,
                                                  jint value, jlong sd)

Modified: commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestSemaphore.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestSemaphore.java?rev=1092621&r1=1092620&r2=1092621&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestSemaphore.java (original)
+++ commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestSemaphore.java Fri Apr 15 08:06:09 2011
@@ -34,7 +34,20 @@ public class TestSemaphore
     }
 
     @Test(groups = { "semaphore.parent" })
-    public void create()
+    public void checkSemaphore()
+        throws Exception
+    {
+        try {
+            Semaphore s = Semaphore.getImpl().create(semname, 0);
+            s.close();
+        } catch (Exception ex) {
+            System.out.println("Removing stalled semaphore " + semname);
+            Assert.assertTrue(Semaphore.remove(semname));
+        }
+    }
+
+    @Test(groups = { "semaphore.parent" })
+    public void createSemaphore()
         throws Exception
     {
         Semaphore s = Semaphore.getImpl().create(semname, 0);
@@ -45,10 +58,21 @@ public class TestSemaphore
     }
 
     @Test(groups = { "semaphore.child" })
-    public void open()
+    public void openSemaphore()
         throws Exception
     {
-        Semaphore s = Semaphore.getImpl().open(semname);
+        Semaphore s = null;
+        int step = 125;
+        while (step <= 2000) {
+            try {
+                s = Semaphore.getImpl().open(semname);
+                break;
+            } catch (Exception x) {
+                
+            }
+            Thread.sleep(step);
+            step *= 2;
+        }
         Assert.assertNotNull(s);
         s.release();
         s.close();