Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement named threads on Windows (v2) #44374

Merged
merged 2 commits into from
Sep 15, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/libstd/sys/windows/c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub type DWORD = c_ulong;
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;
Expand Down Expand Up @@ -197,6 +198,8 @@ pub const ERROR_OPERATION_ABORTED: DWORD = 995;
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;
Expand Down Expand Up @@ -1163,8 +1166,8 @@ extern "system" {
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:

Expand All @@ -1182,6 +1185,10 @@ compat_fn! {
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,
Expand Down
13 changes: 8 additions & 5 deletions src/libstd/sys/windows/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ use sys::handle::Handle;
use sys_common::thread::*;
use time::Duration;

use super::to_u16s;

pub struct Thread {
handle: Handle
}
Expand Down Expand Up @@ -53,11 +55,12 @@ impl Thread {
}
}

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) {
Copy link
Member

@retep998 retep998 Sep 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems kinda silly that this function still takes 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) {
Expand Down