1
0
mirror of https://github.com/ndarilek/tts-rs.git synced 2024-11-01 00:49:36 +00:00

Merge pull request #16 from AlyoshaVasilieva:exit-loop

Exit Android initialization loop with error when stuck
This commit is contained in:
Nolan Darilek 2022-01-10 10:43:57 -06:00 committed by GitHub
commit 89708d9ef1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,7 +4,7 @@ use std::ffi::{CStr, CString};
use std::os::raw::c_void; use std::os::raw::c_void;
use std::sync::{Mutex, RwLock}; use std::sync::{Mutex, RwLock};
use std::thread; use std::thread;
use std::time::Duration; use std::time::{Duration, Instant};
use jni::objects::{GlobalRef, JObject, JString}; use jni::objects::{GlobalRef, JObject, JString};
use jni::sys::{jfloat, jint, JNI_VERSION_1_6}; use jni::sys::{jfloat, jint, JNI_VERSION_1_6};
@ -198,12 +198,18 @@ impl Android {
} }
let tts = env.new_global_ref(tts)?; let tts = env.new_global_ref(tts)?;
// This hack makes my brain bleed. // This hack makes my brain bleed.
const MAX_WAIT_TIME: Duration = Duration::from_millis(500);
let start = Instant::now();
// Wait a max of 500ms for initialization, then return an error to avoid hanging.
loop { loop {
{ {
let pending = PENDING_INITIALIZATIONS.read().unwrap(); let pending = PENDING_INITIALIZATIONS.read().unwrap();
if !(*pending).contains(&bid) { if !(*pending).contains(&bid) {
break; break;
} }
if start.elapsed() > MAX_WAIT_TIME {
return Err(Error::OperationFailed);
}
} }
thread::sleep(Duration::from_millis(5)); thread::sleep(Duration::from_millis(5));
} }