commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mt...@apache.org
Subject svn commit: r806220 - in /commons/sandbox/runtime/trunk/src/main/native: Makefile.in Makefile.msc.in include/acr_error.h include/acr_semaphore.h os/unix/psema.c os/unix/pshm.c os/win32/psema.c shared/sema.c
Date Thu, 20 Aug 2009 15:41:26 GMT
Author: mturk
Date: Thu Aug 20 15:41:26 2009
New Revision: 806220

URL: http://svn.apache.org/viewvc?rev=806220&view=rev
Log:
initial semaphore wrapper API

Added:
    commons/sandbox/runtime/trunk/src/main/native/shared/sema.c   (with props)
Modified:
    commons/sandbox/runtime/trunk/src/main/native/Makefile.in
    commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in
    commons/sandbox/runtime/trunk/src/main/native/include/acr_error.h
    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/unix/pshm.c
    commons/sandbox/runtime/trunk/src/main/native/os/win32/psema.c

Modified: commons/sandbox/runtime/trunk/src/main/native/Makefile.in
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/Makefile.in?rev=806220&r1=806219&r2=806220&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/Makefile.in (original)
+++ commons/sandbox/runtime/trunk/src/main/native/Makefile.in Thu Aug 20 15:41:26 2009
@@ -97,6 +97,7 @@
 	$(SRCDIR)/shared/sha1.$(OBJ) \
 	$(SRCDIR)/shared/sha2.$(OBJ) \
 	$(SRCDIR)/shared/sbuf.$(OBJ) \
+	$(SRCDIR)/shared/sema.$(OBJ) \
 	$(SRCDIR)/shared/shm.$(OBJ) \
 	$(SRCDIR)/shared/string.$(OBJ) \
 	$(SRCDIR)/shared/tables.$(OBJ) \

Modified: commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in?rev=806220&r1=806219&r2=806220&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in (original)
+++ commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in Thu Aug 20 15:41:26 2009
@@ -88,6 +88,7 @@
 	$(SRCDIR)/shared/sha1.$(OBJ) \
 	$(SRCDIR)/shared/sha2.$(OBJ) \
 	$(SRCDIR)/shared/sbuf.$(OBJ) \
+	$(SRCDIR)/shared/sema.$(OBJ) \
 	$(SRCDIR)/shared/shm.$(OBJ) \
 	$(SRCDIR)/shared/string.$(OBJ) \
 	$(SRCDIR)/shared/tables.$(OBJ) \

Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr_error.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr_error.h?rev=806220&r1=806219&r2=806220&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr_error.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr_error.h Thu Aug 20 15:41:26
2009
@@ -137,6 +137,12 @@
         ACR_SET_OS_ERROR((S));                                              \
     } else _E = _E
 
+#define ACR_THROW_EX_IF_ERR(X, S)                                           \
+    if ((S) && IS_VALID_HANDLE(_E)) {                                       \
+        ACR_ThrowException(_E, THROW_NMARK, (X), (S));                      \
+        ACR_SET_OS_ERROR((S));                                              \
+    } else _E = _E
+
 #define ACR_THROW_IO_ERRNO()                                                \
     if (IS_VALID_HANDLE(_E)) {                                              \
         int _se = ACR_GET_OS_ERROR();                                       \

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=806220&r1=806219&r2=806220&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 15:41:26
2009
@@ -97,6 +97,24 @@
  */
 ACR_DECLARE(int) ACR_SemaphoreRemove(JNIEnv *env, const acr_pchar_t *name);
 
+/**
+ * Create and initialize a semaphore that can be used to synchronize processes.
+ * @param env JNI environment to use.
+ * @param name A name to use for the semaphore.
+ * @param value Initial semaphore value.
+ */
+ACR_DECLARE(jobject) ACR_SemaphoreObjectCreate(JNIEnv *env,
+                                               const acr_pchar_t *name,
+                                               int value);
+
+/**
+ * Re-open a semaphore in child process.
+ * @param env JNI environment to use.
+ * @param name A name of the semaphore to open.
+ */
+ACR_DECLARE(jobject) ACR_SemaphoreObjectAttach(JNIEnv *env,
+                                               const acr_pchar_t *name);
+
 #ifdef __cplusplus
 }
 #endif

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=806220&r1=806219&r2=806220&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 15:41:26 2009
@@ -289,3 +289,111 @@
     ACR_UnloadClass(_E, &_clazzn);
 }
 
+static int sem_descriptor_cleanup(ACR_JNISTDARGS,
+                                  acr_descriptor_cb_type_e cm,
+                                  acr_descriptor_cb_t *dp)
+{
+    int rc = ACR_SUCCESS;
+    switch (cm) {
+        case ACR_DESC_CLOSE:
+            if (dp->di > 0)
+                rc = acr_ioh_close(dp->di);
+            else
+                rc = ACR_EBADF;
+        break;
+        default:
+            rc = ACR_ENOTIMPL;
+        break;
+    }
+    return rc;
+}
+
+ACR_DECLARE(jobject) ACR_SemaphoreObjectCreate(JNIEnv *_E,
+                                               const acr_pchar_t *name,
+                                               int value)
+{
+    jobject semo;
+    jobject semd;
+    int     isem;
+
+    isem = ACR_SemaphoreCreate(_E, name, value);
+    if (isem < 0) {
+
+        return NULL;
+    }
+    /* Create Descriptor Object */
+    semd = ACR_DescriptorCreate(_E, ACR_DT_SEMAPHORE, isem, NULL,
+                                sem_descriptor_cleanup);
+    if (!semd) {
+
+        return NULL;
+    }
+    semo = (*_E)->NewObject(_E, _clazzn.i, J4MID(0000), semd);
+    return semo;
+}
+
+ACR_DECLARE(jobject) ACR_SemaphoreObjectAttach(JNIEnv *_E,
+                                               const acr_pchar_t *name)
+{
+    jobject semo;
+    jobject semd;
+    int     isem;
+
+    isem = ACR_SemaphoreAttach(_E, name);
+    if (isem < 0) {
+
+        return NULL;
+    }
+    /* Create Descriptor Object */
+    semd = ACR_DescriptorCreate(_E, ACR_DT_SEMAPHORE, isem, NULL,
+                                sem_descriptor_cleanup);
+    if (!semd) {
+
+        return NULL;
+    }
+    semo = (*_E)->NewObject(_E, _clazzn.i, J4MID(0000), semd);
+    return semo;
+}
+
+ACR_JNI_EXPORT_DECLARE(jobject, Semaphore, create1)(ACR_JNISTDARGS,
+                                                    jstring name,
+                                                    jint value)
+{
+
+    jobject semo = NULL;
+    UNREFERENCED_O;
+
+    WITH_CSTR(name) {
+        semo = ACR_SemaphoreObjectCreate(_E, J2S(name), value);
+    } END_WITH_CSTR(name);
+
+    return semo;
+}
+
+ACR_JNI_EXPORT_DECLARE(jobject, Semaphore, create2)(ACR_JNISTDARGS,
+                                                    jstring name)
+{
+
+    jobject semo = NULL;
+    UNREFERENCED_O;
+
+    WITH_CSTR(name) {
+        semo = ACR_SemaphoreObjectAttach(_E, J2S(name));
+    } END_WITH_CSTR(name);
+
+    return semo;
+}
+
+ACR_JNI_EXPORT_DECLARE(jint, Semaphore, remove0)(ACR_JNISTDARGS,
+                                                 jstring name)
+{
+
+    int rc = ACR_SUCCESS;
+    UNREFERENCED_O;
+
+    WITH_CSTR(name) {
+        rc = ACR_SemaphoreRemove(_E, J2S(name));
+    } END_WITH_CSTR(name);
+    return rc;
+}
+

Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/pshm.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/pshm.c?rev=806220&r1=806219&r2=806220&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/pshm.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/pshm.c Thu Aug 20 15:41:26 2009
@@ -195,12 +195,7 @@
 
 finally:
     unlink(filename);
-    if (rc  && IS_VALID_HANDLE(_E)) {
-        if (ACR_STATUS_IS_EACCES(rc))
-            ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ESECURITY, 0);
-        else
-            ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EIO, rc);
-    }
+    ACR_THROW_IO_IF_ERR(rc);
     return rc;
 }
 
@@ -216,10 +211,7 @@
 
     if (reqsize > ACR_SIZE_T_MAX) {
         /* Guard against insane sizes */
-        if (IS_VALID_HANDLE(_E)) {
-            ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EINVAL, 0);
-        }
-        ACR_SET_OS_ERROR(ACR_EINVAL);
+        ACR_THROW_EX_IF_ERR(ACR_EX_EINVAL, ACR_EINVAL);
         return -1;
     }
     shm = ACR_Calloc(_E, THROW_FMARK, sizeof(acr_shm_t));
@@ -236,13 +228,7 @@
         if (shm->base == (void *)MAP_FAILED) {
             rc =  ACR_GET_OS_ERROR();
             free(shm);
-            if (IS_VALID_HANDLE(_E)) {
-                if (ACR_STATUS_IS_EACCES(rc))
-                    ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ESECURITY, 0);
-                else
-                    ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EIO, rc);
-            }
-            ACR_SET_OS_ERROR(rc);
+            ACR_THROW_IO_IF_ERR(rc);
             return -1;
         }
 
@@ -322,10 +308,7 @@
         if (rc) {
             free((void *)(shm->filename));
             free(shm);
-            if (IS_VALID_HANDLE(_E)) {
-                ACR_THROW_EIO(rc);
-            }
-            ACR_SET_OS_ERROR(rc);
+            ACR_THROW_IO_IF_ERR(rc);
             shm = NULL;
         }
     }
@@ -347,7 +330,7 @@
     acr_size_t  nbytes;
 
     if (!filename) {
-        ACR_SET_OS_ERROR(ACR_EINVAL);
+        ACR_THROW_IO_IF_ERR(ACR_EINVAL);
         return -1;
     }
     shm = ACR_Calloc(_E, THROW_FMARK, sizeof(acr_shm_t));
@@ -408,10 +391,7 @@
     if (rc) {
         free((void *)(shm->filename));
         free(shm);
-        if (IS_VALID_HANDLE(_E)) {
-            ACR_THROW_EIO(rc);
-        }
-        ACR_SET_OS_ERROR(rc);
+        ACR_THROW_IO_IF_ERR(rc);
         shm = NULL;
     }
     if (shm) {
@@ -435,10 +415,9 @@
     m->filename = NULL;
 
     rc = acr_ioh_close(shm);
+
 finally:
-    if (rc  && IS_VALID_HANDLE(_E)) {
-        ACR_THROW_EIO(rc);
-    }
+    ACR_THROW_IO_IF_ERR(rc);
     return rc;
 }
 
@@ -467,9 +446,7 @@
     }
 
 finally:
-    if (rc && IS_VALID_HANDLE(_E)) {
-        ACR_THROW_EIO(rc);
-    }
+    ACR_THROW_IO_IF_ERR(rc);
     return rc;
 }
 
@@ -545,7 +522,7 @@
         return NULL;
     }
     /* Create Descriptor Object */
-    shmd = ACR_DescriptorCreate(_E, ACR_DT_USER, ishm, NULL,
+    shmd = ACR_DescriptorCreate(_E, ACR_DT_SHM, ishm, NULL,
                                 shm_descriptor_cleanup);
     if (!shmd) {
 
@@ -568,7 +545,7 @@
         return NULL;
     }
     /* Create Descriptor Object */
-    shmd = ACR_DescriptorCreate(_E, ACR_DT_USER, ishm, NULL,
+    shmd = ACR_DescriptorCreate(_E, ACR_DT_SHM, ishm, NULL,
                                 shm_descriptor_cleanup);
     if (!shmd) {
 

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=806220&r1=806219&r2=806220&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 15:41:26 2009
@@ -239,3 +239,111 @@
     ACR_UnloadClass(_E, &_clazzn);
 }
 
+static int sem_descriptor_cleanup(ACR_JNISTDARGS,
+                                  acr_descriptor_cb_type_e cm,
+                                  acr_descriptor_cb_t *dp)
+{
+    int rc = ACR_SUCCESS;
+    switch (cm) {
+        case ACR_DESC_CLOSE:
+            if (dp->di > 0)
+                rc = acr_ioh_close(dp->di);
+            else
+                rc = ACR_EBADF;
+        break;
+        default:
+            rc = ACR_ENOTIMPL;
+        break;
+    }
+    return rc;
+}
+
+ACR_DECLARE(jobject) ACR_SemaphoreObjectCreate(JNIEnv *_E,
+                                               const acr_pchar_t *name,
+                                               int value)
+{
+    jobject semo;
+    jobject semd;
+    int     isem;
+
+    isem = ACR_SemaphoreCreate(_E, name, value);
+    if (isem < 0) {
+
+        return NULL;
+    }
+    /* Create Descriptor Object */
+    semd = ACR_DescriptorCreate(_E, ACR_DT_SEMAPHORE, isem, NULL,
+                                sem_descriptor_cleanup);
+    if (!semd) {
+
+        return NULL;
+    }
+    semo = (*_E)->NewObject(_E, _clazzn.i, J4MID(0000), semd);
+    return semo;
+}
+
+ACR_DECLARE(jobject) ACR_SemaphoreObjectAttach(JNIEnv *_E,
+                                               const acr_pchar_t *name)
+{
+    jobject semo;
+    jobject semd;
+    int     isem;
+
+    isem = ACR_SemaphoreAttach(_E, name);
+    if (isem < 0) {
+
+        return NULL;
+    }
+    /* Create Descriptor Object */
+    semd = ACR_DescriptorCreate(_E, ACR_DT_SEMAPHORE, isem, NULL,
+                                sem_descriptor_cleanup);
+    if (!semd) {
+
+        return NULL;
+    }
+    semo = (*_E)->NewObject(_E, _clazzn.i, J4MID(0000), semd);
+    return semo;
+}
+
+ACR_JNI_EXPORT_DECLARE(jobject, Semaphore, create1)(ACR_JNISTDARGS,
+                                                    jstring name,
+                                                    jint value)
+{
+
+    jobject semo = NULL;
+    UNREFERENCED_O;
+
+    WITH_WSTR(name) {
+        semo = ACR_SemaphoreObjectCreate(_E, J2W(name), value);
+    } END_WITH_WSTR(name);
+
+    return semo;
+}
+
+ACR_JNI_EXPORT_DECLARE(jobject, Semaphore, create2)(ACR_JNISTDARGS,
+                                                    jstring name)
+{
+
+    jobject semo = NULL;
+    UNREFERENCED_O;
+
+    WITH_WSTR(name) {
+        semo = ACR_SemaphoreObjectAttach(_E, J2W(name));
+    } END_WITH_WSTR(name);
+
+    return semo;
+}
+
+ACR_JNI_EXPORT_DECLARE(jint, Semaphore, remove0)(ACR_JNISTDARGS,
+                                                 jstring name)
+{
+
+    int rc = ACR_SUCCESS;
+    UNREFERENCED_O;
+
+    WITH_WSTR(name) {
+        rc = ACR_SemaphoreRemove(_E, J2W(name));
+    } END_WITH_WSTR(name);
+    return rc;
+}
+

Added: 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=806220&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/sema.c (added)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/sema.c Thu Aug 20 15:41:26 2009
@@ -0,0 +1,39 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "acr.h"
+#include "acr_private.h"
+#include "acr_arch.h"
+#include "acr_clazz.h"
+#include "acr_error.h"
+#include "acr_memory.h"
+#include "acr_string.h"
+#include "acr_descriptor.h"
+#include "acr_pointer.h"
+#include "acr_semaphore.h"
+
+ACR_JNI_EXPORT_DECLARE(jobject, Semaphore, create0)(ACR_JNISTDARGS,
+                                                    jint value)
+{
+
+    jobject semo = NULL;
+    UNREFERENCED_O;
+
+    semo = ACR_SemaphoreObjectCreate(_E, NULL, value);
+
+    return semo;
+}
+

Propchange: commons/sandbox/runtime/trunk/src/main/native/shared/sema.c
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message