-
Notifications
You must be signed in to change notification settings - Fork 24.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add missing Java files to RN fbjni sync
Reviewed By: mhorowitz Differential Revision: D5129224 fbshipit-source-id: d9fb5f95505f6be7d3d87ead67dbfaa951c03434
- Loading branch information
1 parent
b11dc39
commit f0e4a6c
Showing
5 changed files
with
226 additions
and
14 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
ReactAndroid/src/main/java/com/facebook/jni/CpuCapabilitiesJni.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,27 @@ | ||
// Copyright 2004-present Facebook. All Rights Reserved. | ||
|
||
package com.facebook.jni; | ||
|
||
import com.facebook.proguard.annotations.DoNotStrip; | ||
import com.facebook.soloader.SoLoader; | ||
|
||
/** | ||
* Utility class to determine CPU capabilities | ||
*/ | ||
@DoNotStrip | ||
public class CpuCapabilitiesJni { | ||
|
||
static { | ||
SoLoader.loadLibrary("fb"); | ||
} | ||
|
||
@DoNotStrip | ||
public static native boolean nativeDeviceSupportsNeon(); | ||
|
||
@DoNotStrip | ||
public static native boolean nativeDeviceSupportsVFPFP16(); | ||
|
||
@DoNotStrip | ||
public static native boolean nativeDeviceSupportsX86(); | ||
|
||
} |
132 changes: 132 additions & 0 deletions
132
ReactAndroid/src/main/java/com/facebook/jni/DestructorThread.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,132 @@ | ||
// Copyright 2004-present Facebook. All Rights Reserved. | ||
|
||
package com.facebook.jni; | ||
|
||
import java.lang.ref.PhantomReference; | ||
import java.lang.ref.ReferenceQueue; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
/** | ||
* A thread which invokes the "destruct" routine for objects after they have been garbage collected. | ||
* | ||
* An object which needs to be destructed should create a static subclass of {@link Destructor}. | ||
* Once the referent object is garbage collected, the DestructorThread will callback to the | ||
* {@link Destructor#destruct()} method. | ||
* | ||
* The underlying thread in DestructorThread starts when the first Destructor is constructed | ||
* and then runs indefinitely. | ||
*/ | ||
public class DestructorThread { | ||
|
||
/** | ||
* N.B The Destructor <b>SHOULD NOT</b> refer back to its referent object either explicitly or | ||
* implicitly (for example, as a non-static inner class). This will create a reference cycle where | ||
* the referent object will never be garbage collected. | ||
*/ | ||
public abstract static class Destructor extends PhantomReference<Object> { | ||
|
||
private Destructor next; | ||
private Destructor previous; | ||
|
||
Destructor(Object referent) { | ||
super(referent, sReferenceQueue); | ||
sDestructorStack.push(this); | ||
} | ||
|
||
private Destructor() { | ||
super(null, sReferenceQueue); | ||
} | ||
|
||
/** Callback which is invoked when the original object has been garbage collected. */ | ||
abstract void destruct(); | ||
} | ||
|
||
/** A list to keep all active Destructors in memory confined to the Destructor thread. */ | ||
private static DestructorList sDestructorList; | ||
/** A thread safe stack where new Destructors are placed before being add to sDestructorList. */ | ||
private static DestructorStack sDestructorStack; | ||
private static ReferenceQueue sReferenceQueue; | ||
private static Thread sThread; | ||
|
||
static { | ||
sDestructorStack = new DestructorStack(); | ||
sReferenceQueue = new ReferenceQueue(); | ||
sDestructorList = new DestructorList(); | ||
sThread = new Thread("HybridData DestructorThread") { | ||
@Override | ||
public void run() { | ||
while (true) { | ||
try { | ||
Destructor current = (Destructor) sReferenceQueue.remove(); | ||
current.destruct(); | ||
|
||
// If current is in the sDestructorStack, | ||
// transfer all the Destructors in the stack to the list. | ||
if (current.previous == null) { | ||
sDestructorStack.transferAllToList(); | ||
} | ||
|
||
DestructorList.drop(current); | ||
} catch (InterruptedException e) { | ||
// Continue. This thread should never be terminated. | ||
} | ||
} | ||
} | ||
}; | ||
|
||
sThread.start(); | ||
} | ||
|
||
private static class Terminus extends Destructor { | ||
@Override | ||
void destruct() { | ||
throw new IllegalStateException("Cannot destroy Terminus Destructor."); | ||
} | ||
} | ||
|
||
/** This is a thread safe, lock-free Treiber-like Stack of Destructors. */ | ||
private static class DestructorStack { | ||
private AtomicReference<Destructor> mHead = new AtomicReference<>(); | ||
|
||
public void push(Destructor newHead) { | ||
Destructor oldHead; | ||
do { | ||
oldHead = mHead.get(); | ||
newHead.next = oldHead; | ||
} while (!mHead.compareAndSet(oldHead, newHead)); | ||
} | ||
|
||
public void transferAllToList() { | ||
Destructor current = mHead.getAndSet(null); | ||
while (current != null) { | ||
Destructor next = current.next; | ||
sDestructorList.enqueue(current); | ||
current = next; | ||
} | ||
} | ||
} | ||
|
||
/** A doubly-linked list of Destructors. */ | ||
private static class DestructorList { | ||
private Destructor mHead; | ||
|
||
public DestructorList() { | ||
mHead = new Terminus(); | ||
mHead.next = new Terminus(); | ||
mHead.next.previous = mHead; | ||
} | ||
|
||
public void enqueue(Destructor current) { | ||
current.next = mHead.next; | ||
mHead.next = current; | ||
|
||
current.next.previous = current; | ||
current.previous = mHead; | ||
} | ||
|
||
private static void drop(Destructor current) { | ||
current.next.previous = current.previous; | ||
current.previous.next = current.next; | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
ReactAndroid/src/main/java/com/facebook/jni/HybridClassBase.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,8 @@ | ||
// Copyright 2004-present Facebook. All Rights Reserved. | ||
|
||
package com.facebook.jni; | ||
import com.facebook.proguard.annotations.DoNotStrip; | ||
|
||
@DoNotStrip | ||
public abstract class HybridClassBase extends HybridData { | ||
} |
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
15 changes: 15 additions & 0 deletions
15
ReactAndroid/src/main/java/com/facebook/jni/JniTerminateHandler.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,15 @@ | ||
// Copyright 2004-present Facebook. All Rights Reserved. | ||
|
||
package com.facebook.jni; | ||
|
||
public class JniTerminateHandler { | ||
public static void handleTerminate(Throwable t) throws Throwable { | ||
Thread.UncaughtExceptionHandler h = Thread.getDefaultUncaughtExceptionHandler(); | ||
if (h == null) { | ||
// Odd. Let the default std::terminate_handler deal with it. | ||
return; | ||
} | ||
h.uncaughtException(Thread.currentThread(), t); | ||
// That should exit. If it doesn't, let the default handler deal with it. | ||
} | ||
} |