-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #3345 - RalfJung:win-get-thread-name, r=RalfJung
Windows: support getting the thread name Also organize the thread name tests a bit.
- Loading branch information
Showing
11 changed files
with
145 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
use std::thread; | ||
|
||
fn main() { | ||
// When we have not set the name... | ||
thread::spawn(|| { | ||
assert!(thread::current().name().is_none()); | ||
}); | ||
|
||
// ... and when we have set it. | ||
thread::Builder::new() | ||
.name("childthread".to_string()) | ||
.spawn(move || { | ||
assert_eq!(thread::current().name().unwrap(), "childthread"); | ||
}) | ||
.unwrap() | ||
.join() | ||
.unwrap(); | ||
|
||
// Also check main thread name. | ||
assert_eq!(thread::current().name().unwrap(), "main"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
//! Panicking in other threads. | ||
use std::thread; | ||
|
||
fn panic() { | ||
let result = thread::spawn(|| panic!("Hello!")).join().unwrap_err(); | ||
let msg = result.downcast_ref::<&'static str>().unwrap(); | ||
assert_eq!(*msg, "Hello!"); | ||
} | ||
|
||
fn panic_named() { | ||
thread::Builder::new() | ||
.name("childthread".to_string()) | ||
.spawn(move || { | ||
panic!("Hello, world!"); | ||
}) | ||
.unwrap() | ||
.join() | ||
.unwrap_err(); | ||
} | ||
|
||
fn main() { | ||
panic(); | ||
panic_named(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
thread '<unnamed>' panicked at $DIR/thread_panic.rs:LL:CC: | ||
Hello! | ||
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace | ||
thread 'childthread' panicked at $DIR/thread_panic.rs:LL:CC: | ||
Hello, world! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
//@only-target-windows: this directly tests windows-only functions | ||
|
||
use std::ffi::OsStr; | ||
use std::os::windows::ffi::OsStrExt; | ||
|
||
use core::ffi::c_void; | ||
type HANDLE = *mut c_void; | ||
type PWSTR = *mut u16; | ||
type PCWSTR = *const u16; | ||
type HRESULT = i32; | ||
type HLOCAL = *mut ::core::ffi::c_void; | ||
extern "system" { | ||
fn GetCurrentThread() -> HANDLE; | ||
fn GetThreadDescription(hthread: HANDLE, lpthreaddescription: *mut PWSTR) -> HRESULT; | ||
fn SetThreadDescription(hthread: HANDLE, lpthreaddescription: PCWSTR) -> HRESULT; | ||
fn LocalFree(hmem: HLOCAL) -> HLOCAL; | ||
} | ||
|
||
fn to_u16s<S: AsRef<OsStr>>(s: S) -> Vec<u16> { | ||
let mut result: Vec<_> = s.as_ref().encode_wide().collect(); | ||
result.push(0); | ||
result | ||
} | ||
|
||
fn main() { | ||
unsafe { | ||
let name = c"mythreadname"; | ||
|
||
let utf16 = to_u16s(name.to_str().unwrap()); | ||
SetThreadDescription(GetCurrentThread(), utf16.as_ptr()); | ||
|
||
let mut ptr = core::ptr::null_mut::<u16>(); | ||
let result = GetThreadDescription(GetCurrentThread(), &mut ptr); | ||
assert!(result >= 0); | ||
let name_gotten = String::from_utf16_lossy({ | ||
let mut len = 0; | ||
while *ptr.add(len) != 0 { | ||
len += 1; | ||
} | ||
core::slice::from_raw_parts(ptr, len) | ||
}); | ||
assert_eq!(name_gotten, name.to_str().unwrap()); | ||
let r = LocalFree(ptr.cast()); | ||
assert!(r.is_null()); | ||
} | ||
} |