authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-04-20 16:03:12-07:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-04-20 16:03:12-07:00
loga1f7c8d860e52cc2d5b8c2196230306095aaeea3
tree0fbb33755eb365c12a54ea4a3c5e4af642c354b4
parente8fdb249b673e3d69225c1b7cba68bdfc63061b1
parent140ca67ea6d42d635c837558c075d8d9abd4e884
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #14696 from r00ster91/thread

std.Thread: use dead code

1 files changed, 33 insertions(+), 41 deletions(-)

lib/std/Thread.zig+33-41
......@@ -20,7 +20,6 @@ pub const Pool = @import("Thread/Pool.zig");
2020pub const WaitGroup = @import("Thread/WaitGroup.zig");
2121
2222pub const use_pthreads = target.os.tag != .windows and target.os.tag != .wasi and builtin.link_libc;
23const is_gnu = target.abi.isGnu();
2423
2524const Thread = @This();
2625const Impl = if (target.os.tag == .windows)
......@@ -64,18 +63,20 @@ pub fn setName(self: Thread, name: []const u8) SetNameError!void {
6463
6564 switch (target.os.tag) {
6665 .linux => if (use_pthreads) {
67 const err = std.c.pthread_setname_np(self.getHandle(), name_with_terminator.ptr);
68 switch (err) {
69 .SUCCESS => return,
70 .RANGE => unreachable,
71 else => |e| return os.unexpectedErrno(e),
72 }
73 } else if (use_pthreads and self.getHandle() == std.c.pthread_self()) {
74 // TODO: this is dead code. what did the author of this code intend to happen here?
75 const err = try os.prctl(.SET_NAME, .{@ptrToInt(name_with_terminator.ptr)});
76 switch (@intToEnum(os.E, err)) {
77 .SUCCESS => return,
78 else => |e| return os.unexpectedErrno(e),
66 if (self.getHandle() == std.c.pthread_self()) {
67 // Set the name of the calling thread (no thread id required).
68 const err = try os.prctl(.SET_NAME, .{@ptrToInt(name_with_terminator.ptr)});
69 switch (@intToEnum(os.E, err)) {
70 .SUCCESS => return,
71 else => |e| return os.unexpectedErrno(e),
72 }
73 } else {
74 const err = std.c.pthread_setname_np(self.getHandle(), name_with_terminator.ptr);
75 switch (err) {
76 .SUCCESS => return,
77 .RANGE => unreachable,
78 else => |e| return os.unexpectedErrno(e),
79 }
7980 }
8081 } else {
8182 var buf: [32]u8 = undefined;
......@@ -171,20 +172,23 @@ pub fn getName(self: Thread, buffer_ptr: *[max_name_len:0]u8) GetNameError!?[]co
171172 var buffer: [:0]u8 = buffer_ptr;
172173
173174 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 const err = try os.prctl(.GET_NAME, .{@ptrToInt(buffer.ptr)});
183 switch (@intToEnum(os.E, err)) {
184 .SUCCESS => return std.mem.sliceTo(buffer, 0),
185 else => |e| return os.unexpectedErrno(e),
175 .linux => if (use_pthreads) {
176 if (self.getHandle() == std.c.pthread_self()) {
177 // Get the name of the calling thread (no thread id required).
178 const err = try os.prctl(.GET_NAME, .{@ptrToInt(buffer.ptr)});
179 switch (@intToEnum(os.E, err)) {
180 .SUCCESS => return std.mem.sliceTo(buffer, 0),
181 else => |e| return os.unexpectedErrno(e),
182 }
183 } else {
184 const err = std.c.pthread_getname_np(self.getHandle(), buffer.ptr, max_name_len + 1);
185 switch (err) {
186 .SUCCESS => return std.mem.sliceTo(buffer, 0),
187 .RANGE => unreachable,
188 else => |e| return os.unexpectedErrno(e),
189 }
186190 }
187 } else if (!use_pthreads) {
191 } else {
188192 var buf: [32]u8 = undefined;
189193 const path = try std.fmt.bufPrint(&buf, "/proc/self/task/{d}/comm", .{self.getHandle()});
190194
......@@ -194,9 +198,6 @@ pub fn getName(self: Thread, buffer_ptr: *[max_name_len:0]u8) GetNameError!?[]co
194198 const data_len = try file.reader().readAll(buffer_ptr[0 .. max_name_len + 1]);
195199
196200 return if (data_len >= 1) buffer[0 .. data_len - 1] else null;
197 } else {
198 // musl doesn't provide pthread_getname_np and there's no way to retrieve the thread id of an arbitrary thread.
199 return error.Unsupported;
200201 },
201202 .windows => {
202203 const buf_capacity = @sizeOf(os.windows.UNICODE_STRING) + (@sizeOf(u16) * max_name_len);
......@@ -327,10 +328,10 @@ pub const SpawnError = error{
327328 Unexpected,
328329};
329330
330/// Spawns a new thread which executes `function` using `args` and returns a handle the spawned thread.
331/// Spawns a new thread which executes `function` using `args` and returns a handle to the spawned thread.
331332/// `config` can be used as hints to the platform for now to spawn and execute the `function`.
332333/// The caller must eventually either call `join()` to wait for the thread to finish and free its resources
333/// or call `detach()` to excuse the caller from calling `join()` and have the thread clean up its resources on completion`.
334/// or call `detach()` to excuse the caller from calling `join()` and have the thread clean up its resources on completion.
334335pub fn spawn(config: SpawnConfig, comptime function: anytype, args: anytype) SpawnError!Thread {
335336 if (builtin.single_threaded) {
336337 @compileError("Cannot spawn thread when building in single-threaded mode");
......@@ -1133,16 +1134,7 @@ test "setName, getName" {
11331134 error.Unsupported => return error.SkipZigTest,
11341135 else => return err,
11351136 },
1136 else => |tag| if (tag == .linux and use_pthreads and comptime target.abi.isMusl()) {
1137 try thread.setName("foobar");
1138
1139 var name_buffer: [max_name_len:0]u8 = undefined;
1140 const res = thread.getName(&name_buffer);
1141
1142 try std.testing.expectError(error.Unsupported, res);
1143 } else {
1144 try testThreadName(&thread);
1145 },
1137 else => try testThreadName(&thread),
11461138 }
11471139
11481140 context.thread_done_event.set();