Author: mturk
Date: Tue Apr 26 14:42:07 2011
New Revision: 1096768
URL: http://svn.apache.org/viewvc?rev=1096768&view=rev
Log:
Implement Posix shared memory
Added:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/PosixShm.java
(with props)
Added:
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=1096768&view=auto
==============================================================================
---
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/PosixShm.java
(added)
+++
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/PosixShm.java
Tue Apr 26 14:42:07 2011
@@ -0,0 +1,202 @@
+/* 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.unix;
+
+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.InvalidArgumentException;
+import org.apache.commons.runtime.NoSuchObjectException;
+import org.apache.commons.runtime.ClosedDescriptorException;
+import org.apache.commons.runtime.SystemException;
+import org.apache.commons.runtime.io.Syncable;
+
+/**
+ * PosixShm class.
+ * <p>
+ * </p>
+ *
+ * @since Runtime 1.0
+ */
+final class PosixShm extends Shm
+{
+
+ private PosixShm()
+ {
+ // No Instance
+ }
+
+ private static native int open0(String name, int flags, int mode)
+ throws InvalidArgumentException,
+ AlreadyExistsException,
+ NoSuchObjectException,
+ SystemException;
+ private static native int unlink0(String name);
+
+ // OS shmem descriptor
+ private int fd;
+ private long base;
+ private Pointer bptr;
+
+ private static String posixObjectName(final String name)
+ {
+ if (name.charAt(0) == '/')
+ return "/" + name.substring(1).replace('/', '_');
+ else
+ return "/" + name.replace('/', '_');
+ }
+
+ public PosixShm(final String name, long size)
+ throws InvalidArgumentException,
+ AlreadyExistsException,
+ SystemException
+ {
+ if (name == null)
+ throw new InvalidArgumentException("Shared memory name cannot be
null");
+ this.name = posixObjectName(name);
+ fd = open0(this.name, Posix.O_RDWR | Posix.O_CREAT | Posix.O_EXCL,
0644);
+ int rc = Posix.ftruncate(fd, size);
+ if (rc != 0) {
+ Posix.close(fd);
+ throw new SystemException(Status.describe(rc));
+ }
+ this.size = size;
+ owner = true;
+ }
+
+ public PosixShm(final String name)
+ throws InvalidArgumentException,
+ NoSuchObjectException,
+ SystemException
+ {
+ if (name == null)
+ throw new InvalidArgumentException("Shared memory name cannot be
null");
+ this.name = posixObjectName(name);
+ fd = open0(this.name, Posix.O_RDWR, 0);
+ long[] sb = new long[10];
+ int rc = Posix.fstat(fd, sb);
+ if (rc != 0) {
+ Posix.close(fd);
+ throw new SystemException(Status.describe(rc));
+ }
+ this.size = sb[6];
+ owner = false;
+ }
+
+ @Override
+ public final Pointer attach(long addr, boolean readOnly)
+ throws SystemException
+ {
+ if (fd == -1)
+ throw new ClosedDescriptorException();
+ synchronized (this) {
+ if (bptr != null) {
+ // TODO: Should we throw an exception here
+ return bptr;
+ }
+ int prot = Posix.PROT_READ;
+ if (!readOnly)
+ prot |= Posix.PROT_WRITE;
+ base = Posix.mmap(addr, size, prot, Posix.MAP_SHARED, fd, 0L);
+ if (base == 0L)
+ throw new SystemException(Errno.msg());
+ if (readOnly)
+ bptr = Posix.pointer(base, size, Posix.CONST_POINTER);
+ else
+ bptr = Posix.pointer(base, size, Posix.SLICE_POINTER);
+ }
+ return bptr;
+ }
+
+ @Override
+ public final void detach()
+ throws SystemException
+ {
+ if (fd == -1)
+ throw new ClosedDescriptorException();
+ synchronized (this) {
+ if (base != 0L) {
+ int rc = Posix.munmap(base, size);
+ if (rc != 0)
+ throw new SystemException(Status.describe(rc));
+ base = 0L;
+ bptr = null;
+ }
+ }
+ }
+
+ @Override
+ public final void flush()
+ throws SystemException
+ {
+ if (fd == -1)
+ throw new ClosedDescriptorException();
+ int rc = Posix.msync(base, size, Posix.MS_ASYNC);
+ if (rc != 0)
+ throw new SystemException(Status.describe(rc));
+ }
+
+ @Override
+ public final void sync()
+ throws SystemException
+ {
+ if (fd == -1)
+ throw new ClosedDescriptorException();
+ int rc = Posix.msync(base, size, Posix.MS_SYNC);
+ if (rc != 0)
+ throw new SystemException(Status.describe(rc));
+ }
+
+ @Override
+ public final void close()
+ throws SystemException
+ {
+ if (fd == -1)
+ throw new ClosedDescriptorException();
+ detach();
+ int rc = Posix.close(fd);
+ if (rc != 0)
+ throw new SystemException(Status.describe(rc));
+ if (owner) {
+ rc = unlink0(name);
+ if (rc != 0)
+ throw new SystemException(Status.describe(rc));
+ }
+ fd = -1;
+ }
+
+ /**
+ * 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 (fd != -1) {
+ if (base != 0L)
+ Posix.munmap(base, size);
+ Posix.close(fd);
+ if (owner)
+ unlink0(name);
+ }
+ }
+
+}
Propchange:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/PosixShm.java
------------------------------------------------------------------------------
svn:eol-style = native