commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mt...@apache.org
Subject svn commit: r806251 - in /commons/sandbox/runtime/trunk/src/main: java/org/apache/commons/runtime/Semaphore.java native/include/acr_semaphore.h native/os/unix/psema.c native/os/win32/psema.c native/shared/sema.c
Date Thu, 20 Aug 2009 16:55:27 GMT
Author: mturk
Date: Thu Aug 20 16:55:27 2009
New Revision: 806251

URL: http://svn.apache.org/viewvc?rev=806251&view=rev
Log:
Add Semaphore.reset

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/os/unix/psema.c
    commons/sandbox/runtime/trunk/src/main/native/os/win32/psema.c
    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=806251&r1=806250&r2=806251&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:55:27 2009
@@ -197,5 +197,21 @@
         }
     }
 
+    public static native void release1(Descriptor sem)
+        throws IOException, SecurityException;
+    /**
+     * Reset the given semaphore.
+     */
+    public void reset()
+        throws IOException, SecurityException, ClosedDescriptorException
+    {
+        if (sem.valid()) {
+            release1(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=806251&r1=806250&r2=806251&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:55:27
2009
@@ -81,6 +81,13 @@
 ACR_DECLARE(int) ACR_SemaphoreRelease(JNIEnv *env, int semaphore);
 
 /**
+ * Reset the the given semaphore.
+ * @param env JNI environment to use.
+ * @param semaphore the semaphore which to reset.
+ */
+ACR_DECLARE(int) ACR_SemaphoreReset(JNIEnv *env, int semaphore);
+
+/**
  * Destroy the semaphore and free the memory associated with the lock.
  * @param env JNI environment to use.
  * @param semaphore the semaphore to destroy.

Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/psema.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/psema.c?rev=806251&r1=806250&r2=806251&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/psema.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/psema.c Thu Aug 20 16:55:27 2009
@@ -252,6 +252,36 @@
     return ACR_SUCCESS;
 }
 
+ACR_DECLARE(int) ACR_SemaphoreReset(JNIEnv *_E, int sema)
+{
+    int rc = 0;
+    acr_semaphore_t *s = (acr_semaphore_t *)ACR_IOH(sema);
+
+    if (IS_INVALID_HANDLE(s) || ACR_IOH_TYPE(sema) != ACR_DT_SEMAPHORE) {
+        return ACR_EINVAL;
+    }
+
+    s->locked = 0;
+    for (;;) {
+        do {
+            rc = sem_trywait(s->sem);
+        } while (rc < 0 && errno == EINTR);
+        if (rc < 0) {
+            if (s->locked == 0) {
+                /* We are not the semaphore owner */
+                if (errno == EAGAIN)
+                    return ACR_EBUSY;
+                else
+                    return ACR_GET_OS_ERROR();
+            }
+            else
+                break;
+        }
+        s->locked++;
+    }
+    return ACR_SUCCESS;
+}
+
 ACR_DECLARE(int) ACR_SemaphoreRelease(JNIEnv *env, int sema)
 {
     int rc = 0;

Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/psema.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/psema.c?rev=806251&r1=806250&r2=806251&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/psema.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/psema.c Thu Aug 20 16:55:27 2009
@@ -201,6 +201,31 @@
     return ACR_GET_OS_ERROR();
 }
 
+ACR_DECLARE(int) ACR_SemaphoreReset(JNIEnv *_E, int sema)
+{
+    int nc = 0;
+    int rc = 0;
+    acr_semaphore_t *s = (acr_semaphore_t *)ACR_IOH(sema);
+
+    if (IS_INVALID_HANDLE(s) || ACR_IOH_TYPE(sema) != ACR_DT_SEMAPHORE) {
+        return ACR_EINVAL;
+    }
+
+    for (;;) {
+        rc = WaitForSingleObject(s, 0);
+        if (rc == WAIT_OBJECT_0 || rc == WAIT_ABANDONED) {
+            nc++;
+        }
+        else if (rc == WAIT_TIMEOUT) {
+            if (nc == 0)
+                return ACR_EBUSY;
+            else
+                break;
+        }
+    }
+    return ACR_SUCCESS;
+}
+
 ACR_DECLARE(int) ACR_SemaphoreRelease(JNIEnv *env, int sema)
 {
     int rc = 0;

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=806251&r1=806250&r2=806251&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:55:27 2009
@@ -107,3 +107,18 @@
     ACR_THROW_IO_IF_ERR(rc);
 }
 
+ACR_JNI_EXPORT_DECLARE(void, Semaphore, release1)(ACR_JNISTDARGS,
+                                                  jobject semd)
+{
+    int rc = ACR_EBADF;
+    int sd;
+    UNREFERENCED_O;
+
+    sd = ACR_DescriptorGetInt(_E, semd);
+    if (sd > 0) {    
+        rc = ACR_SemaphoreReset(_E, sd);
+    }
+
+    ACR_THROW_IO_IF_ERR(rc);
+}
+



Mime
View raw message