Author: mturk
Date: Wed Apr 20 05:51:20 2011
New Revision: 1095274

URL: http://svn.apache.org/viewvc?rev=1095274&view=rev
Log:
Allow attaching/detaching only once per Shm instance

Modified:
    
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Shm.java
    
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/Posix.java
    
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/PosixShm.java
    
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/Win32.java
    commons/sandbox/runtime/trunk/src/main/native/os/unix/shmem.c
    commons/sandbox/runtime/trunk/src/main/native/shared/pointer.c

Modified: 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Shm.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Shm.java?rev=1095274&r1=1095273&r2=1095274&view=diff
==============================================================================
--- 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Shm.java 
(original)
+++ 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Shm.java 
Wed Apr 20 05:51:20 2011
@@ -86,14 +86,12 @@ public abstract class Shm
     }
         
     /**
-     * Detaches the shared memory segment located at the address
-     * specified by {@code address} from the address space ofthe
-     * calling  process.
+     * Detaches the shared memory segment from the address space
+     * of the calling  process.
      * The to-be-detached segment must be currently attached with
-     * {@code address} equal to the value returned by the attaching
-     * attach() call.
+     * by the attaching attach() call.
      */
-    public abstract void detach(Pointer addr)
+    public abstract void detach()
         throws SystemException;
 
     /**

Modified: 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/Posix.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/Posix.java?rev=1095274&r1=1095273&r2=1095274&view=diff
==============================================================================
--- 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/Posix.java
 (original)
+++ 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/Posix.java
 Wed Apr 20 05:51:20 2011
@@ -15,6 +15,7 @@
  */
 package org.apache.commons.runtime.platform.unix;
 
+import org.apache.commons.runtime.Pointer;
 import org.apache.commons.runtime.Status;
 import org.apache.commons.runtime.AlreadyExistsException;
 import org.apache.commons.runtime.NoSuchObjectException;
@@ -79,5 +80,9 @@ final class Posix
                                              int flags, long offset);
     public static native int            munmap(long addr, long length);
     public static native int            msync(long addr, long length, int 
flags);
-    
+
+    public static final int HEAP_PTR            = 1;
+    public static final int SLICE_PTR           = 2;
+    public static final int CONST_PTR           = 3;
+    public static native Pointer        pointer(long addr, long length, int 
type);
 }

Modified: 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/PosixShm.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/PosixShm.java?rev=1095274&r1=1095273&r2=1095274&view=diff
==============================================================================
--- 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/PosixShm.java
 (original)
+++ 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/PosixShm.java
 Wed Apr 20 05:51:20 2011
@@ -49,7 +49,8 @@ final class PosixShm extends Shm
                NoSuchObjectException,
                SystemException;
     
-    private static native Pointer shmat0(long addr, long size, int fd, int 
flags);
+    private static native Pointer shmat0(long addr, long size, int fd, int 
flags)
+        throws SystemException;
     private static native int     shmdt0(long addr);
     private static native int     unlink0(int fd, String name);
 
@@ -88,42 +89,48 @@ final class PosixShm extends Shm
         owner = false;
     }
 
-    public Pointer attach(long address, boolean readOnly)
+    public Pointer attach(long addr, boolean readOnly)
         throws SystemException
     {
         if (fd == -1)
             throw new ClosedDescriptorException();
-        int flags = readOnly ? RDONLY : 0;
-        Pointer p = shmat0(address, size, fd, flags);
-        if (base == null)
-            base = p;
-        return p;
+        synchronized (this) {
+            if (base != null) {
+                // TODO: Should we throw an exception here
+                return base;
+            }
+            int flags = readOnly ? RDONLY : 0;
+            base = shmat0(addr, size, fd, flags);
+        }
+        return base;
     }
 
-    public void detach(Pointer addr)
+    public void detach()
         throws SystemException
     {
         if (fd == -1)
             throw new ClosedDescriptorException();
-        int rc = shmdt0(addr.address());
-        if (rc != 0)
-            throw new SystemException(Status.describe(rc));
-        if (addr.equals(base))
-            base = null;
+        synchronized (this) {
+            if (base != null) {
+                int rc = shmdt0(base.address());
+                if (rc != 0)
+                    throw new SystemException(Status.describe(rc));
+                base = null;
+            }
+        }
     }
 
     public void close()
         throws SystemException
     {
-        int rc = 0;
         if (fd == -1)
             throw new ClosedDescriptorException();
-        if (base != null)
-            rc = shmdt0(base.address());
-        if (owner)
-            rc = unlink0(fd, name);
-        if (rc != 0)
-            throw new SystemException(Status.describe(rc));
+        detach();
+        if (owner) {
+            int rc = unlink0(fd, name);
+            if (rc != 0)
+                throw new SystemException(Status.describe(rc));
+        }
         fd = -1;
     }
     

Modified: 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/Win32.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/Win32.java?rev=1095274&r1=1095273&r2=1095274&view=diff
==============================================================================
--- 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/Win32.java
 (original)
+++ 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/Win32.java
 Wed Apr 20 05:51:20 2011
@@ -16,6 +16,7 @@
 
 package org.apache.commons.runtime.platform.windows;
 
+import org.apache.commons.runtime.Pointer;
 import org.apache.commons.runtime.Status;
 import org.apache.commons.runtime.AlreadyExistsException;
 import org.apache.commons.runtime.NoSuchObjectException;
@@ -158,4 +159,10 @@ final class Win32
     public static native int        VirtualProtect(long addr, long size, int 
protect);
     public static native int        VirtualFree(long addr, long size, int 
type);
 
+
+    public static final int         HEAP_PTR                = 1;
+    public static final int         SLICE_PTR               = 2;
+    public static final int         CONST_PTR               = 3;
+    public static native Pointer    pointer(long addr, long length, int type);
+    
 }

Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/shmem.c
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/shmem.c?rev=1095274&r1=1095273&r2=1095274&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/shmem.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/shmem.c Wed Apr 20 
05:51:20 2011
@@ -167,23 +167,22 @@ cleanup:
 ACR_UNX_EXPORT(jobject, PosixShm, shmat0)(JNI_STDARGS, jlong addr, jlong size,
                                           jint fd, jint flags)
 {
-    void *sa = J2P(addr, void *);
-    void *sm;
+    void   *sa = J2P(addr, void *);
+    void   *sm;
+    size_t len = (size_t)size;
 
     if (flags == 1)
         flags = SHM_RDONLY;
     if ((sm = shmat(fd, sa, flags)) == (void *)-1) {
-        ACR_SAVE_OS_ERROR();
-        sm = 0;
+        ACR_THROW_SYS_ERROR();
+        return 0;        
     }
-    if (sm != 0) {
-        size_t len = (size_t)size;
+    else {
         if (flags)
             return AcrNewConstPointer(env, sm, len);
         else
             return AcrNewSlicePointer(env, sm, len);
     }
-    return 0;
 }
 
 ACR_UNX_EXPORT(jint, PosixShm, shmdt0)(JNI_STDARGS, jlong addr)

Modified: commons/sandbox/runtime/trunk/src/main/native/shared/pointer.c
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/pointer.c?rev=1095274&r1=1095273&r2=1095274&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/pointer.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/pointer.c Wed Apr 20 
05:51:20 2011
@@ -157,3 +157,27 @@ AcrSetPointerEx(JNI_STDARGS, void *val, 
     SET_IFIELD_P(0001, obj, len);
     return 0;
 }
+
+#if defined(WINDOWS)
+ACR_WIN_EXPORT(jobject, Win32,  pointer)
+#else
+ACR_UNX_EXPORT(jobject, Posix,  pointer)
+#endif
+(JNI_STDARGS, jlong addr, jlong length, jint type)
+{
+    void *memptr = J2P(addr, void *);
+    size_t size  = (size_t)length;
+
+    switch (type) {
+        case 1:
+            return AcrNewHeapPointer(env, memptr, size);
+        break;
+        case 2:
+            return AcrNewSlicePointer(env, memptr, size);
+        break;
+        case 3:
+            return AcrNewConstPointer(env, memptr, size);
+        break;
+    }
+    return 0;
+}


Reply via email to