18
18
19
19
import java .lang .foreign .GroupLayout ;
20
20
import java .lang .foreign .MemorySegment ;
21
- import java .lang .ref .Cleaner ;
22
21
import java .util .concurrent .CountDownLatch ;
22
+ import java .util .concurrent .atomic .AtomicBoolean ;
23
23
24
24
public class AutoArenaTest {
25
25
26
-
27
26
@ Test
28
27
@ SuppressWarnings ("removal" ) // System.runFinalization() will be removed
29
28
public void cleaner_releases_native_resource () {
@@ -34,16 +33,21 @@ public void cleaner_releases_native_resource() {
34
33
35
34
// we're retaining the `object`, register it with the arena:
36
35
AutoSwiftMemorySession arena = (AutoSwiftMemorySession ) SwiftArena .ofAuto ();
37
- arena .register (object , new SwiftHeapObjectCleanup (object .$memorySegment (), object .$swiftType ()) {
38
- @ Override
39
- public void run () throws UnexpectedRetainCountException {
40
- cleanupLatch .countDown ();
41
- }
42
- });
36
+
37
+ var statusDestroyedFlag = object .$statusDestroyedFlag ();
38
+ Runnable markAsDestroyed = () -> statusDestroyedFlag .set (true );
39
+
40
+ arena .register (object ,
41
+ new SwiftHeapObjectCleanup (object .$memorySegment (), object .$swiftType (), markAsDestroyed ) {
42
+ @ Override
43
+ public void run () throws UnexpectedRetainCountException {
44
+ cleanupLatch .countDown ();
45
+ }
46
+ });
43
47
44
48
// Release the object and hope it gets GC-ed soon
45
49
46
- //noinspection UnusedAssignment
50
+ // noinspection UnusedAssignment
47
51
object = null ;
48
52
49
53
var i = 1_000 ;
@@ -76,5 +80,10 @@ public FakeSwiftHeapObject() {
76
80
public SwiftAnyType $swiftType () {
77
81
return null ;
78
82
}
83
+
84
+ @ Override
85
+ public AtomicBoolean $statusDestroyedFlag () {
86
+ return new AtomicBoolean ();
87
+ }
79
88
}
80
89
}
0 commit comments