zcash-android-wallet-sdk/sdk-lib/src/main/java/cash/z/ecc/android/sdk/jni/NativeLibraryLoader.kt

46 lines
1.4 KiB
Kotlin
Raw Normal View History

package cash.z.ecc.android.sdk.jni
import cash.z.ecc.android.sdk.internal.twig
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext
import java.util.concurrent.atomic.AtomicBoolean
/**
* Loads a native library once. This class is thread-safe.
*
* To use this class, create a singleton instance for each given [libraryName].
*
* @param libraryName Name of the library to load.
*/
internal class NativeLibraryLoader(private val libraryName: String) {
private val isLoaded = AtomicBoolean(false)
private val mutex = Mutex()
suspend fun load() {
// Double-checked locking to avoid the Mutex unless necessary, as the hot path is
// for the library to be loaded since this should only run once for the lifetime
// of the application
if (!isLoaded.get()) {
mutex.withLock {
if (!isLoaded.get()) {
loadRustLibrary()
}
}
}
}
[#366] Fix Detekt warnings * Disable baseline file. Too many functions. * CurrencyFormatter.kt suppress too many functions * PersistentTransactionManager.kt suppress too many functions * OutboundTransactionManager suppress too many functions * Suppress long parameter list * Too many functions * Add log to avoid empty block warning * Fix several magic number warnings * Solve max line length warnings * Solve max line length warnings * Suppress too long method warnings * Suppress too complex method warnings * Suppress large class warning * Fixed empty catch block * Changed directory path to the file * Fix too generic and swallowed exception * Fix print stack trace warning * Suppressed single top level file name declaration * Change parameters name * Suppress Spread operator warning * Remove unused private code * Add Locale to suppress default locale used warning * Solve several forbidden TODOs warnings * Fixed another max line length warning * Simplify return statement * Suppress class to object change * Make DemoConstants variables const * Use error() instead of throwing an IllegalStateException * Solve too complex condition * Suppress intentionally generic and swallowed exception * Suppress TooGenericExceptionCaught * Solve or suppress several TooGenericExceptionCaught * Fix swallowed exception * Suppress warning TooGenericExceptionCaught of PersistentTransactionManager * Suppress warning TooGenericExceptionCaught of WalletTransactionEncoder * Suppress TooGenericExceptionCaught of SdkSynchronizer * Suppress TooGenericExceptionCaught in SaplingParamTool * Suppress TooGenericExceptionCaught in CompactBlockDownloader * Suppress TooGenericExceptionCaught in CheckpointTool * Fix TooGenericExceptionCaught in WalletService * Suppress TooGenericExceptionCaught in DerivedDataDb * Suppress TooGenericExceptionCaught in CompactBlockProcessor * Apply ktlint format after all the previous changes * Remove detekt baseline file * Set Android studio right margin * Address comments from review * Suppress failing tests on CI
2022-08-23 06:49:00 -07:00
@Suppress("TooGenericExceptionCaught")
private suspend fun loadRustLibrary() {
try {
withContext(Dispatchers.IO) {
twig("Loading native library $libraryName") { System.loadLibrary(libraryName) }
}
isLoaded.set(true)
} catch (e: Throwable) {
twig("Error while loading native library: ${e.message}")
}
}
2021-11-18 04:10:30 -08:00
}