| ... | ... | @@ -18,7 +18,6 @@ pub const Condition = @import("Thread/Condition.zig"); |
| 18 | 18 | pub const RwLock = @import("Thread/RwLock.zig"); |
| 19 | 19 | |
| 20 | 20 | pub const use_pthreads = target.os.tag != .windows and target.os.tag != .wasi and builtin.link_libc; |
| 21 | | const is_gnu = target.abi.isGnu(); |
| 22 | 21 | |
| 23 | 22 | const Thread = @This(); |
| 24 | 23 | const Impl = if (target.os.tag == .windows) |
| ... | ... | @@ -171,21 +170,27 @@ pub fn getName(self: Thread, buffer_ptr: *[max_name_len:0]u8) GetNameError!?[]co |
| 171 | 170 | var buffer: [:0]u8 = buffer_ptr; |
| 172 | 171 | |
| 173 | 172 | switch (target.os.tag) { |
| 174 | | .linux => if (use_pthreads and is_gnu) { |
| 175 | | const err = std.c.pthread_getname_np(self.getHandle(), buffer.ptr, max_name_len + 1); |
| 176 | | switch (err) { |
| 177 | | .SUCCESS => return std.mem.sliceTo(buffer, 0), |
| 178 | | .RANGE => unreachable, |
| 179 | | else => |e| return os.unexpectedErrno(e), |
| 180 | | } |
| 181 | | } else if (use_pthreads and self.getHandle() == std.c.pthread_self()) { |
| 182 | | // Get the name of the calling thread (no thread id required). |
| 183 | | const err = try os.prctl(.GET_NAME, .{@ptrToInt(buffer.ptr)}); |
| 184 | | switch (@intToEnum(os.E, err)) { |
| 185 | | .SUCCESS => return std.mem.sliceTo(buffer, 0), |
| 186 | | else => |e| return os.unexpectedErrno(e), |
| 173 | .linux => if (use_pthreads) { |
| 174 | if (self.getHandle() == std.c.pthread_self()) { |
| 175 | // Get the name of the calling thread (no thread id required). |
| 176 | const err = try os.prctl(.GET_NAME, .{@ptrToInt(buffer.ptr)}); |
| 177 | switch (@intToEnum(os.E, err)) { |
| 178 | .SUCCESS => return std.mem.sliceTo(buffer, 0), |
| 179 | else => |e| return os.unexpectedErrno(e), |
| 180 | } |
| 181 | } else { |
| 182 | if (target.abi.isMusl()) { |
| 183 | // musl doesn't provide pthread_getname_np and there's no way to retrieve the thread id of an arbitrary thread. |
| 184 | return error.Unsupported; |
| 185 | } |
| 186 | const err = std.c.pthread_getname_np(self.getHandle(), buffer.ptr, max_name_len + 1); |
| 187 | switch (err) { |
| 188 | .SUCCESS => return std.mem.sliceTo(buffer, 0), |
| 189 | .RANGE => unreachable, |
| 190 | else => |e| return os.unexpectedErrno(e), |
| 191 | } |
| 187 | 192 | } |
| 188 | | } else if (!use_pthreads) { |
| 193 | } else { |
| 189 | 194 | var buf: [32]u8 = undefined; |
| 190 | 195 | const path = try std.fmt.bufPrint(&buf, "/proc/self/task/{d}/comm", .{self.getHandle()}); |
| 191 | 196 | |
| ... | ... | @@ -195,9 +200,6 @@ pub fn getName(self: Thread, buffer_ptr: *[max_name_len:0]u8) GetNameError!?[]co |
| 195 | 200 | const data_len = try file.reader().readAll(buffer_ptr[0 .. max_name_len + 1]); |
| 196 | 201 | |
| 197 | 202 | return if (data_len >= 1) buffer[0 .. data_len - 1] else null; |
| 198 | | } else { |
| 199 | | // musl doesn't provide pthread_getname_np and there's no way to retrieve the thread id of an arbitrary thread. |
| 200 | | return error.Unsupported; |
| 201 | 203 | }, |
| 202 | 204 | .windows => { |
| 203 | 205 | const buf_capacity = @sizeOf(os.windows.UNICODE_STRING) + (@sizeOf(u16) * max_name_len); |