]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/java/src/main/java/org/rocksdb/AbstractImmutableNativeReference.java
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / rocksdb / java / src / main / java / org / rocksdb / AbstractImmutableNativeReference.java
1 // Copyright (c) 2016, Facebook, Inc. All rights reserved.
2 // This source code is licensed under both the GPLv2 (found in the
3 // COPYING file in the root directory) and Apache 2.0 License
4 // (found in the LICENSE.Apache file in the root directory).
5
6 package org.rocksdb;
7
8 import java.util.concurrent.atomic.AtomicBoolean;
9
10 /**
11 * Offers functionality for implementations of
12 * {@link AbstractNativeReference} which have an immutable reference to the
13 * underlying native C++ object
14 */
15 //@ThreadSafe
16 public abstract class AbstractImmutableNativeReference
17 extends AbstractNativeReference {
18
19 /**
20 * A flag indicating whether the current {@code AbstractNativeReference} is
21 * responsible to free the underlying C++ object
22 */
23 protected final AtomicBoolean owningHandle_;
24
25 protected AbstractImmutableNativeReference(final boolean owningHandle) {
26 this.owningHandle_ = new AtomicBoolean(owningHandle);
27 }
28
29 @Override
30 public boolean isOwningHandle() {
31 return owningHandle_.get();
32 }
33
34 /**
35 * Releases this {@code AbstractNativeReference} from the responsibility of
36 * freeing the underlying native C++ object
37 * <p>
38 * This will prevent the object from attempting to delete the underlying
39 * native object in {@code close()}. This must be used when another object
40 * takes over ownership of the native object or both will attempt to delete
41 * the underlying object when closed.
42 * <p>
43 * When {@code disOwnNativeHandle()} is called, {@code close()} will
44 * subsequently take no action. As a result, incorrect use of this function
45 * may cause a memory leak.
46 * </p>
47 */
48 protected final void disOwnNativeHandle() {
49 owningHandle_.set(false);
50 }
51
52 @Override
53 public void close() {
54 if (owningHandle_.compareAndSet(true, false)) {
55 disposeInternal();
56 }
57 }
58
59 /**
60 * The helper function of {@link AbstractImmutableNativeReference#close()}
61 * which all subclasses of {@code AbstractImmutableNativeReference} must
62 * implement to release their underlying native C++ objects.
63 */
64 protected abstract void disposeInternal();
65 }