Skip to content

Commit

Permalink
Name spawned threads
Browse files Browse the repository at this point in the history
Name spawned threads to make things more clear during debugging and
profiling.
  • Loading branch information
fornwall committed Nov 18, 2023
1 parent 98aef99 commit 83f0321
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 26 deletions.
34 changes: 21 additions & 13 deletions android-activity/src/game_activity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -925,21 +925,24 @@ pub unsafe extern "C" fn _rust_glue_entry(native_app: *mut ffi::android_app) {
File::from_raw_fd(logpipe[0])
};

thread::spawn(move || {
let tag = CStr::from_bytes_with_nul(b"RustStdoutStderr\0").unwrap();
let mut reader = BufReader::new(file);
let mut buffer = String::new();
loop {
buffer.clear();
if let Ok(len) = reader.read_line(&mut buffer) {
if len == 0 {
break;
} else if let Ok(msg) = CString::new(buffer.clone()) {
android_log(Level::Info, tag, &msg);
thread::Builder::new()
.name("android-stdio".to_string())
.spawn(move || {
let tag = CStr::from_bytes_with_nul(b"RustStdoutStderr\0").unwrap();
let mut reader = BufReader::new(file);
let mut buffer = String::new();
loop {
buffer.clear();
if let Ok(len) = reader.read_line(&mut buffer) {
if len == 0 {
break;
} else if let Ok(msg) = CString::new(buffer.clone()) {
android_log(Level::Info, tag, &msg);
}
}
}
}
});
})
.unwrap();

let jvm = unsafe {
let jvm = (*(*native_app).activity).vm;
Expand All @@ -955,6 +958,11 @@ pub unsafe extern "C" fn _rust_glue_entry(native_app: *mut ffi::android_app) {
};

unsafe {
// Name thread - this needs to happen here after attaching to a JVM thread,
// since that changes the thread name to something like "Thread-2".
let thread_name = CStr::from_bytes_with_nul(b"android-main\0").unwrap();
libc::pthread_setname_np(libc::pthread_self(), thread_name.unwrap().as_ptr());

Check failure on line 964 in android-activity/src/game_activity/mod.rs

View workflow job for this annotation

GitHub Actions / build (1.68.0)

no method named `unwrap` found for reference `&CStr` in the current scope

Check failure on line 964 in android-activity/src/game_activity/mod.rs

View workflow job for this annotation

GitHub Actions / build (stable)

no method named `unwrap` found for reference `&CStr` in the current scope

let app = AndroidApp::from_ptr(NonNull::new(native_app).unwrap(), jvm.clone());

// We want to specifically catch any panic from the application's android_main
Expand Down
34 changes: 21 additions & 13 deletions android-activity/src/native_activity/glue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -845,21 +845,24 @@ extern "C" fn ANativeActivity_onCreate(
File::from_raw_fd(logpipe[0])
};

std::thread::spawn(move || {
let tag = CStr::from_bytes_with_nul(b"RustStdoutStderr\0").unwrap();
let mut reader = BufReader::new(file);
let mut buffer = String::new();
loop {
buffer.clear();
if let Ok(len) = reader.read_line(&mut buffer) {
if len == 0 {
break;
} else if let Ok(msg) = CString::new(buffer.clone()) {
android_log(Level::Info, tag, &msg);
std::thread::Builder::new()
.name("android-stdio".to_string())
.spawn(move || {
let tag = CStr::from_bytes_with_nul(b"RustStdoutStderr\0").unwrap();
let mut reader = BufReader::new(file);
let mut buffer = String::new();
loop {
buffer.clear();
if let Ok(len) = reader.read_line(&mut buffer) {
if len == 0 {
break;
} else if let Ok(msg) = CString::new(buffer.clone()) {
android_log(Level::Info, tag, &msg);
}
}
}
}
});
})
.unwrap();

log::trace!(
"Creating: {:p}, saved_state = {:p}, save_state_size = {}",
Expand Down Expand Up @@ -899,6 +902,11 @@ extern "C" fn ANativeActivity_onCreate(
rust_glue.notify_main_thread_running();

unsafe {
// Name thread - this needs to happen here after attaching to a JVM thread,
// since that changes the thread name to something like "Thread-2".
let thread_name = CStr::from_bytes_with_nul(b"android-main\0").unwrap();
libc::pthread_setname_np(libc::pthread_self(), thread_name.as_ptr());

// We want to specifically catch any panic from the application's android_main
// so we can finish + destroy the Activity gracefully via the JVM
catch_unwind(|| {
Expand Down

0 comments on commit 83f0321

Please sign in to comment.