diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2021-12-03 17:29:55 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-12-03 17:29:55 -0800 |
| commit | 2a0adef583d96d87fdca1bc536269931486e4349 (patch) | |
| tree | 039f0a872b51d50bf9633c45ba280fa94e5da187 /lib | |
| parent | 36b6e95aa3638fa8449526f68e46bc86d4b22e5c (diff) | |
| parent | e376fab186e7ff94808fd0cd88161f7345211a07 (diff) | |
| download | zig-2a0adef583d96d87fdca1bc536269931486e4349.tar.gz zig-2a0adef583d96d87fdca1bc536269931486e4349.zip | |
Merge pull request #9910 from mikdusan/dragonfly
dragonfly: port Thread.setname/getname
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/std/Thread.zig | 43 | ||||
| -rw-r--r-- | lib/std/c/dragonfly.zig | 3 |
2 files changed, 34 insertions, 12 deletions
diff --git a/lib/std/Thread.zig b/lib/std/Thread.zig index 855c44c032..31e54f00d8 100644 --- a/lib/std/Thread.zig +++ b/lib/std/Thread.zig @@ -39,6 +39,7 @@ pub const max_name_len = switch (target.os.tag) { .netbsd => 31, .freebsd => 15, .openbsd => 31, + .dragonfly => 1023, .solaris => 31, else => 0, }; @@ -82,13 +83,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); @@ -98,8 +98,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. @@ -127,9 +126,22 @@ 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); + switch (err) { + .SUCCESS => return, + .INVAL => unreachable, + .FAULT => unreachable, + .NAMETOOLONG => unreachable, // already checked + .SRCH => unreachable, + else => |e| return os.unexpectedErrno(e), + } }, - else => return error.Unsupported, + else => {}, } + return error.Unsupported; } pub const GetNameError = error{ @@ -179,9 +191,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); @@ -190,8 +200,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); @@ -217,8 +225,19 @@ 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); }, - else => return error.Unsupported, + .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; } /// Represents a unique ID per thread. 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 |
