From d621f4322b351817b6fdb058b264adb329c54fdb Mon Sep 17 00:00:00 2001 From: Michael Dusan Date: Thu, 7 Oct 2021 14:31:05 -0400 Subject: [PATCH 1/2] dragonfly: port std.Thread.setname/getname --- lib/std/Thread.zig | 22 ++++++++++++++++++++++ lib/std/c/dragonfly.zig | 3 +++ 2 files changed, 25 insertions(+) diff --git a/lib/std/Thread.zig b/lib/std/Thread.zig index ae3a3e9d9e..1b91ab597b 100644 --- a/lib/std/Thread.zig +++ b/lib/std/Thread.zig @@ -41,6 +41,7 @@ pub const max_name_len = switch (target.os.tag) { .netbsd => 31, .freebsd => 15, .openbsd => 31, + .dragonfly => 1023, .solaris => 31, else => 0, }; @@ -130,6 +131,17 @@ pub fn setName(self: Thread, name: []const u8) SetNameError!void { std.c.pthread_set_name_np(self.getHandle(), name_with_terminator.ptr); }, + .dragonfly => if (use_pthreads) { + const err = std.c.pthread_setname_np(self.getHandle(), name_with_terminator.ptr); + switch (err) { + .SUCCESS => return, + .INVAL => unreachable, + .FAULT => unreachable, + .NAMETOOLONG => unreachable, // already checked + .SRCH => unreachable, + else => |e| return os.unexpectedErrno(e), + } + }, else => return error.Unsupported, } } @@ -219,6 +231,16 @@ pub fn getName(self: Thread, buffer_ptr: *[max_name_len:0]u8) GetNameError!?[]co std.c.pthread_get_name_np(self.getHandle(), buffer.ptr, max_name_len + 1); return std.mem.sliceTo(buffer, 0); }, + .dragonfly => if (use_pthreads) { + const err = std.c.pthread_getname_np(self.getHandle(), buffer.ptr, max_name_len + 1); + switch (err) { + .SUCCESS => return std.mem.sliceTo(buffer, 0), + .INVAL => unreachable, + .FAULT => unreachable, + .SRCH => unreachable, + else => |e| return os.unexpectedErrno(e), + } + }, else => return error.Unsupported, } } diff --git a/lib/std/c/dragonfly.zig b/lib/std/c/dragonfly.zig index a2b7e31b4f..737cb70f40 100644 --- a/lib/std/c/dragonfly.zig +++ b/lib/std/c/dragonfly.zig @@ -33,6 +33,9 @@ pub const pthread_attr_t = extern struct { // copied from freebsd pub const sem_t = ?*opaque {}; +pub extern "c" fn pthread_setname_np(thread: std.c.pthread_t, name: [*:0]const u8) E; +pub extern "c" fn pthread_getname_np(thread: std.c.pthread_t, name: [*:0]u8, len: usize) E; + // See: // - https://gitweb.dragonflybsd.org/dragonfly.git/blob/HEAD:/include/unistd.h // - https://gitweb.dragonflybsd.org/dragonfly.git/blob/HEAD:/sys/sys/types.h From e376fab186e7ff94808fd0cd88161f7345211a07 Mon Sep 17 00:00:00 2001 From: Michael Dusan Date: Thu, 7 Oct 2021 14:31:06 -0400 Subject: [PATCH 2/2] housekeeping: return error.Unsupported Return error at end of std.Thread.setName/getName to simplify flow-control. --- lib/std/Thread.zig | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/lib/std/Thread.zig b/lib/std/Thread.zig index 1b91ab597b..be38501405 100644 --- a/lib/std/Thread.zig +++ b/lib/std/Thread.zig @@ -85,13 +85,12 @@ pub fn setName(self: Thread, name: []const u8) SetNameError!void { defer file.close(); try file.writer().writeAll(name); + return; }, .windows => if (target.os.isAtLeast(.windows, .win10_rs1)) |res| { // SetThreadDescription is only available since version 1607, which is 10.0.14393.795 // See https://en.wikipedia.org/wiki/Microsoft_Windows_SDK - if (!res) { - return error.Unsupported; - } + if (!res) return error.Unsupported; var name_buf_w: [max_name_len:0]u16 = undefined; const length = try std.unicode.utf8ToUtf16Le(&name_buf_w, name); @@ -101,8 +100,7 @@ pub fn setName(self: Thread, name: []const u8) SetNameError!void { self.getHandle(), @ptrCast(os.windows.LPWSTR, &name_buf_w), ); - } else { - return error.Unsupported; + return; }, .macos, .ios, .watchos, .tvos => if (use_pthreads) { // There doesn't seem to be a way to set the name for an arbitrary thread, only the current one. @@ -130,6 +128,7 @@ pub fn setName(self: Thread, name: []const u8) SetNameError!void { // pthread_setname_np can return an error. std.c.pthread_set_name_np(self.getHandle(), name_with_terminator.ptr); + return; }, .dragonfly => if (use_pthreads) { const err = std.c.pthread_setname_np(self.getHandle(), name_with_terminator.ptr); @@ -142,8 +141,9 @@ pub fn setName(self: Thread, name: []const u8) SetNameError!void { else => |e| return os.unexpectedErrno(e), } }, - else => return error.Unsupported, + else => {}, } + return error.Unsupported; } pub const GetNameError = error{ @@ -193,9 +193,7 @@ pub fn getName(self: Thread, buffer_ptr: *[max_name_len:0]u8) GetNameError!?[]co .windows => if (target.os.isAtLeast(.windows, .win10_rs1)) |res| { // GetThreadDescription is only available since version 1607, which is 10.0.14393.795 // See https://en.wikipedia.org/wiki/Microsoft_Windows_SDK - if (!res) { - return error.Unsupported; - } + if (!res) return error.Unsupported; var name_w: os.windows.LPWSTR = undefined; try os.windows.GetThreadDescription(self.getHandle(), &name_w); @@ -204,8 +202,6 @@ pub fn getName(self: Thread, buffer_ptr: *[max_name_len:0]u8) GetNameError!?[]co const data_len = try std.unicode.utf16leToUtf8(buffer, std.mem.sliceTo(name_w, 0)); return if (data_len >= 1) buffer[0..data_len] else null; - } else { - return error.Unsupported; }, .macos, .ios, .watchos, .tvos => if (use_pthreads) { const err = std.c.pthread_getname_np(self.getHandle(), buffer.ptr, max_name_len + 1); @@ -241,8 +237,9 @@ pub fn getName(self: Thread, buffer_ptr: *[max_name_len:0]u8) GetNameError!?[]co else => |e| return os.unexpectedErrno(e), } }, - else => return error.Unsupported, + else => {}, } + return error.Unsupported; } /// Represents a unique ID per thread.