Skip to content

Commit c2f467c

Browse files
committed
Name spawned threads
Name spawned threads to make things more clear during debugging and profiling.
1 parent 98aef99 commit c2f467c

File tree

2 files changed

+42
-26
lines changed

2 files changed

+42
-26
lines changed

android-activity/src/game_activity/mod.rs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -925,21 +925,24 @@ pub unsafe extern "C" fn _rust_glue_entry(native_app: *mut ffi::android_app) {
925925
File::from_raw_fd(logpipe[0])
926926
};
927927

928-
thread::spawn(move || {
929-
let tag = CStr::from_bytes_with_nul(b"RustStdoutStderr\0").unwrap();
930-
let mut reader = BufReader::new(file);
931-
let mut buffer = String::new();
932-
loop {
933-
buffer.clear();
934-
if let Ok(len) = reader.read_line(&mut buffer) {
935-
if len == 0 {
936-
break;
937-
} else if let Ok(msg) = CString::new(buffer.clone()) {
938-
android_log(Level::Info, tag, &msg);
928+
thread::Builder::new()
929+
.name("android-stdio".to_string())
930+
.spawn(move || {
931+
let tag = CStr::from_bytes_with_nul(b"RustStdoutStderr\0").unwrap();
932+
let mut reader = BufReader::new(file);
933+
let mut buffer = String::new();
934+
loop {
935+
buffer.clear();
936+
if let Ok(len) = reader.read_line(&mut buffer) {
937+
if len == 0 {
938+
break;
939+
} else if let Ok(msg) = CString::new(buffer.clone()) {
940+
android_log(Level::Info, tag, &msg);
941+
}
939942
}
940943
}
941-
}
942-
});
944+
})
945+
.unwrap();
943946

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

957960
unsafe {
961+
// Name thread - this needs to happen here after attaching to a JVM thread,
962+
// since that changes the thread name to something like "Thread-2".
963+
let thread_name = CStr::from_bytes_with_nul(b"android-main\0").unwrap();
964+
libc::pthread_setname_np(libc::pthread_self(), thread_name.as_ptr());
965+
958966
let app = AndroidApp::from_ptr(NonNull::new(native_app).unwrap(), jvm.clone());
959967

960968
// We want to specifically catch any panic from the application's android_main

android-activity/src/native_activity/glue.rs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -845,21 +845,24 @@ extern "C" fn ANativeActivity_onCreate(
845845
File::from_raw_fd(logpipe[0])
846846
};
847847

848-
std::thread::spawn(move || {
849-
let tag = CStr::from_bytes_with_nul(b"RustStdoutStderr\0").unwrap();
850-
let mut reader = BufReader::new(file);
851-
let mut buffer = String::new();
852-
loop {
853-
buffer.clear();
854-
if let Ok(len) = reader.read_line(&mut buffer) {
855-
if len == 0 {
856-
break;
857-
} else if let Ok(msg) = CString::new(buffer.clone()) {
858-
android_log(Level::Info, tag, &msg);
848+
std::thread::Builder::new()
849+
.name("android-stdio".to_string())
850+
.spawn(move || {
851+
let tag = CStr::from_bytes_with_nul(b"RustStdoutStderr\0").unwrap();
852+
let mut reader = BufReader::new(file);
853+
let mut buffer = String::new();
854+
loop {
855+
buffer.clear();
856+
if let Ok(len) = reader.read_line(&mut buffer) {
857+
if len == 0 {
858+
break;
859+
} else if let Ok(msg) = CString::new(buffer.clone()) {
860+
android_log(Level::Info, tag, &msg);
861+
}
859862
}
860863
}
861-
}
862-
});
864+
})
865+
.unwrap();
863866

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

901904
unsafe {
905+
// Name thread - this needs to happen here after attaching to a JVM thread,
906+
// since that changes the thread name to something like "Thread-2".
907+
let thread_name = CStr::from_bytes_with_nul(b"android-main\0").unwrap();
908+
libc::pthread_setname_np(libc::pthread_self(), thread_name.as_ptr());
909+
902910
// We want to specifically catch any panic from the application's android_main
903911
// so we can finish + destroy the Activity gracefully via the JVM
904912
catch_unwind(|| {

0 commit comments

Comments
 (0)