| ... | @@ -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(); |
| 83 | | 84 | |
| 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.795 | 89 | // SetThreadDescription is only available since version 1607, which is 10.0.14393.795 |
| 88 | // See https://en.wikipedia.org/wiki/Microsoft_Windows_SDK | 90 | // See https://en.wikipedia.org/wiki/Microsoft_Windows_SDK |
| 89 | if (!res) { | 91 | if (!res) return error.Unsupported; |
| 90 | return error.Unsupported; | | |
| 91 | } | | |
| 92 | | 92 | |
| 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. |
| 128 | | 127 | |
| 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 | } |
| 134 | | 146 | |
| 135 | pub const GetNameError = error{ | 147 | pub 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.795 | 192 | // GetThreadDescription is only available since version 1607, which is 10.0.14393.795 |
| 181 | // See https://en.wikipedia.org/wiki/Microsoft_Windows_SDK | 193 | // See https://en.wikipedia.org/wiki/Microsoft_Windows_SDK |
| 182 | if (!res) { | 194 | if (!res) return error.Unsupported; |
| 183 | return error.Unsupported; | | |
| 184 | } | | |
| 185 | | 195 | |
| 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)); |
| 191 | | 201 | |
| 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 | } |
| 223 | | 242 | |
| 224 | /// Represents a unique ID per thread. | 243 | /// Represents a unique ID per thread. |