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