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 2011/04/20 18:31:47 UTC
svn commit: r1095451 - in /commons/sandbox/runtime/trunk/src/main:
java/org/apache/commons/runtime/platform/windows/ native/os/win32/
test/org/apache/commons/runtime/
Author: mturk
Date: Wed Apr 20 16:31:47 2011
New Revision: 1095451
URL: http://svn.apache.org/viewvc?rev=1095451&view=rev
Log:
Implement win32 shared memory
Added:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsShm.java (with props)
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsShmImpl.java (with props)
Modified:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/Win32.java
commons/sandbox/runtime/trunk/src/main/native/os/win32/shmem.c
commons/sandbox/runtime/trunk/src/main/native/os/win32/winapi.c
commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestShm.java
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=1095451&r1=1095450&r2=1095451&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 16:31:47 2011
@@ -133,15 +133,16 @@ final class Win32
public static native long CreateFileMapping(long file, long sa, int protect, long size, String name);
public static native long OpenFileMapping(int acc, boolean inherit, String name);
public static native long MapViewOfFile(long handle, int acc, long offset, long size);
+ public static native int FlushViewOfFile(long address, long size);
public static native int UnmapViewOfFile(long address);
public static native long VirtualAlloc(long base, long size, int type, int protect);
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 final int HEAP_POINTER = 1;
+ public static final int SLICE_POINTER = 2;
+ public static final int CONST_POINTER = 3;
public static native Pointer pointer(long addr, long len, int type);
}
Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsShm.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsShm.java?rev=1095451&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsShm.java (added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsShm.java Wed Apr 20 16:31:47 2011
@@ -0,0 +1,183 @@
+/* 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.
+ */
+package org.apache.commons.runtime.platform.windows;
+
+import org.apache.commons.runtime.Errno;
+import org.apache.commons.runtime.Pointer;
+import org.apache.commons.runtime.Shm;
+import org.apache.commons.runtime.Status;
+import org.apache.commons.runtime.AlreadyExistsException;
+import org.apache.commons.runtime.NoSuchObjectException;
+import org.apache.commons.runtime.ClosedDescriptorException;
+import org.apache.commons.runtime.SystemException;
+
+/**
+ * WindowsShm class.
+ * <p>
+ * </p>
+ *
+ * @since Runtime 1.0
+ */
+final class WindowsShm extends Shm
+{
+
+ private WindowsShm()
+ {
+ // No Instance
+ }
+
+ private static native void hdrset0(long addr, long size);
+ private static native long hdrlen0(long addr);
+
+ // OS shmem descriptor
+ private long handle;
+ private long base;
+ private Pointer bptr;
+
+ public WindowsShm(final String name, long size)
+ throws IllegalAccessException,
+ IllegalArgumentException,
+ AlreadyExistsException,
+ SystemException
+ {
+ if (name == null)
+ throw new NullPointerException();
+ this.name = "Global\\" + name.replace('\\', '_');
+ this.size = size;
+ handle = Win32.CreateFileMapping(Win32.INVALID_HANDLE_VALUE,
+ 0L,
+ Win32.PAGE_READWRITE,
+ this.size + 16,
+ this.name);
+ if (handle == 0L) {
+ int rc = Errno.get();
+ if (rc == Errno.EEXIST)
+ throw new AlreadyExistsException();
+ else
+ throw new SystemException(Status.describe(rc));
+ }
+ base = Win32.MapViewOfFile(handle,
+ Win32.FILE_MAP_ALL_ACCESS,
+ 0L, this.size + 16);
+ if (base == 0L) {
+ Win32.CloseHandle(handle);
+ throw new SystemException(Errno.msg());
+ }
+ hdrset0(base, this.size);
+ owner = true;
+ }
+
+ public WindowsShm(final String name)
+ throws IllegalAccessException,
+ IllegalArgumentException,
+ NoSuchObjectException,
+ SystemException
+ {
+ if (name == null)
+ throw new NullPointerException();
+ this.name = "Global\\" + name.replace('\\', '_');
+ handle = Win32.OpenFileMapping(Win32.FILE_MAP_READ | Win32.FILE_MAP_WRITE,
+ false,
+ this.name);
+ if (handle == 0L) {
+ int rc = Errno.get();
+ if (Status.IS_ENOENT(rc))
+ throw new NoSuchObjectException();
+ else
+ throw new SystemException(Status.describe(rc));
+ }
+ long header = Win32.MapViewOfFile(handle, Win32.FILE_MAP_READ, 0L, 64);
+ if (header == 0L) {
+ Win32.CloseHandle(handle);
+ throw new SystemException(Errno.msg());
+ }
+ size = hdrlen0(header);
+ Win32.UnmapViewOfFile(header);
+ if (size == 0L) {
+ Win32.CloseHandle(handle);
+ throw new SystemException(Errno.msg());
+ }
+ base = Win32.MapViewOfFile(handle,
+ Win32.FILE_MAP_READ | Win32.FILE_MAP_WRITE,
+ 0L, size + 16);
+ if (base == 0L) {
+ Win32.CloseHandle(handle);
+ throw new SystemException(Errno.msg());
+ }
+ owner = false;
+ }
+
+ public Pointer attach(long addr, boolean readOnly)
+ throws SystemException
+ {
+ if (handle == 0L)
+ throw new ClosedDescriptorException();
+ synchronized (this) {
+ if (bptr != null) {
+ // TODO: Should we throw an exception here
+ return bptr;
+ }
+ if (readOnly)
+ bptr = Win32.pointer(base + 16, size, Win32.CONST_POINTER);
+ else
+ bptr = Win32.pointer(base + 16, size, Win32.SLICE_POINTER);
+ }
+ return bptr;
+ }
+
+ public void detach()
+ throws SystemException
+ {
+ if (handle == 0L)
+ throw new ClosedDescriptorException();
+ synchronized (this) {
+ if (base != 0L) {
+ int rc = Win32.UnmapViewOfFile(base);
+ if (rc != 0)
+ throw new SystemException(Status.describe(rc));
+ base = 0L;
+ bptr = null;
+ }
+ }
+ }
+
+ public void close()
+ throws SystemException
+ {
+ if (handle == 0L)
+ throw new ClosedDescriptorException();
+ detach();
+ handle = 0L;
+ }
+
+ /**
+ * Called by the garbage collector when the object is destroyed.
+ * The class will free internal resources allocated by the Operating system.
+ * @see Object#finalize()
+ * @throws Throwable the {@code Exception} raised by this method.
+ */
+ @Override
+ protected final void finalize()
+ throws Throwable
+ {
+ if (handle != 0L) {
+ if (base != 0L)
+ Win32.UnmapViewOfFile(base);
+ Win32.CloseHandle(handle);
+ }
+ }
+
+}
Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsShm.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsShmImpl.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsShmImpl.java?rev=1095451&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsShmImpl.java (added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsShmImpl.java Wed Apr 20 16:31:47 2011
@@ -0,0 +1,61 @@
+/* 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.
+ */
+package org.apache.commons.runtime.platform.windows;
+
+import org.apache.commons.runtime.Limits;
+import org.apache.commons.runtime.Shm;
+import org.apache.commons.runtime.ShmImpl;
+import org.apache.commons.runtime.AlreadyExistsException;
+import org.apache.commons.runtime.NoSuchObjectException;
+import org.apache.commons.runtime.SystemException;
+
+/**
+ * ShmImpl class.
+ * <p>
+ * </p>
+ *
+ * @since Runtime 1.0
+ */
+final class WindowsShmImpl extends ShmImpl
+{
+
+ public WindowsShmImpl()
+ {
+ // No Instance
+ }
+
+ public Shm create(String name, long size)
+ throws IllegalAccessException,
+ IllegalArgumentException,
+ AlreadyExistsException,
+ SystemException
+ {
+ if (size < Limits.SHMMIN || size > Limits.SHMMAX)
+ throw new IllegalArgumentException("Shared memory size is outside the limits.");
+ return new WindowsShm(name, size);
+ }
+
+ public Shm open(String name)
+ throws IllegalAccessException,
+ IllegalArgumentException,
+ NoSuchObjectException,
+ SystemException
+ {
+ return new WindowsShm(name);
+ }
+
+}
+
Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsShmImpl.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/shmem.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/shmem.c?rev=1095451&r1=1095450&r2=1095451&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/shmem.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/shmem.c Wed Apr 20 16:31:47 2011
@@ -45,7 +45,7 @@ ACR_JNI_EXPORT(jobject, ShmImpl, init0)(
if (_clazzn.u == 1)
return (*env)->NewObject(env, _clazzn.i, J4MID(0000));
if (AcrLoadClass(env, &_clazzn, 0) != 0) {
- ACR_THROW_MSG(ACR_EX_EINSTANCE, "WindowsShmemImpl not initialized");
+ ACR_THROW_MSG(ACR_EX_EINSTANCE, "WindowsShmImpl not initialized");
return 0;
}
R_LOAD_METHOD(0000, 0);
Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/winapi.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/winapi.c?rev=1095451&r1=1095450&r2=1095451&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/winapi.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/winapi.c Wed Apr 20 16:31:47 2011
@@ -110,6 +110,14 @@ ACR_WIN_EXPORT(jlong, Win32, MapViewOfFi
return P2J(m);
}
+ACR_WIN_EXPORT(jint, Win32, FlushViewOfFile)(JNI_STDARGS, jlong addr, jlong size)
+{
+ if (FlushViewOfFile(J2P(addr, LPVOID), (SIZE_T)size))
+ return 0;
+ else
+ return ACR_GET_OS_ERROR();
+}
+
ACR_WIN_EXPORT(jint, Win32, UnmapViewOfFile)(JNI_STDARGS, jlong addr)
{
if (UnmapViewOfFile(J2P(addr, LPVOID)))
Modified: commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestShm.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestShm.java?rev=1095451&r1=1095450&r2=1095451&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestShm.java (original)
+++ commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestShm.java Wed Apr 20 16:31:47 2011
@@ -37,7 +37,7 @@ public class TestShm extends Assert
} catch (Exception x) {
// Ignore
}
- System.out.println("[parent] Creating shared memory [" + Limits.SHMMIN + ".." + Limits.SHMMAX + "]" );
+ System.out.println("[parent] Creating shared memory" );
System.out.flush();
Semaphore s = Semaphore.create(semname, 0);
assertNotNull(s);
@@ -77,6 +77,7 @@ public class TestShm extends Assert
Shm m = Shm.open(shmname);
assertNotNull(m);
assertEquals(m.size(), shmsize);
+ System.out.println("[child] Shared memory size " + m.size());
assertNotNull(m.attach(0, false));
m.close();
System.out.println("[child] Done.");