-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[jnigen] Finalize interfaces in Java (dart-archive/jnigen#369)
- Loading branch information
1 parent
4f85ced
commit 95c8375
Showing
9 changed files
with
199 additions
and
87 deletions.
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
pkgs/jni/java/src/main/java/com/github/dart_lang/jni/PortCleaner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
package com.github.dart_lang.jni; | ||
|
||
import java.lang.ref.PhantomReference; | ||
import java.lang.ref.ReferenceQueue; | ||
|
||
/// A registry of Java objects with associated Dart resources that cleans up the | ||
/// resources after they get unreachable and collected by the garbage collector. | ||
/// | ||
/// A simple alternative to [java.lang.ref.Cleaner] which is only available in | ||
/// Android API level 33+. | ||
class PortCleaner { | ||
static { | ||
System.loadLibrary("dartjni"); | ||
} | ||
|
||
private final ReferenceQueue<Object> queue = new ReferenceQueue<>(); | ||
private final PortPhantom list = new PortPhantom(); | ||
|
||
private class PortPhantom extends PhantomReference<Object> { | ||
final long port; | ||
|
||
/// Form a linked list. | ||
PortPhantom prev = this, next = this; | ||
|
||
PortPhantom(Object referent, long port) { | ||
super(referent, queue); | ||
this.port = port; | ||
insert(); | ||
} | ||
|
||
/// Only used for the head of the list. | ||
PortPhantom() { | ||
super(null, null); | ||
this.port = 0; | ||
} | ||
|
||
void insert() { | ||
synchronized (list) { | ||
prev = list; | ||
next = list.next; | ||
next.prev = this; | ||
list.next = this; | ||
} | ||
} | ||
|
||
private void remove() { | ||
synchronized (list) { | ||
next.prev = prev; | ||
prev.next = next; | ||
prev = this; | ||
next = this; | ||
} | ||
} | ||
} | ||
|
||
PortCleaner() { | ||
// Only a single PortCleaner and therefore thread will be created. | ||
Thread thread = | ||
new Thread( | ||
() -> { | ||
while (true) { | ||
try { | ||
PortPhantom portPhantom = (PortPhantom) queue.remove(); | ||
portPhantom.remove(); | ||
if (portPhantom.port != 0) { | ||
clean(portPhantom.port); | ||
} | ||
} catch (Throwable e) { | ||
// Ignore. | ||
} | ||
} | ||
}, | ||
"PortCleaner"); | ||
thread.setDaemon(true); | ||
thread.start(); | ||
} | ||
|
||
/// Registers [obj] to be cleaned up later by sending a signal through [port]. | ||
void register(Object obj, long port) { | ||
new PortPhantom(obj, port); | ||
} | ||
|
||
private static native void clean(long port); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.