-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Chore]: Fix Android bindings (#4095)
* [Chore]: Add GenericPhantomReference.java * [Chore]: Fix unnecessary null assertion in WalletCoreLibLoader.kt
- Loading branch information
1 parent
e41d66f
commit c61daac
Showing
3 changed files
with
56 additions
and
7 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package wallet.core.java; | ||
|
||
import java.lang.ref.PhantomReference; | ||
import java.lang.ref.ReferenceQueue; | ||
import java.util.Set; | ||
import java.util.HashSet; | ||
|
||
public class GenericPhantomReference extends PhantomReference<Object> { | ||
private final long nativeHandle; | ||
private final OnDeleteCallback onDeleteCallback; | ||
|
||
private static final Set<GenericPhantomReference> references = new HashSet<>(); | ||
private static final ReferenceQueue<Object> queue = new ReferenceQueue<>(); | ||
|
||
static { | ||
Thread finalizingDaemon = new Thread(() -> { | ||
try { | ||
doDeletes(); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
} | ||
}); | ||
finalizingDaemon.setName("WCFinalizingDaemon"); | ||
finalizingDaemon.setDaemon(true); | ||
finalizingDaemon.setPriority(Thread.NORM_PRIORITY); | ||
finalizingDaemon.start(); | ||
} | ||
|
||
private GenericPhantomReference(Object referent, long handle, OnDeleteCallback onDelete) { | ||
super(referent, queue); | ||
this.nativeHandle = handle; | ||
this.onDeleteCallback = onDelete; | ||
} | ||
|
||
public static void register(Object referent, long handle, OnDeleteCallback onDelete) { | ||
references.add(new GenericPhantomReference(referent, handle, onDelete)); | ||
} | ||
|
||
private static void doDeletes() throws InterruptedException { | ||
GenericPhantomReference ref = (GenericPhantomReference) queue.remove(); | ||
for (; ref != null; ref = (GenericPhantomReference) queue.remove()) { | ||
ref.onDeleteCallback.nativeDelete(ref.nativeHandle); | ||
references.remove(ref); | ||
} | ||
} | ||
|
||
@FunctionalInterface | ||
public interface OnDeleteCallback { | ||
void nativeDelete(long handle); | ||
} | ||
} |
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