mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Add missing Java files to RN fbjni sync
Reviewed By: mhorowitz Differential Revision: D5129224 fbshipit-source-id: d9fb5f95505f6be7d3d87ead67dbfaa951c03434
This commit is contained in:
committed by
Facebook Github Bot
parent
b11dc39430
commit
f0e4a6cd2c
@@ -2,6 +2,8 @@
|
||||
|
||||
package com.facebook.jni;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import com.facebook.proguard.annotations.DoNotStrip;
|
||||
import com.facebook.soloader.SoLoader;
|
||||
|
||||
@@ -10,11 +12,9 @@ import com.facebook.soloader.SoLoader;
|
||||
*
|
||||
* NB: THREAD SAFETY
|
||||
*
|
||||
* {@link #dispose} deletes the corresponding native object on whatever thread
|
||||
* the method is called on. In the common case when this is called by
|
||||
* HybridData#finalize(), this will be called on the system finalizer
|
||||
* thread. If you manually call resetNative() on the Java object, the C++
|
||||
* object will be deleted synchronously on that thread.
|
||||
* {@link #resetNative} deletes the corresponding native object synchronously on whatever thread
|
||||
* the method is called on. Otherwise, deletion will occur on the {@link DestructorThread}
|
||||
* thread.
|
||||
*/
|
||||
@DoNotStrip
|
||||
public class HybridData {
|
||||
@@ -23,27 +23,57 @@ public class HybridData {
|
||||
SoLoader.loadLibrary("fb");
|
||||
}
|
||||
|
||||
// Private C++ instance
|
||||
@DoNotStrip
|
||||
private long mNativePointer = 0;
|
||||
private Destructor mDestructor = new Destructor(this);
|
||||
|
||||
/**
|
||||
* To explicitly delete the instance, call resetNative(). If the C++
|
||||
* instance is referenced after this is called, a NullPointerException will
|
||||
* be thrown. resetNative() may be called multiple times safely. Because
|
||||
* {@link #finalize} calls resetNative, the instance will not leak if this is
|
||||
* the {@link DestructorThread} also calls resetNative, the instance will not leak if this is
|
||||
* not called, but timing of deletion and the thread the C++ dtor is called
|
||||
* on will be at the whim of the Java GC. If you want to control the thread
|
||||
* and timing of the destructor, you should call resetNative() explicitly.
|
||||
*/
|
||||
public native void resetNative();
|
||||
|
||||
protected void finalize() throws Throwable {
|
||||
resetNative();
|
||||
super.finalize();
|
||||
public synchronized void resetNative() {
|
||||
mDestructor.destruct();
|
||||
}
|
||||
|
||||
/**
|
||||
* N.B. Thread safety.
|
||||
* If you call isValid from a different thread than {@link #resetNative()} then be sure to
|
||||
* do so while synchronizing on the hybrid. For example:
|
||||
* <pre><code>
|
||||
* synchronized(hybrid) {
|
||||
* if (hybrid.isValid) {
|
||||
* // Do stuff.
|
||||
* }
|
||||
* }
|
||||
* </code></pre>
|
||||
*/
|
||||
public boolean isValid() {
|
||||
return mNativePointer != 0;
|
||||
return mDestructor.mNativePointer != 0;
|
||||
}
|
||||
|
||||
public static class Destructor extends DestructorThread.Destructor {
|
||||
|
||||
// Private C++ instance
|
||||
@DoNotStrip
|
||||
private long mNativePointer;
|
||||
|
||||
Destructor(Object referent) {
|
||||
super(referent);
|
||||
}
|
||||
|
||||
@Override
|
||||
void destruct() {
|
||||
// When invoked from the DestructorThread instead of resetNative,
|
||||
// the DestructorThread has exclusive ownership of the HybridData
|
||||
// so synchronization is not necessary.
|
||||
deleteNative(mNativePointer);
|
||||
mNativePointer = 0;
|
||||
}
|
||||
|
||||
static native void deleteNative(long pointer);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user