Throw UnsupportedOperationException if loading hexagon delegate shared library failed to load at startup.

PiperOrigin-RevId: 287900992
Change-Id: If6705db1a0ce842a792b193e681172ff2d7f001d
This commit is contained in:
Karim Nosir 2020-01-02 15:02:29 -08:00 committed by TensorFlower Gardener
parent 499a28d7fa
commit b4fd6a5963

View File

@ -23,6 +23,7 @@ public class HexagonDelegate implements Delegate, Closeable {
private static final long INVALID_DELEGATE_HANDLE = 0;
private static final String TFLITE_HEXAGON_LIB = "tensorflowlite_hexagon_jni";
private static volatile boolean nativeLibraryLoaded = false;
private long delegateHandle;
@ -32,6 +33,7 @@ public class HexagonDelegate implements Delegate, Closeable {
* on this device.
*/
public HexagonDelegate(Context context) throws UnsupportedOperationException {
ensureNativeLibraryLoaded();
setAdspLibraryPath(context.getApplicationInfo().nativeLibraryDir);
delegateHandle = createDelegate();
if (delegateHandle == INVALID_DELEGATE_HANDLE) {
@ -57,8 +59,16 @@ public class HexagonDelegate implements Delegate, Closeable {
}
}
static {
System.loadLibrary(TFLITE_HEXAGON_LIB);
private static void ensureNativeLibraryLoaded() {
if (nativeLibraryLoaded) {
return;
}
try {
System.loadLibrary(TFLITE_HEXAGON_LIB);
nativeLibraryLoaded = true;
} catch (Exception e) {
throw new UnsupportedOperationException("Failed to load native Hexagon shared library: " + e);
}
}
private static native long createDelegate();