Merge pull request #9910 from mikdusan/dragonfly

dragonfly: port Thread.setname/getname
This commit is contained in:
Andrew Kelley 2021-12-03 17:29:55 -08:00 committed by GitHub
commit 2a0adef583
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 12 deletions

View File

@ -39,6 +39,7 @@ pub const max_name_len = switch (target.os.tag) {
.netbsd => 31,
.freebsd => 15,
.openbsd => 31,
.dragonfly => 1023,
.solaris => 31,
else => 0,
};
@ -82,13 +83,12 @@ pub fn setName(self: Thread, name: []const u8) SetNameError!void {
defer file.close();
try file.writer().writeAll(name);
return;
},
.windows => if (target.os.isAtLeast(.windows, .win10_rs1)) |res| {
// SetThreadDescription is only available since version 1607, which is 10.0.14393.795
// See https://en.wikipedia.org/wiki/Microsoft_Windows_SDK
if (!res) {
return error.Unsupported;
}
if (!res) return error.Unsupported;
var name_buf_w: [max_name_len:0]u16 = undefined;
const length = try std.unicode.utf8ToUtf16Le(&name_buf_w, name);
@ -98,8 +98,7 @@ pub fn setName(self: Thread, name: []const u8) SetNameError!void {
self.getHandle(),
@ptrCast(os.windows.LPWSTR, &name_buf_w),
);
} else {
return error.Unsupported;
return;
},
.macos, .ios, .watchos, .tvos => if (use_pthreads) {
// 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 {
// pthread_setname_np can return an error.
std.c.pthread_set_name_np(self.getHandle(), name_with_terminator.ptr);
return;
},
else => return error.Unsupported,
.dragonfly => if (use_pthreads) {
const err = std.c.pthread_setname_np(self.getHandle(), name_with_terminator.ptr);
switch (err) {
.SUCCESS => return,
.INVAL => unreachable,
.FAULT => unreachable,
.NAMETOOLONG => unreachable, // already checked
.SRCH => unreachable,
else => |e| return os.unexpectedErrno(e),
}
},
else => {},
}
return error.Unsupported;
}
pub const GetNameError = error{
@ -179,9 +191,7 @@ pub fn getName(self: Thread, buffer_ptr: *[max_name_len:0]u8) GetNameError!?[]co
.windows => if (target.os.isAtLeast(.windows, .win10_rs1)) |res| {
// GetThreadDescription is only available since version 1607, which is 10.0.14393.795
// See https://en.wikipedia.org/wiki/Microsoft_Windows_SDK
if (!res) {
return error.Unsupported;
}
if (!res) return error.Unsupported;
var name_w: os.windows.LPWSTR = undefined;
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
const data_len = try std.unicode.utf16leToUtf8(buffer, std.mem.sliceTo(name_w, 0));
return if (data_len >= 1) buffer[0..data_len] else null;
} else {
return error.Unsupported;
},
.macos, .ios, .watchos, .tvos => if (use_pthreads) {
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
std.c.pthread_get_name_np(self.getHandle(), buffer.ptr, max_name_len + 1);
return std.mem.sliceTo(buffer, 0);
},
else => return error.Unsupported,
.dragonfly => if (use_pthreads) {
const err = std.c.pthread_getname_np(self.getHandle(), buffer.ptr, max_name_len + 1);
switch (err) {
.SUCCESS => return std.mem.sliceTo(buffer, 0),
.INVAL => unreachable,
.FAULT => unreachable,
.SRCH => unreachable,
else => |e| return os.unexpectedErrno(e),
}
},
else => {},
}
return error.Unsupported;
}
/// Represents a unique ID per thread.

View File

@ -33,6 +33,9 @@ pub const pthread_attr_t = extern struct { // copied from freebsd
pub const sem_t = ?*opaque {};
pub extern "c" fn pthread_setname_np(thread: std.c.pthread_t, name: [*:0]const u8) E;
pub extern "c" fn pthread_getname_np(thread: std.c.pthread_t, name: [*:0]u8, len: usize) E;
// See:
// - https://gitweb.dragonflybsd.org/dragonfly.git/blob/HEAD:/include/unistd.h
// - https://gitweb.dragonflybsd.org/dragonfly.git/blob/HEAD:/sys/sys/types.h