| ... | ... | @@ -39,6 +39,7 @@ pub const max_name_len = switch (target.os.tag) { |
| 39 | 39 | .netbsd => 31, |
| 40 | 40 | .freebsd => 15, |
| 41 | 41 | .openbsd => 31, |
| 42 | .dragonfly => 1023, |
| 42 | 43 | .solaris => 31, |
| 43 | 44 | else => 0, |
| 44 | 45 | }; |
| ... | ... | @@ -82,13 +83,12 @@ pub fn setName(self: Thread, name: []const u8) SetNameError!void { |
| 82 | 83 | defer file.close(); |
| 83 | 84 | |
| 84 | 85 | try file.writer().writeAll(name); |
| 86 | return; |
| 85 | 87 | }, |
| 86 | 88 | .windows => if (target.os.isAtLeast(.windows, .win10_rs1)) |res| { |
| 87 | 89 | // SetThreadDescription is only available since version 1607, which is 10.0.14393.795 |
| 88 | 90 | // See https://en.wikipedia.org/wiki/Microsoft_Windows_SDK |
| 89 | | if (!res) { |
| 90 | | return error.Unsupported; |
| 91 | | } |
| 91 | if (!res) return error.Unsupported; |
| 92 | 92 | |
| 93 | 93 | var name_buf_w: [max_name_len:0]u16 = undefined; |
| 94 | 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 | 98 | self.getHandle(), |
| 99 | 99 | @ptrCast(os.windows.LPWSTR, &name_buf_w), |
| 100 | 100 | ); |
| 101 | | } else { |
| 102 | | return error.Unsupported; |
| 101 | return; |
| 103 | 102 | }, |
| 104 | 103 | .macos, .ios, .watchos, .tvos => if (use_pthreads) { |
| 105 | 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 | 126 | // pthread_setname_np can return an error. |
| 128 | 127 | |
| 129 | 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 | } |
| 134 | 146 | |
| 135 | 147 | pub const GetNameError = error{ |
| ... | ... | @@ -179,9 +191,7 @@ pub fn getName(self: Thread, buffer_ptr: *[max_name_len:0]u8) GetNameError!?[]co |
| 179 | 191 | .windows => if (target.os.isAtLeast(.windows, .win10_rs1)) |res| { |
| 180 | 192 | // GetThreadDescription is only available since version 1607, which is 10.0.14393.795 |
| 181 | 193 | // See https://en.wikipedia.org/wiki/Microsoft_Windows_SDK |
| 182 | | if (!res) { |
| 183 | | return error.Unsupported; |
| 184 | | } |
| 194 | if (!res) return error.Unsupported; |
| 185 | 195 | |
| 186 | 196 | var name_w: os.windows.LPWSTR = undefined; |
| 187 | 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 | 200 | const data_len = try std.unicode.utf16leToUtf8(buffer, std.mem.sliceTo(name_w, 0)); |
| 191 | 201 | |
| 192 | 202 | return if (data_len >= 1) buffer[0..data_len] else null; |
| 193 | | } else { |
| 194 | | return error.Unsupported; |
| 195 | 203 | }, |
| 196 | 204 | .macos, .ios, .watchos, .tvos => if (use_pthreads) { |
| 197 | 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 | 225 | std.c.pthread_get_name_np(self.getHandle(), buffer.ptr, max_name_len + 1); |
| 218 | 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 | } |
| 223 | 242 | |
| 224 | 243 | /// Represents a unique ID per thread. |