Direct-BT  2.3.1
Direct-BT - Direct Bluetooth Programming.
DBTNativeDownlink.java
Go to the documentation of this file.
1 /**
2  * Author: Sven Gothel <sgothel@jausoft.com>
3  * Copyright (c) 2020 Gothel Software e.K.
4  * Copyright (c) 2020 ZAFENA AB
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25 
26 package jau.direct_bt;
27 
28 import java.util.concurrent.atomic.AtomicBoolean;
29 
30 import org.direct_bt.BTFactory;
31 
32 public abstract class DBTNativeDownlink
33 {
34  private long nativeInstance;
35  private final AtomicBoolean isValid = new AtomicBoolean(false);
36  private final Object nativeLock = new Object();
37 
38  static {
40  }
41 
42  protected DBTNativeDownlink(final long nativeInstance)
43  {
44  this.nativeInstance = nativeInstance;
45  isValid.set(true);
46  initNativeJavaObject(nativeInstance);
47  }
48 
49  protected boolean isValid() { return isValid.get(); }
50 
51  @Override
52  protected void finalize()
53  {
54  delete();
55  }
56 
57  /**
58  * Deletes the {@code nativeInstance} in the following order
59  * <ol>
60  * <li>Removes this java reference from the {@code nativeInstance}</li>
61  * <li>Deletes the {@code nativeInstance} via {@link #deleteImpl(long)}</li>
62  * <li>Zeros the {@code nativeInstance} reference</li>
63  * </ol>
64  */
65  public final void delete() {
66  synchronized (nativeLock) {
67  if( !isValid.compareAndSet(true, false) ) {
68  if( DBTManager.DEBUG ) {
69  System.err.println("JAVA: delete: !valid -> bail: "+getClass().getSimpleName());
70  }
71  return;
72  }
73  if( DBTManager.DEBUG ) {
74  System.err.println("JAVA: delete.0: "+getClass().getSimpleName()+": valid, handle 0x"+Long.toHexString(nativeInstance));
75  }
76  final long _nativeInstance = nativeInstance;
77  nativeInstance = 0;
78  deleteNativeJavaObject(_nativeInstance); // will issue notifyDeleted() itself!
79  deleteImpl(_nativeInstance);
80  if( DBTManager.DEBUG ) {
81  System.err.println("JAVA: delete.X: "+getClass().getSimpleName()+": handle 0x"+Long.toHexString(nativeInstance));
82  }
83  }
84  }
85 
86  /**
87  * Called from native JavaUplink dtor -> JavaGlobalObj dtor,
88  * i.e. native instance destructed in native land.
89  */
90  private final void notifyDeleted() {
91  synchronized (nativeLock) {
92  final boolean _isValid = isValid.get();
93  final long _nativeInstance = nativeInstance;
94  isValid.set(false);
95  nativeInstance = 0;
96  if( DBTManager.DEBUG ) {
97  System.err.println("JAVA: delete.notifyDeleted: "+getClass().getSimpleName()+", was: valid "+_isValid+", handle 0x"+Long.toHexString(_nativeInstance)+": "+toString());
98  }
99  }
100  }
101 
102  /**
103  * Deletes the native instance.
104  * <p>
105  * Called via {@link #delete()} and at this point
106  * <ul>
107  * <li>this java reference has been removed from the native instance, i.e. {@code JavaUplink}'s {@code javaObjectRef = nullptr}</li>
108  * <li>the {@link #nativeInstance} reference has been zeroed, but passed as argument for this final native deletion task.</li>
109  * </ul>
110  * </p>
111  * @param nativeInstance copy of {@link #nativeInstance} reference, which has been already zeroed.
112  */
113  protected abstract void deleteImpl(long nativeInstance);
114 
115  private native void initNativeJavaObject(final long nativeInstance);
116  private native void deleteNativeJavaObject(final long nativeInstance);
117 }
org.direct_bt
Author: Sven Gothel sgothel@jausoft.com Copyright (c) 2020 Gothel Software e.K.
org.direct_bt.BTFactory.checkInitialized
static synchronized void checkInitialized()
Definition: BTFactory.java:215
org.direct_bt.BTFactory
One stop BTManager API entry point.
Definition: BTFactory.java:52
jau.direct_bt.DBTManager
Definition: DBTManager.java:51
jau.direct_bt.DBTManager.DEBUG
static final boolean DEBUG
Definition: DBTManager.java:52
org