Skip to content

Commit e14edf6

Browse files
committed
std.Thread(windows): use NT internals for name fns
1 parent a71c6ae commit e14edf6

File tree

3 files changed

+39
-41
lines changed

3 files changed

+39
-41
lines changed

lib/std/Thread.zig

+39-23
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
const std = @import("std.zig");
66
const builtin = @import("builtin");
7+
const math = std.math;
78
const os = std.os;
89
const assert = std.debug.assert;
910
const target = builtin.target;
@@ -85,20 +86,28 @@ pub fn setName(self: Thread, name: []const u8) SetNameError!void {
8586
try file.writer().writeAll(name);
8687
return;
8788
},
88-
.windows => if (target.os.isAtLeast(.windows, .win10_rs1)) |res| {
89-
// SetThreadDescription is only available since version 1607, which is 10.0.14393.795
90-
// See https://en.wikipedia.org/wiki/Microsoft_Windows_SDK
91-
if (!res) return error.Unsupported;
92-
93-
var name_buf_w: [max_name_len:0]u16 = undefined;
94-
const length = try std.unicode.utf8ToUtf16Le(&name_buf_w, name);
95-
name_buf_w[length] = 0;
89+
.windows => {
90+
var buf: [max_name_len]u16 = undefined;
91+
const len = try std.unicode.utf8ToUtf16Le(&buf, name);
92+
const byte_len = math.cast(c_ushort, len * 2) catch return error.NameTooLong;
93+
94+
// Note: NT allocates its own copy, no use-after-free here.
95+
const unicode_string = os.windows.UNICODE_STRING{
96+
.Length = byte_len,
97+
.MaximumLength = byte_len,
98+
.Buffer = &buf,
99+
};
96100

97-
try os.windows.SetThreadDescription(
101+
switch (os.windows.ntdll.NtSetInformationThread(
98102
self.getHandle(),
99-
@ptrCast(os.windows.LPWSTR, &name_buf_w),
100-
);
101-
return;
103+
.ThreadNameInformation,
104+
&unicode_string,
105+
@sizeOf(os.windows.UNICODE_STRING),
106+
)) {
107+
.SUCCESS => {},
108+
.NOT_IMPLEMENTED => return error.Unsupported,
109+
else => |err| return os.windows.unexpectedStatus(err),
110+
}
102111
},
103112
.macos, .ios, .watchos, .tvos => if (use_pthreads) {
104113
// There doesn't seem to be a way to set the name for an arbitrary thread, only the current one.
@@ -188,18 +197,25 @@ pub fn getName(self: Thread, buffer_ptr: *[max_name_len:0]u8) GetNameError!?[]co
188197
// musl doesn't provide pthread_getname_np and there's no way to retrieve the thread id of an arbitrary thread.
189198
return error.Unsupported;
190199
},
191-
.windows => if (target.os.isAtLeast(.windows, .win10_rs1)) |res| {
192-
// GetThreadDescription is only available since version 1607, which is 10.0.14393.795
193-
// See https://en.wikipedia.org/wiki/Microsoft_Windows_SDK
194-
if (!res) return error.Unsupported;
195-
196-
var name_w: os.windows.LPWSTR = undefined;
197-
try os.windows.GetThreadDescription(self.getHandle(), &name_w);
198-
defer os.windows.LocalFree(name_w);
200+
.windows => {
201+
const buf_capacity = @sizeOf(os.windows.UNICODE_STRING) + (@sizeOf(u16) * max_name_len);
202+
var buf: [buf_capacity]u8 align(@alignOf(os.windows.UNICODE_STRING)) = undefined;
199203

200-
const data_len = try std.unicode.utf16leToUtf8(buffer, std.mem.sliceTo(name_w, 0));
201-
202-
return if (data_len >= 1) buffer[0..data_len] else null;
204+
switch (os.windows.ntdll.NtQueryInformationThread(
205+
self.getHandle(),
206+
.ThreadNameInformation,
207+
&buf,
208+
buf_capacity,
209+
null,
210+
)) {
211+
.SUCCESS => {
212+
const string = @ptrCast(*const os.windows.UNICODE_STRING, &buf);
213+
const len = try std.unicode.utf16leToUtf8(buffer, string.Buffer[0 .. string.Length / 2]);
214+
return if (len > 0) buffer[0..len] else null;
215+
},
216+
.NOT_IMPLEMENTED => return error.Unsupported,
217+
else => |err| return os.windows.unexpectedStatus(err),
218+
}
203219
},
204220
.macos, .ios, .watchos, .tvos => if (use_pthreads) {
205221
const err = std.c.pthread_getname_np(self.getHandle(), buffer.ptr, max_name_len + 1);

lib/std/os/windows.zig

-15
Original file line numberDiff line numberDiff line change
@@ -2029,21 +2029,6 @@ pub fn unexpectedStatus(status: NTSTATUS) std.os.UnexpectedError {
20292029
return error.Unexpected;
20302030
}
20312031

2032-
pub fn SetThreadDescription(hThread: HANDLE, lpThreadDescription: LPCWSTR) !void {
2033-
if (kernel32.SetThreadDescription(hThread, lpThreadDescription) == 0) {
2034-
switch (kernel32.GetLastError()) {
2035-
else => |err| return unexpectedError(err),
2036-
}
2037-
}
2038-
}
2039-
pub fn GetThreadDescription(hThread: HANDLE, ppszThreadDescription: *LPWSTR) !void {
2040-
if (kernel32.GetThreadDescription(hThread, ppszThreadDescription) == 0) {
2041-
switch (kernel32.GetLastError()) {
2042-
else => |err| return unexpectedError(err),
2043-
}
2044-
}
2045-
}
2046-
20472032
pub const Win32Error = @import("windows/win32error.zig").Win32Error;
20482033
pub const NTSTATUS = @import("windows/ntstatus.zig").NTSTATUS;
20492034
pub const LANG = @import("windows/lang.zig");

lib/std/os/windows/kernel32.zig

-3
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,3 @@ pub extern "kernel32" fn SleepConditionVariableSRW(
400400
pub extern "kernel32" fn TryAcquireSRWLockExclusive(s: *SRWLOCK) callconv(WINAPI) BOOLEAN;
401401
pub extern "kernel32" fn AcquireSRWLockExclusive(s: *SRWLOCK) callconv(WINAPI) void;
402402
pub extern "kernel32" fn ReleaseSRWLockExclusive(s: *SRWLOCK) callconv(WINAPI) void;
403-
404-
pub extern "kernel32" fn SetThreadDescription(hThread: HANDLE, lpThreadDescription: LPCWSTR) callconv(WINAPI) HRESULT;
405-
pub extern "kernel32" fn GetThreadDescription(hThread: HANDLE, ppszThreadDescription: *LPWSTR) callconv(WINAPI) HRESULT;

0 commit comments

Comments
 (0)