]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
java: clean-up in finalize()
authorNoah Watkins <noahwatkins@gmail.com>
Thu, 25 Oct 2012 21:43:09 +0000 (14:43 -0700)
committerNoah Watkins <noahwatkins@gmail.com>
Fri, 26 Oct 2012 20:58:20 +0000 (13:58 -0700)
Signed-off-by: Noah Watkins <noahwatkins@gmail.com>
src/java/java/com/ceph/fs/CephMount.java

index 8dea7f6872c3ad364d0868d19dce5b37025786bf..d9c6f46996354f9edaa1f944d7a45d28aaffd8dd 100644 (file)
@@ -89,6 +89,30 @@ public class CephMount {
    */
   static native void native_initialize();
 
+  /*
+   * Controls clean-up synchronization between the constructor and finalize().
+   * If native_ceph_create fails, then we want a call to finalize() to not
+   * attempt to clean-up native context, because there is none.
+   */
+  private boolean initialized = false;
+
+  /*
+   * Try to clean-up. First, unmount() will catch users who forget to do the
+   * unmount manually. Second, release() will destroy the entire context. It
+   * is safe to call release after a failure in unmount.
+   */
+  protected void finalize() throws Throwable {
+    if (initialized) {
+      try {
+        unmount();
+      } catch (Exception e) {}
+      try {
+        native_ceph_release(instance_ptr);
+      } catch (Exception e) {}
+    }
+    super.finalize();
+  }
+
   /**
    * Create a new CephMount with specific client id.
    *
@@ -96,6 +120,7 @@ public class CephMount {
    */
   public CephMount(String id) {
     native_ceph_create(this, id);
+    initialized = true;
   }
 
   private static synchronized native int native_ceph_create(CephMount mount, String id);