authorgravatar for r00ster91@proton.meWooster <r00ster91@proton.me> 2023-02-24 20:58:09+01:00
committergravatar for r00ster91@proton.meWooster <r00ster91@proton.me> 2023-02-24 20:58:09+01:00
logf10100cf9b3d6929f2ed2df64835b2773e117665
tree5d0bc50e2973e90ce2e532a2c84cf10081385844
parent0b2ee093788ba84c5d130c021f347844b5939889

std.Thread: drop is_gnu check

I believe the reason we had that check in the first place was because for both `pthread_setname_np` and `pthread_getname_np` man says: ``` CONFORMING TO These functions are nonstandard GNU extensions; hence the suffix "_np" (nonportable) in the names. ``` However, this `is_gnu` check was never consistently applied; it was missing in `setName` in the Linux case. It is also missing on all other call sites for other platforms (macOS, iOS, et al.). Though, that could be because it may only apply to Linux. I think for a best-effort approach it is okay to drop this. It's probably less non-standard than man makes it out to be.

1 files changed, 20 insertions(+), 18 deletions(-)

lib/std/Thread.zig+20-18
......@@ -18,7 +18,6 @@ pub const Condition = @import("Thread/Condition.zig");
1818pub const RwLock = @import("Thread/RwLock.zig");
1919
2020pub const use_pthreads = target.os.tag != .windows and target.os.tag != .wasi and builtin.link_libc;
21const is_gnu = target.abi.isGnu();
2221
2322const Thread = @This();
2423const Impl = if (target.os.tag == .windows)
......@@ -171,21 +170,27 @@ pub fn getName(self: Thread, buffer_ptr: *[max_name_len:0]u8) GetNameError!?[]co
171170 var buffer: [:0]u8 = buffer_ptr;
172171
173172 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 }
187192 }
188 } else if (!use_pthreads) {
193 } else {
189194 var buf: [32]u8 = undefined;
190195 const path = try std.fmt.bufPrint(&buf, "/proc/self/task/{d}/comm", .{self.getHandle()});
191196
......@@ -195,9 +200,6 @@ pub fn getName(self: Thread, buffer_ptr: *[max_name_len:0]u8) GetNameError!?[]co
195200 const data_len = try file.reader().readAll(buffer_ptr[0 .. max_name_len + 1]);
196201
197202 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;
201203 },
202204 .windows => {
203205 const buf_capacity = @sizeOf(os.windows.UNICODE_STRING) + (@sizeOf(u16) * max_name_len);