authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-12-03 17:29:55-08:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2021-12-03 17:29:55-08:00
log2a0adef583d96d87fdca1bc536269931486e4349
tree039f0a872b51d50bf9633c45ba280fa94e5da187
parent36b6e95aa3638fa8449526f68e46bc86d4b22e5c
parente376fab186e7ff94808fd0cd88161f7345211a07
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #9910 from mikdusan/dragonfly

dragonfly: port Thread.setname/getname

2 files changed, 34 insertions(+), 12 deletions(-)

lib/std/Thread.zig+31-12
......@@ -39,6 +39,7 @@ pub const max_name_len = switch (target.os.tag) {
3939 .netbsd => 31,
4040 .freebsd => 15,
4141 .openbsd => 31,
42 .dragonfly => 1023,
4243 .solaris => 31,
4344 else => 0,
4445};
......@@ -82,13 +83,12 @@ pub fn setName(self: Thread, name: []const u8) SetNameError!void {
8283 defer file.close();
8384
8485 try file.writer().writeAll(name);
86 return;
8587 },
8688 .windows => if (target.os.isAtLeast(.windows, .win10_rs1)) |res| {
8789 // SetThreadDescription is only available since version 1607, which is 10.0.14393.795
8890 // See https://en.wikipedia.org/wiki/Microsoft_Windows_SDK
89 if (!res) {
90 return error.Unsupported;
91 }
91 if (!res) return error.Unsupported;
9292
9393 var name_buf_w: [max_name_len:0]u16 = undefined;
9494 const length = try std.unicode.utf8ToUtf16Le(&name_buf_w, name);
......@@ -98,8 +98,7 @@ pub fn setName(self: Thread, name: []const u8) SetNameError!void {
9898 self.getHandle(),
9999 @ptrCast(os.windows.LPWSTR, &name_buf_w),
100100 );
101 } else {
102 return error.Unsupported;
101 return;
103102 },
104103 .macos, .ios, .watchos, .tvos => if (use_pthreads) {
105104 // 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 {
127126 // pthread_setname_np can return an error.
128127
129128 std.c.pthread_set_name_np(self.getHandle(), name_with_terminator.ptr);
129 return;
130 },
131 .dragonfly => if (use_pthreads) {
132 const err = std.c.pthread_setname_np(self.getHandle(), name_with_terminator.ptr);
133 switch (err) {
134 .SUCCESS => return,
135 .INVAL => unreachable,
136 .FAULT => unreachable,
137 .NAMETOOLONG => unreachable, // already checked
138 .SRCH => unreachable,
139 else => |e| return os.unexpectedErrno(e),
140 }
130141 },
131 else => return error.Unsupported,
142 else => {},
132143 }
144 return error.Unsupported;
133145}
134146
135147pub const GetNameError = error{
......@@ -179,9 +191,7 @@ pub fn getName(self: Thread, buffer_ptr: *[max_name_len:0]u8) GetNameError!?[]co
179191 .windows => if (target.os.isAtLeast(.windows, .win10_rs1)) |res| {
180192 // GetThreadDescription is only available since version 1607, which is 10.0.14393.795
181193 // See https://en.wikipedia.org/wiki/Microsoft_Windows_SDK
182 if (!res) {
183 return error.Unsupported;
184 }
194 if (!res) return error.Unsupported;
185195
186196 var name_w: os.windows.LPWSTR = undefined;
187197 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
190200 const data_len = try std.unicode.utf16leToUtf8(buffer, std.mem.sliceTo(name_w, 0));
191201
192202 return if (data_len >= 1) buffer[0..data_len] else null;
193 } else {
194 return error.Unsupported;
195203 },
196204 .macos, .ios, .watchos, .tvos => if (use_pthreads) {
197205 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
217225 std.c.pthread_get_name_np(self.getHandle(), buffer.ptr, max_name_len + 1);
218226 return std.mem.sliceTo(buffer, 0);
219227 },
220 else => return error.Unsupported,
228 .dragonfly => if (use_pthreads) {
229 const err = std.c.pthread_getname_np(self.getHandle(), buffer.ptr, max_name_len + 1);
230 switch (err) {
231 .SUCCESS => return std.mem.sliceTo(buffer, 0),
232 .INVAL => unreachable,
233 .FAULT => unreachable,
234 .SRCH => unreachable,
235 else => |e| return os.unexpectedErrno(e),
236 }
237 },
238 else => {},
221239 }
240 return error.Unsupported;
222241}
223242
224243/// Represents a unique ID per thread.
lib/std/c/dragonfly.zig+3
......@@ -33,6 +33,9 @@ pub const pthread_attr_t = extern struct { // copied from freebsd
3333
3434pub const sem_t = ?*opaque {};
3535
36pub extern "c" fn pthread_setname_np(thread: std.c.pthread_t, name: [*:0]const u8) E;
37pub extern "c" fn pthread_getname_np(thread: std.c.pthread_t, name: [*:0]u8, len: usize) E;
38
3639// See:
3740// - https://gitweb.dragonflybsd.org/dragonfly.git/blob/HEAD:/include/unistd.h
3841// - https://gitweb.dragonflybsd.org/dragonfly.git/blob/HEAD:/sys/sys/types.h