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