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:57:51 UTC

svn commit: r1095459 - in /commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime: Shm.java io/Syncable.java platform/unix/PosixShm.java platform/windows/WindowsShm.java

Author: mturk
Date: Wed Apr 20 16:57:51 2011
New Revision: 1095459

URL: http://svn.apache.org/viewvc?rev=1095459&view=rev
Log:
Implement shmem sync and flush

Added:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Syncable.java   (with props)
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/PosixShm.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsShm.java

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=1095459&r1=1095458&r2=1095459&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 16:57:51 2011
@@ -15,6 +15,8 @@
  */
 package org.apache.commons.runtime;
 
+import org.apache.commons.runtime.io.Syncable;
+
 /**
  * Shm class.
  * <p>
@@ -22,7 +24,7 @@ package org.apache.commons.runtime;
  *
  * @since Runtime 1.0
  */
-public abstract class Shm
+public abstract class Shm implements Syncable
 {
 
     protected Shm()
@@ -104,6 +106,46 @@ public abstract class Shm
     }
 
     /**
+     * Force the underlying object to synchonize it's state with the storage
+     * device.
+     * <p>
+     * Sync does not necessarily syncronize it's meta data unless that metadata
+     * is needed in order to allow a subsequent data retrieval to be
+     * correctly handled. For example, changes to time of last  access and
+     * time of last modification do not not require flushing
+     * because they are not necessary for a subsequent data read to be
+     * handled correctly.  On the other hand, a change to the file size
+     * would require a metadata flush.
+     * </p>
+     * <p>
+     * The aim of {@code sync} is to reduce disk activity for applications
+     * that do not require all metadata to be synchronised with the disk.
+     * </p>
+     *
+     * @throws SyncFailedException when the object cannot be synced.
+     * @throws IOException if an I/O error occurs.
+     */
+    public abstract void sync()
+        throws SystemException;
+
+    /**
+     * Flushed the underlying object by writing any buffered data.
+     * <p>
+     * {@code fsync()} transfers  all  modified in-core data of the object
+     * referred to by {@code this} Descriptor to the disk device
+     * (or other permanent storage device)  where  that  object resides.
+     * The call blocks until the device reports that the transfer has
+     * completed.  It also flushes  metadata information associated with
+     * {@code this} Descriptor.
+     * </p>
+     *
+     * @throws SyncFailedException when the object cannot be flushed.
+     * @throws IOException if an I/O error occurs.
+     */
+    public abstract void flush()
+        throws SystemException;
+
+    /**
      * Closes the sharedmem.
      *
      */

Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Syncable.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Syncable.java?rev=1095459&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Syncable.java (added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Syncable.java Wed Apr 20 16:57:51 2011
@@ -0,0 +1,69 @@
+/*
+ * 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.io;
+
+import org.apache.commons.runtime.SystemException;
+
+/**
+ * Defines an interface for classes that can (or need to) be synced, typically
+ * before some output processing is considered to be finished and the object
+ * gets closed.
+ * <p>
+ * Unilike {@link java.io.Flushable} interface {@code flush}, the {@code sync}
+ * is usually used for flushing object's metadata to the output device.
+ *
+ */
+public interface Syncable
+{
+
+    /**
+     * Flush the underlying stream metadata.
+     * <p>
+     * {@code flush} transfers  all modified metadata of the stream object
+     * referred to by {@code this} stream to the disk device
+     * (or other storage device)  where  that  object resides.
+     * The call blocks until the device reports that the transfer has
+     * completed.
+     * </p>
+     *
+     * @throws SyncFailedException when the object cannot be flushed.
+     * @throws IOException if an I/O error occurs.
+     */
+    public void flush()
+        throws SystemException;
+
+
+    /**
+     * Sync the underlying stream by writing any buffered data.
+     * <p>
+     * {@code sync} transfers  all  modified in-core data of the stream object
+     * referred to by {@code this} stream to the disk device
+     * (or other storage device)  where  that  object resides.
+     * The call blocks until the device reports that the transfer has
+     * completed.  It also flushes  metadata information associated with
+     * {@code this} Descriptor.
+     * </p>
+     *
+     * @throws SyncFailedException when the object cannot be synced.
+     * @throws IOException if an I/O error occurs.
+     */
+    public void sync()
+        throws SystemException;
+
+}

Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Syncable.java
------------------------------------------------------------------------------
    svn:eol-style = native

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=1095459&r1=1095458&r2=1095459&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 16:57:51 2011
@@ -22,6 +22,7 @@ import org.apache.commons.runtime.Alread
 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.
@@ -56,7 +57,8 @@ final class PosixShm extends Shm
 
     // OS shmem descriptor
     private int     fd;
-    private Pointer base;
+    private long    base;
+    private Pointer bptr;
 
     public PosixShm(final String name, long size)
         throws IllegalAccessException,
@@ -87,41 +89,67 @@ final class PosixShm extends Shm
         owner = false;
     }
 
-    public Pointer attach(long addr, boolean readOnly)
+    @Override
+    public final Pointer attach(long addr, boolean readOnly)
         throws SystemException
     {
         if (fd == -1)
             throw new ClosedDescriptorException();
         synchronized (this) {
-            if (base != null) {
+            if (bptr != null) {
                 // TODO: Should we throw an exception here
-                return base;
+                return bptr;
             }
-            long badr = shmat0(addr, fd, readOnly);
+            base = shmat0(addr, fd, readOnly);
             if (readOnly)
-                base = Posix.pointer(badr, size, Posix.CONST_POINTER);
+                bptr = Posix.pointer(base, size, Posix.CONST_POINTER);
             else
-                base = Posix.pointer(badr, size, Posix.SLICE_POINTER);
+                bptr = Posix.pointer(base, size, Posix.SLICE_POINTER);
         }
-        return base;
+        return bptr;
     }
 
-    public void detach()
+    @Override
+    public final void detach()
         throws SystemException
     {
         if (fd == -1)
             throw new ClosedDescriptorException();
         synchronized (this) {
-            if (base != null) {
-                int rc = shmdt0(base.address());
+            if (base != 0L) {
+                int rc = shmdt0(base);
                 if (rc != 0)
                     throw new SystemException(Status.describe(rc));
-                base = null;
+                base = 0L;
+                bptr = null;
             }
         }
     }
 
-    public void close()
+    @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)
@@ -146,8 +174,8 @@ final class PosixShm extends Shm
         throws Throwable
     {
         if (fd != -1) {
-            if (base != null)
-                shmdt0(base.address());
+            if (base != 0L)
+                shmdt0(base);
             if (owner)
                 unlink0(fd, name);
         }

Modified: 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=1095459&r1=1095458&r2=1095459&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsShm.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsShm.java Wed Apr 20 16:57:51 2011
@@ -23,6 +23,7 @@ import org.apache.commons.runtime.Alread
 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;
 
 /**
  * WindowsShm class.
@@ -120,7 +121,8 @@ final class WindowsShm extends Shm
         owner = false;
     }
 
-    public Pointer attach(long addr, boolean readOnly)
+    @Override
+    public final Pointer attach(long addr, boolean readOnly)
         throws SystemException
     {
         if (handle == 0L)
@@ -138,7 +140,8 @@ final class WindowsShm extends Shm
         return bptr;
     }
 
-    public void detach()
+    @Override
+    public final void detach()
         throws SystemException
     {
         if (handle == 0L)
@@ -154,7 +157,30 @@ final class WindowsShm extends Shm
         }
     }
 
-    public void close()
+    @Override
+    public final void flush()
+        throws SystemException
+    {
+        if (handle == 0L)
+            throw new ClosedDescriptorException();
+        int rc = Win32.FlushViewOfFile(base, 0L);
+        if (rc != 0)
+            throw new SystemException(Status.describe(rc));
+    }
+
+    @Override
+    public final void sync()
+        throws SystemException
+    {
+        if (handle == 0L)
+            throw new ClosedDescriptorException();
+        int rc = Win32.FlushViewOfFile(base, 0L);
+        if (rc != 0)
+            throw new SystemException(Status.describe(rc));
+    }
+
+    @Override
+    public final void close()
         throws SystemException
     {
         if (handle == 0L)