cassandra-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bened...@apache.org
Subject [1/3] cassandra git commit: Safer Resource Management++
Date Wed, 11 Feb 2015 15:36:35 GMT
Repository: cassandra
Updated Branches:
  refs/heads/cassandra-2.1 857ee0ac3 -> 207751c8a
  refs/heads/trunk 96866ce53 -> d73e7db80


Safer Resource Management++

patch by benedict; reviewed by marcus for CASSANDRA-8707


Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo
Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/207751c8
Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/207751c8
Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/207751c8

Branch: refs/heads/cassandra-2.1
Commit: 207751c8ac9800b7e004ec72096c478a30aafebc
Parents: 857ee0a
Author: Benedict Elliott Smith <benedict@apache.org>
Authored: Wed Feb 11 15:35:50 2015 +0000
Committer: Benedict Elliott Smith <benedict@apache.org>
Committed: Wed Feb 11 15:35:50 2015 +0000

----------------------------------------------------------------------
 .../utils/concurrent/SharedCloseable.java       | 35 ++++++++++++++
 .../utils/concurrent/SharedCloseableImpl.java   | 47 ++++++++++++++++++
 .../concurrent/WrappedSharedCloseable.java      | 51 ++++++++++++++++++++
 3 files changed, 133 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cassandra/blob/207751c8/src/java/org/apache/cassandra/utils/concurrent/SharedCloseable.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/utils/concurrent/SharedCloseable.java b/src/java/org/apache/cassandra/utils/concurrent/SharedCloseable.java
new file mode 100644
index 0000000..1e5a026
--- /dev/null
+++ b/src/java/org/apache/cassandra/utils/concurrent/SharedCloseable.java
@@ -0,0 +1,35 @@
+/*
+* 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.cassandra.utils.concurrent;
+
+/**
+ * A simple extension of AutoCloseable, that represents a resource that can be utilised in
multiple locations,
+ * each managing their own closure of the resource, so that when the last such instance is
closed all are.
+ *
+ */
+public interface SharedCloseable extends AutoCloseable
+{
+    /**
+     * @return a new instance of the object representing the same state and backed by the
same underlying resources.
+     * Coordinates with the original (and other instances) when the underlying resource should
be closed.
+     * Throws an exception if the shared resource has already been closed.
+     */
+    public SharedCloseable sharedCopy();
+
+}

http://git-wip-us.apache.org/repos/asf/cassandra/blob/207751c8/src/java/org/apache/cassandra/utils/concurrent/SharedCloseableImpl.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/utils/concurrent/SharedCloseableImpl.java b/src/java/org/apache/cassandra/utils/concurrent/SharedCloseableImpl.java
new file mode 100644
index 0000000..0d3a843
--- /dev/null
+++ b/src/java/org/apache/cassandra/utils/concurrent/SharedCloseableImpl.java
@@ -0,0 +1,47 @@
+/*
+* 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.cassandra.utils.concurrent;
+
+/**
+ * A simple abstract implementation of SharedCloseable
+ */
+public abstract class SharedCloseableImpl implements SharedCloseable
+{
+    final Ref<?> ref;
+
+    public SharedCloseableImpl(RefCounted.Tidy tidy)
+    {
+        ref = new Ref<Object>(null, tidy);
+    }
+
+    protected SharedCloseableImpl(SharedCloseableImpl copy)
+    {
+        this.ref = copy.ref.ref();
+    }
+
+    public boolean isCleanedUp()
+    {
+        return ref.globalCount() == 0;
+    }
+
+    public void close()
+    {
+        ref.ensureReleased();
+    }
+}

http://git-wip-us.apache.org/repos/asf/cassandra/blob/207751c8/src/java/org/apache/cassandra/utils/concurrent/WrappedSharedCloseable.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/utils/concurrent/WrappedSharedCloseable.java b/src/java/org/apache/cassandra/utils/concurrent/WrappedSharedCloseable.java
new file mode 100644
index 0000000..c656f28
--- /dev/null
+++ b/src/java/org/apache/cassandra/utils/concurrent/WrappedSharedCloseable.java
@@ -0,0 +1,51 @@
+/*
+* 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.cassandra.utils.concurrent;
+
+/**
+ * An implementation of SharedCloseable that wraps a normal AutoCloseable,
+ * ensuring its close method is only called when all instances of SharedCloseable have been
+ */
+public abstract class WrappedSharedCloseable extends SharedCloseableImpl
+{
+    final AutoCloseable wrapped;
+
+    public WrappedSharedCloseable(final AutoCloseable closeable)
+    {
+        super(new RefCounted.Tidy()
+        {
+            public void tidy() throws Exception
+            {
+                closeable.close();
+            }
+
+            public String name()
+            {
+                return closeable.toString();
+            }
+        });
+        wrapped = closeable;
+    }
+
+    protected WrappedSharedCloseable(WrappedSharedCloseable copy)
+    {
+        super(copy);
+        wrapped = copy.wrapped;
+    }
+}


Mime
View raw message