]> git.lizzy.rs Git - rust.git/commitdiff
Implement named threads on Windows
authorJoshua Sheard <mail@jsheard.com>
Wed, 6 Sep 2017 19:40:34 +0000 (20:40 +0100)
committerJoshua Sheard <mail@jsheard.com>
Wed, 6 Sep 2017 19:40:34 +0000 (20:40 +0100)
src/libstd/sys/windows/c.rs
src/libstd/sys/windows/thread.rs

index 7dfcc996e18e2e8202b7f6c061800ea005356204..9535ddfe5cada5143c22cee3dbf9a1f9a48ba84a 100644 (file)
@@ -32,6 +32,7 @@
 pub type HANDLE = LPVOID;
 pub type HINSTANCE = HANDLE;
 pub type HMODULE = HINSTANCE;
+pub type HRESULT = LONG;
 pub type BOOL = c_int;
 pub type BYTE = u8;
 pub type BOOLEAN = BYTE;
@@ -197,6 +198,8 @@ fn clone(&self) -> Self { *self }
 pub const ERROR_IO_PENDING: DWORD = 997;
 pub const ERROR_TIMEOUT: DWORD = 0x5B4;
 
+pub const E_NOTIMPL: HRESULT = 0x80004001u32 as HRESULT;
+
 pub const INVALID_HANDLE_VALUE: HANDLE = !0 as HANDLE;
 
 pub const FACILITY_NT_BIT: DWORD = 0x1000_0000;
@@ -1163,8 +1166,8 @@ pub fn select(nfds: c_int,
                   timeout: *const timeval) -> c_int;
 }
 
-// Functions that aren't available on Windows XP, but we still use them and just
-// provide some form of a fallback implementation.
+// Functions that aren't available on every version of Windows that we support,
+// but we still use them and just provide some form of a fallback implementation.
 compat_fn! {
     kernel32:
 
@@ -1182,6 +1185,10 @@ pub fn GetFinalPathNameByHandleW(_hFile: HANDLE,
     pub fn SetThreadStackGuarantee(_size: *mut c_ulong) -> BOOL {
         SetLastError(ERROR_CALL_NOT_IMPLEMENTED as DWORD); 0
     }
+    pub fn SetThreadDescription(hThread: HANDLE,
+                                lpThreadDescription: LPCWSTR) -> HRESULT {
+        SetLastError(ERROR_CALL_NOT_IMPLEMENTED as DWORD); E_NOTIMPL
+    }
     pub fn SetFileInformationByHandle(_hFile: HANDLE,
                     _FileInformationClass: FILE_INFO_BY_HANDLE_CLASS,
                     _lpFileInformation: LPVOID,
index 2cdd86e88b0a074eb87125792b9f888aeb9eaaa3..4043f6bc738f66401caab071b531a49936bc7fb8 100644 (file)
@@ -19,6 +19,8 @@
 use sys_common::thread::*;
 use time::Duration;
 
+use super::to_u16s;
+
 pub struct Thread {
     handle: Handle
 }
@@ -53,11 +55,12 @@ extern "system" fn thread_start(main: *mut c_void) -> c::DWORD {
         }
     }
 
-    pub fn set_name(_name: &CStr) {
-        // Windows threads are nameless
-        // The names in MSVC debugger are obtained using a "magic" exception,
-        // which requires a use of MS C++ extensions.
-        // See https://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx
+    pub fn set_name(name: &CStr) {
+        if let Ok(utf8) = name.to_str() {
+            if let Ok(utf16) = to_u16s(utf8) {
+                unsafe { c::SetThreadDescription(c::GetCurrentThread(), utf16.as_ptr()); };
+            };
+        };
     }
 
     pub fn join(self) {