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) {...@@ -39,6 +39,7 @@ pub const max_name_len = switch (target.os.tag) {
39 .netbsd => 31,39 .netbsd => 31,
40 .freebsd => 15,40 .freebsd => 15,
41 .openbsd => 31,41 .openbsd => 31,
42 .dragonfly => 1023,
42 .solaris => 31,43 .solaris => 31,
43 else => 0,44 else => 0,
44};45};
...@@ -82,13 +83,12 @@ pub fn setName(self: Thread, name: []const u8) SetNameError!void {...@@ -82,13 +83,12 @@ pub fn setName(self: Thread, name: []const u8) SetNameError!void {
82 defer file.close();83 defer file.close();
8384
84 try file.writer().writeAll(name);85 try file.writer().writeAll(name);
86 return;
85 },87 },
86 .windows => if (target.os.isAtLeast(.windows, .win10_rs1)) |res| {88 .windows => if (target.os.isAtLeast(.windows, .win10_rs1)) |res| {
87 // SetThreadDescription is only available since version 1607, which is 10.0.14393.79589 // SetThreadDescription is only available since version 1607, which is 10.0.14393.795
88 // See https://en.wikipedia.org/wiki/Microsoft_Windows_SDK90 // See https://en.wikipedia.org/wiki/Microsoft_Windows_SDK
89 if (!res) {91 if (!res) return error.Unsupported;
90 return error.Unsupported;
91 }
9292
93 var name_buf_w: [max_name_len:0]u16 = undefined;93 var name_buf_w: [max_name_len:0]u16 = undefined;
94 const length = try std.unicode.utf8ToUtf16Le(&name_buf_w, name);94 const length = try std.unicode.utf8ToUtf16Le(&name_buf_w, name);
...@@ -98,8 +98,7 @@ pub fn setName(self: Thread, name: []const u8) SetNameError!void {...@@ -98,8 +98,7 @@ pub fn setName(self: Thread, name: []const u8) SetNameError!void {
98 self.getHandle(),98 self.getHandle(),
99 @ptrCast(os.windows.LPWSTR, &name_buf_w),99 @ptrCast(os.windows.LPWSTR, &name_buf_w),
100 );100 );
101 } else {101 return;
102 return error.Unsupported;
103 },102 },
104 .macos, .ios, .watchos, .tvos => if (use_pthreads) {103 .macos, .ios, .watchos, .tvos => if (use_pthreads) {
105 // There doesn't seem to be a way to set the name for an arbitrary thread, only the current one.104 // 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 {...@@ -127,9 +126,22 @@ pub fn setName(self: Thread, name: []const u8) SetNameError!void {
127 // pthread_setname_np can return an error.126 // pthread_setname_np can return an error.
128127
129 std.c.pthread_set_name_np(self.getHandle(), name_with_terminator.ptr);128 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 }
130 },141 },
131 else => return error.Unsupported,142 else => {},
132 }143 }
144 return error.Unsupported;
133}145}
134146
135pub const GetNameError = error{147pub const GetNameError = error{
...@@ -179,9 +191,7 @@ pub fn getName(self: Thread, buffer_ptr: *[max_name_len:0]u8) GetNameError!?[]co...@@ -179,9 +191,7 @@ pub fn getName(self: Thread, buffer_ptr: *[max_name_len:0]u8) GetNameError!?[]co
179 .windows => if (target.os.isAtLeast(.windows, .win10_rs1)) |res| {191 .windows => if (target.os.isAtLeast(.windows, .win10_rs1)) |res| {
180 // GetThreadDescription is only available since version 1607, which is 10.0.14393.795192 // GetThreadDescription is only available since version 1607, which is 10.0.14393.795
181 // See https://en.wikipedia.org/wiki/Microsoft_Windows_SDK193 // See https://en.wikipedia.org/wiki/Microsoft_Windows_SDK
182 if (!res) {194 if (!res) return error.Unsupported;
183 return error.Unsupported;
184 }
185195
186 var name_w: os.windows.LPWSTR = undefined;196 var name_w: os.windows.LPWSTR = undefined;
187 try os.windows.GetThreadDescription(self.getHandle(), &name_w);197 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...@@ -190,8 +200,6 @@ pub fn getName(self: Thread, buffer_ptr: *[max_name_len:0]u8) GetNameError!?[]co
190 const data_len = try std.unicode.utf16leToUtf8(buffer, std.mem.sliceTo(name_w, 0));200 const data_len = try std.unicode.utf16leToUtf8(buffer, std.mem.sliceTo(name_w, 0));
191201
192 return if (data_len >= 1) buffer[0..data_len] else null;202 return if (data_len >= 1) buffer[0..data_len] else null;
193 } else {
194 return error.Unsupported;
195 },203 },
196 .macos, .ios, .watchos, .tvos => if (use_pthreads) {204 .macos, .ios, .watchos, .tvos => if (use_pthreads) {
197 const err = std.c.pthread_getname_np(self.getHandle(), buffer.ptr, max_name_len + 1);205 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...@@ -217,8 +225,19 @@ pub fn getName(self: Thread, buffer_ptr: *[max_name_len:0]u8) GetNameError!?[]co
217 std.c.pthread_get_name_np(self.getHandle(), buffer.ptr, max_name_len + 1);225 std.c.pthread_get_name_np(self.getHandle(), buffer.ptr, max_name_len + 1);
218 return std.mem.sliceTo(buffer, 0);226 return std.mem.sliceTo(buffer, 0);
219 },227 },
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 => {},
221 }239 }
240 return error.Unsupported;
222}241}
223242
224/// Represents a unique ID per thread.243/// 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...@@ -33,6 +33,9 @@ pub const pthread_attr_t = extern struct { // copied from freebsd
3333
34pub const sem_t = ?*opaque {};34pub 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
36// See:39// See:
37// - https://gitweb.dragonflybsd.org/dragonfly.git/blob/HEAD:/include/unistd.h40// - https://gitweb.dragonflybsd.org/dragonfly.git/blob/HEAD:/include/unistd.h
38// - https://gitweb.dragonflybsd.org/dragonfly.git/blob/HEAD:/sys/sys/types.h41// - https://gitweb.dragonflybsd.org/dragonfly.git/blob/HEAD:/sys/sys/types.h