From 2efd0eb884d69c967f8b0c7066d62a8b656f1fda Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Tue, 13 Dec 2022 11:36:00 +0100 Subject: [PATCH] darwin: wrap task_threads mach syscall --- lib/std/c/darwin.zig | 2 +- lib/std/os/darwin.zig | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/std/c/darwin.zig b/lib/std/c/darwin.zig index 817b7a18d6..e10dd5489f 100644 --- a/lib/std/c/darwin.zig +++ b/lib/std/c/darwin.zig @@ -573,7 +573,7 @@ pub const vm_region_submap_short_info_64 = extern struct { pub const thread_act_t = mach_port_t; pub const thread_state_t = *natural_t; -pub const mach_port_array_t = *mach_port_t; +pub const mach_port_array_t = [*]mach_port_t; pub extern "c" fn task_threads( target_task: mach_port_t, diff --git a/lib/std/os/darwin.zig b/lib/std/os/darwin.zig index 2b0d8c4c0b..0cf35a6306 100644 --- a/lib/std/os/darwin.zig +++ b/lib/std/os/darwin.zig @@ -438,6 +438,18 @@ const mach_task = if (builtin.target.isDarwin()) struct { }, } } + + pub fn getThreads(task: MachTask) MachError![]std.c.mach_port_t { + var thread_list: std.c.mach_port_array_t = undefined; + var thread_count: std.c.mach_msg_type_number_t = undefined; + switch (std.c.getKernError(std.c.task_threads(task.port, &thread_list, &thread_count))) { + .SUCCESS => return thread_list[0..thread_count], + else => |err| { + log.err("task_threads kernel call failed with error code: {s}", .{@tagName(err)}); + return error.Unexpected; + }, + } + } }; pub fn machTaskForPid(pid: std.os.pid_t) MachError!MachTask {