Skip to content

Commit 3ef792a

Browse files
authored
Rollup merge of #44374 - jsheard:threadname, r=alexcrichton
Implement named threads on Windows (v2) https://msdn.microsoft.com/en-us/library/windows/desktop/mt774976(v=vs.85).aspx Windows 10 version 1607 finally added a sensible API for naming threads, so we can now implement named threads without having to use MSVC compiler extensions like before. VS2017s debugger and the WPA profiler already use this API where available, but other tools may need some time to catch up. ![thread](https://user-images.githubusercontent.com/3153547/30133438-c92a3cda-934b-11e7-9668-915d53e8d860.png)
2 parents 9761a8d + 597ac36 commit 3ef792a

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

src/libstd/sys/windows/c.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub type DWORD = c_ulong;
3232
pub type HANDLE = LPVOID;
3333
pub type HINSTANCE = HANDLE;
3434
pub type HMODULE = HINSTANCE;
35+
pub type HRESULT = LONG;
3536
pub type BOOL = c_int;
3637
pub type BYTE = u8;
3738
pub type BOOLEAN = BYTE;
@@ -197,6 +198,8 @@ pub const ERROR_OPERATION_ABORTED: DWORD = 995;
197198
pub const ERROR_IO_PENDING: DWORD = 997;
198199
pub const ERROR_TIMEOUT: DWORD = 0x5B4;
199200

201+
pub const E_NOTIMPL: HRESULT = 0x80004001u32 as HRESULT;
202+
200203
pub const INVALID_HANDLE_VALUE: HANDLE = !0 as HANDLE;
201204

202205
pub const FACILITY_NT_BIT: DWORD = 0x1000_0000;
@@ -1163,8 +1166,8 @@ extern "system" {
11631166
timeout: *const timeval) -> c_int;
11641167
}
11651168

1166-
// Functions that aren't available on Windows XP, but we still use them and just
1167-
// provide some form of a fallback implementation.
1169+
// Functions that aren't available on every version of Windows that we support,
1170+
// but we still use them and just provide some form of a fallback implementation.
11681171
compat_fn! {
11691172
kernel32:
11701173

@@ -1182,6 +1185,10 @@ compat_fn! {
11821185
pub fn SetThreadStackGuarantee(_size: *mut c_ulong) -> BOOL {
11831186
SetLastError(ERROR_CALL_NOT_IMPLEMENTED as DWORD); 0
11841187
}
1188+
pub fn SetThreadDescription(hThread: HANDLE,
1189+
lpThreadDescription: LPCWSTR) -> HRESULT {
1190+
SetLastError(ERROR_CALL_NOT_IMPLEMENTED as DWORD); E_NOTIMPL
1191+
}
11851192
pub fn SetFileInformationByHandle(_hFile: HANDLE,
11861193
_FileInformationClass: FILE_INFO_BY_HANDLE_CLASS,
11871194
_lpFileInformation: LPVOID,

src/libstd/sys/windows/thread.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ use sys::handle::Handle;
1919
use sys_common::thread::*;
2020
use time::Duration;
2121

22+
use super::to_u16s;
23+
2224
pub const DEFAULT_MIN_STACK_SIZE: usize = 2 * 1024 * 1024;
2325

2426
pub struct Thread {
@@ -55,11 +57,12 @@ impl Thread {
5557
}
5658
}
5759

58-
pub fn set_name(_name: &CStr) {
59-
// Windows threads are nameless
60-
// The names in MSVC debugger are obtained using a "magic" exception,
61-
// which requires a use of MS C++ extensions.
62-
// See https://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx
60+
pub fn set_name(name: &CStr) {
61+
if let Ok(utf8) = name.to_str() {
62+
if let Ok(utf16) = to_u16s(utf8) {
63+
unsafe { c::SetThreadDescription(c::GetCurrentThread(), utf16.as_ptr()); };
64+
};
65+
};
6366
}
6467

6568
pub fn join(self) {

0 commit comments

Comments
 (0)