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 2009/08/20 18:34:52 UTC

svn commit: r806243 - in /commons/sandbox/runtime/trunk/src/main: java/org/apache/commons/runtime/Semaphore.java native/include/acr_semaphore.h native/shared/sema.c

Author: mturk
Date: Thu Aug 20 16:34:52 2009
New Revision: 806243

URL: http://svn.apache.org/viewvc?rev=806243&view=rev
Log:
Initial Java Semaphore API

Modified:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Semaphore.java
    commons/sandbox/runtime/trunk/src/main/native/include/acr_semaphore.h
    commons/sandbox/runtime/trunk/src/main/native/shared/sema.c

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=806243&r1=806242&r2=806243&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 Thu Aug 20 16:34:52 2009
@@ -17,11 +17,13 @@
 
 import java.io.IOException;
 import org.apache.commons.runtime.io.File;
+import org.apache.commons.runtime.io.Status;
+import org.apache.commons.runtime.exception.ClosedDescriptorException;
 import org.apache.commons.runtime.exception.OperatingSystemException;
 import org.apache.commons.runtime.exception.UnsupportedOperatingSystemException;
 
 /**
- * Semaphoreclass.
+ * Semaphore class.
  * <p>
  * </p>
  *
@@ -31,7 +33,7 @@
 {
 
     /*
-     * Reference to the OS process mutex descriptor
+     * Reference to the OS semaphore descriptor
      */
     private Descriptor sem;
 
@@ -45,5 +47,155 @@
         this.sem = sem;
     }
 
+    private static native Semaphore create0(int value)
+        throws IOException, SecurityException, OutOfMemoryError;
+    /**
+    /**
+     * Create and make accessable anonymous {@code Semaphore} object.
+     * <p>
+     * A note about Anonymous vs. Named semaphore objects:<br />
+     *         Not all plaforms support anonymous semaphores, but in
+     *         some cases it is prefered over other types of semaphore
+     *         implementations. If such a system is not available,
+     *         {@code UnsupportedOperatingSystemException} is thrown.
+     * </p>
+     * @param value The initial semaphore value.
+     * @param name The file to use for semaphore on platforms that
+     *        require it.
+     */
+    public static Semaphore create(int value)
+        throws IOException, SecurityException, OutOfMemoryError
+    {
+        return create0(value);
+    }
+
+    private static native Semaphore create1(String name, int value)
+        throws IOException, SecurityException, OutOfMemoryError;
+    /**
+    /**
+     * Create and make accessable anonymous {@code Semaphore} object.
+     * <p>
+     * A note about Anonymous vs. Named semaphore objects:<br />
+     *         Not all plaforms support anonymous semaphores, but in
+     *         some cases it is prefered over other types of semaphore
+     *         implementations. If such a system is not available,
+     *         {@code UnsupportedOperatingSystemException} is thrown.
+     * </p>
+     * @param value The initial semaphore value.
+     * @param file The abstract file path to use for semaphore on platforms
+     *             that require it.
+     */
+    public static Semaphore create(File file, int value)
+        throws IOException, SecurityException, OutOfMemoryError
+    {
+        return create1(file.getPath(), value);
+    }
+
+    private static native Semaphore create2(String name)
+        throws IOException, SecurityException, OutOfMemoryError;
+    /**
+     * Open a semaphore that was created by another process.
+     * @param file The abstract file path that was used to create a
+     *             semaphore.
+     */
+    public static Semaphore open(File file)
+        throws IOException, SecurityException, OutOfMemoryError
+    {
+        return create2(file.getPath());
+    }
+
+    private static native int remove0(String name)
+        throws IOException, SecurityException;
+    /**
+     * Remove semaphore associated with a {@code file}.
+     * @param file The abstract file path associated with semaphore
+     *             object which needs to be removed.
+     */
+    public static boolean remove(File file)
+        throws IOException, SecurityException
+    {
+        int rc;
+
+        rc = remove0(file.getPath());
+        if (rc == Status.OK)
+            return true;
+        else
+            return false;
+    }
+
+    private static native int close0(Descriptor sem)
+        throws IOException, SecurityException;
+    /**
+     * Close and remove the semaphore object from the system.
+     * <p>
+     * Closing semaphore will remove reference to the semaphore
+     * object and remove it from the system. Actual removal
+     * will happen when the last {@code Semaphore} object referencing
+     * the semaphore is closed.
+     * </p>
+     */
+    public void close()
+        throws IOException, SecurityException, ClosedDescriptorException
+    {
+        if (sem.valid()) {
+            close0(sem);
+        }
+        else {
+            throw new ClosedDescriptorException();
+        }
+    }
+
+    public static native void wait0(Descriptor sem)
+        throws IOException, SecurityException;
+    /**
+     * Acquire the lock for the given semaphore.
+     * If the semaphore is already locked,
+     * the current thread will be put to sleep until the lock becomes available.
+     */
+    public void acquire()
+        throws IOException, SecurityException, ClosedDescriptorException
+    {
+        if (sem.valid()) {
+            wait0(sem);
+        }
+        else {
+            throw new ClosedDescriptorException();
+        }
+    }
+
+    public static native boolean wait1(Descriptor sem)
+        throws IOException, SecurityException;
+    /**
+     * Attempt to acquire the lock for the given semaphore.
+     * If the semaphore has already
+     * been acquired, the call returns immediately with {@code false}.
+     */
+    public boolean tryAcquire()
+        throws IOException, SecurityException, ClosedDescriptorException
+    {
+        if (sem.valid()) {
+            return wait1(sem);
+        }
+        else {
+            throw new ClosedDescriptorException();
+        }
+    }
+
+    public static native void release0(Descriptor sem)
+        throws IOException, SecurityException;
+    /**
+     * Release the lock for the given semaphore.
+     */
+    public void release()
+        throws IOException, SecurityException, ClosedDescriptorException
+    {
+        if (sem.valid()) {
+            release0(sem);
+        }
+        else {
+            throw new ClosedDescriptorException();
+        }
+    }
+
 }
 

Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr_semaphore.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr_semaphore.h?rev=806243&r1=806242&r2=806243&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr_semaphore.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr_semaphore.h Thu Aug 20 16:34:52 2009
@@ -74,7 +74,7 @@
 ACR_DECLARE(int) ACR_SemaphoreTryWait(JNIEnv *env, int semaphore);
 
 /**
- * Release the lock for the given mutex.
+ * Release the lock for the given semaphore.
  * @param env JNI environment to use.
  * @param semaphore the semaphore from which to release the lock.
  */

Modified: commons/sandbox/runtime/trunk/src/main/native/shared/sema.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/sema.c?rev=806243&r1=806242&r2=806243&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/sema.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/sema.c Thu Aug 20 16:34:52 2009
@@ -49,11 +49,12 @@
         ACR_DescriptorClear(_E, semd);
         rc = ACR_SemaphoreClose(_E, sd);
     }
+    ACR_THROW_IO_IF_ERR(rc);
     return rc;
 }
 
-ACR_JNI_EXPORT_DECLARE(jboolean, Semaphore, wait0)(ACR_JNISTDARGS,
-                                                   jobject semd)
+ACR_JNI_EXPORT_DECLARE(void, Semaphore, wait0)(ACR_JNISTDARGS,
+                                               jobject semd)
 {
     int rc = ACR_EBADF;
     int sd;
@@ -63,18 +64,15 @@
     if (sd > 0) {    
         rc = ACR_SemaphoreWait(_E, sd);
     }
-    if (rc == ACR_SUCCESS)
-        return JNI_TRUE;
-    else {
-        if (rc != ACR_EBUSY) {
+    if (rc != ACR_SUCCESS) {
+        if (!ACR_STATUS_IS_EBUSY(rc)) {
             ACR_THROW_IO_IF_ERR(rc);
         }
-        return JNI_FALSE;
     }
 }
 
 ACR_JNI_EXPORT_DECLARE(jboolean, Semaphore, wait1)(ACR_JNISTDARGS,
-                                                   jobject semd)
+                                                  jobject semd)
 {
     int rc = ACR_EBADF;
     int sd;
@@ -84,18 +82,18 @@
     if (sd > 0) {    
         rc = ACR_SemaphoreTryWait(_E, sd);
     }
-    if (rc == ACR_SUCCESS)
-        return JNI_TRUE;
-    else {
-        if (rc != ACR_EBUSY) {
+    if (rc != ACR_SUCCESS) {
+        if (!ACR_STATUS_IS_EBUSY(rc)) {
             ACR_THROW_IO_IF_ERR(rc);
         }
         return JNI_FALSE;
     }
+    else
+        return JNI_FALSE;
 }
 
-ACR_JNI_EXPORT_DECLARE(jboolean, Semaphore, release0)(ACR_JNISTDARGS,
-                                                      jobject semd)
+ACR_JNI_EXPORT_DECLARE(void, Semaphore, release0)(ACR_JNISTDARGS,
+                                                  jobject semd)
 {
     int rc = ACR_EBADF;
     int sd;
@@ -107,6 +105,5 @@
     }
 
     ACR_THROW_IO_IF_ERR(rc);
-    return rc ? JNI_FALSE : JNI_TRUE;
 }