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/14 16:30:10 UTC

svn commit: r804239 - in /commons/sandbox/runtime/trunk/src/main/native: include/acr_shm.h os/hpux/shm.c os/unix/shm.c os/win32/shm.c test/testcase.c

Author: mturk
Date: Fri Aug 14 14:30:10 2009
New Revision: 804239

URL: http://svn.apache.org/viewvc?rev=804239&view=rev
Log:
Added ShmDetach API.

Modified:
    commons/sandbox/runtime/trunk/src/main/native/include/acr_shm.h
    commons/sandbox/runtime/trunk/src/main/native/os/hpux/shm.c
    commons/sandbox/runtime/trunk/src/main/native/os/unix/shm.c
    commons/sandbox/runtime/trunk/src/main/native/os/win32/shm.c
    commons/sandbox/runtime/trunk/src/main/native/test/testcase.c

Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr_shm.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr_shm.h?rev=804239&r1=804238&r2=804239&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr_shm.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr_shm.h Fri Aug 14 14:30:10 2009
@@ -80,6 +80,14 @@
                                const acr_pchar_t *filename);
 
 /**
+ * Dettach shared memory segment without destroying it..
+ * @param env JNI environment to use.
+ * @param m The shared memory structure representing the segment
+ *        to detach from.
+ */
+ACR_DECLARE(int) ACR_ShmDetach(JNIEnv *env, int m);
+
+/**
  * Destroy or detach from a shared memory segment.
  * @param env JNI environment to use.
  * @param m The shared memory structure representing the segment

Modified: commons/sandbox/runtime/trunk/src/main/native/os/hpux/shm.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/hpux/shm.c?rev=804239&r1=804238&r2=804239&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/hpux/shm.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/hpux/shm.c Fri Aug 14 14:30:10 2009
@@ -63,9 +63,11 @@
     if (type != ACR_DT_SHM) {
         return ACR_EINVAL;
     }
-    /* anonymous shared memory */
+    /* Anonymous shared memory or
+     * we are calling this from forked child. 
+     */
     if (m->filename == NULL) {
-        if (shmdt(m->base) == -1) {
+        if (m->base && shmdt(m->base) == -1) {
             rc = ACR_GET_OS_ERROR();
         }
     }
@@ -79,7 +81,7 @@
             rc = ACR_GET_OS_ERROR();
             goto cleanup;
         }
-        if (shmdt(m->base) == -1) {
+        if (m->base && shmdt(m->base) == -1) {
             rc = ACR_GET_OS_ERROR();
             goto cleanup;
         }
@@ -114,7 +116,7 @@
         rc = ACR_EINVAL;
     }
     else {
-        if (shmdt(m->base) == -1) {
+        if (m->base && shmdt(m->base) == -1) {
             rc = ACR_GET_OS_ERROR();
         }
     }
@@ -138,6 +140,20 @@
     return rv;
 }
 
+ACR_DECLARE(int) ACR_ShmDetach(JNIEnv *_E, int shm)
+{
+    acr_shm_t *m = (acr_shm_t *)ACR_IOH(shm);
+
+    if (IS_INVALID_HANDLE(m) || ACR_IOH_TYPE(shm) != ACR_DT_SHM) {
+        return ACR_EINVAL;
+    }        
+    if (m->base && shmdt(m->base) == -1) {
+        return ACR_GET_OS_ERROR();
+    }
+    m->base = NULL;
+    return 0;
+}
+
 ACR_DECLARE(int) ACR_ShmRemove(JNIEnv *_E, const acr_pchar_t *filename)
 {
     int     rc = 0;

Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/shm.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/shm.c?rev=804239&r1=804238&r2=804239&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/shm.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/shm.c Fri Aug 14 14:30:10 2009
@@ -63,9 +63,11 @@
     if (type != ACR_DT_SHM) {
         return ACR_EINVAL;
     }
-    /* anonymous shared memory */
+    /* Anonymous shared memory or
+     * we are calling this from forked child. 
+     */
     if (m->filename == NULL) {
-        if (munmap(m->base, m->realsize) == -1) {
+        if (m->base && munmap(m->base, m->realsize) == -1) {
             rc = ACR_GET_OS_ERROR();
         }
     }
@@ -79,7 +81,7 @@
             rc = ACR_GET_OS_ERROR();
             goto cleanup;
         }
-        if (shmdt(m->base) == -1) {
+        if (m->base && shmdt(m->base) == -1) {
             rc = ACR_GET_OS_ERROR();
             goto cleanup;
         }
@@ -114,7 +116,7 @@
         rc = ACR_EINVAL;
     }
     else {
-        if (shmdt(m->base) == -1) {
+        if (m->base && shmdt(m->base) == -1) {
             rc = ACR_GET_OS_ERROR();
         }
     }
@@ -414,6 +416,20 @@
         return -1;
 }
 
+ACR_DECLARE(int) ACR_ShmDetach(JNIEnv *_E, int shm)
+{
+    acr_shm_t *m = (acr_shm_t *)ACR_IOH(shm);
+
+    if (IS_INVALID_HANDLE(m) || ACR_IOH_TYPE(shm) != ACR_DT_SHM) {
+        return ACR_EINVAL;
+    }        
+    if (m->base && shmdt(m->base) == -1) {
+        return ACR_GET_OS_ERROR();
+    }
+    m->base = NULL;
+    return 0;
+}
+
 ACR_DECLARE(int) ACR_ShmPermSet(JNIEnv *_E, int shm, int perms,
                                 acr_uid_t uid, acr_uid_t gid)
 {

Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/shm.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/shm.c?rev=804239&r1=804238&r2=804239&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/shm.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/shm.c Fri Aug 14 14:30:10 2009
@@ -50,7 +50,7 @@
     if (type != ACR_DT_SHM) {
         return ACR_EBADF;
     }
-    if (!UnmapViewOfFile(m->memblk)) {
+    if (m->memblk && !UnmapViewOfFile(m->memblk)) {
         rc = ACR_GET_OS_ERROR();
     }
     if (!CloseHandle(m->hmap) && rc) {
@@ -296,6 +296,21 @@
         return -1;
 }
 
+ACR_DECLARE(int) ACR_ShmDetach(JNIEnv *_E, int shm)
+{
+    acr_shm_t *m = (acr_shm_t *)ACR_IOH(shm);
+
+    if (IS_INVALID_HANDLE(m) || ACR_IOH_TYPE(shm) != ACR_DT_SHM) {
+        return ACR_EINVAL;
+    }        
+    if (m->memblk && !UnmapViewOfFile(m->memblk)) {
+        return ACR_GET_OS_ERROR();
+    }
+    m->memblk = NULL;
+    m->usrmem = NULL;
+    return 0;
+}
+
 ACR_DECLARE(int) ACR_ShmPermSet(JNIEnv *_E, int shm, int perms,
                                 acr_uid_t uid, acr_uid_t gid)
 {

Modified: commons/sandbox/runtime/trunk/src/main/native/test/testcase.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/test/testcase.c?rev=804239&r1=804238&r2=804239&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/test/testcase.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/test/testcase.c Fri Aug 14 14:30:10 2009
@@ -681,13 +681,15 @@
 #if defined(WIN32)
     return ACR_ShmAttach(_E, shr_name);
 #else
-    extern int acr_shm_detach();
     int stat;
     pid_t child;
     if ((child = fork()) == 0) {
         int rc;
         errno = 0;
-        acr_shm_detach(d);
+        /* Detach child so we can attach like regular process.
+         * ACR_ShmClose will only detach without destroying the shm.
+         */
+        ACR_ShmDetach(_E, d);
         rc = ACR_ShmAttach(_E, shr_name);
         fprintf(stdout, "Attach returned %d errno=%d\n", rc, errno);
         fflush(stdout);