@@ -24,6 +24,18 @@ const mach_task = if (builtin.target.isDarwin()) struct {
2424 return self .port != std .c .TASK_NULL ;
2525 }
2626
27+ pub fn pidForTask (self : MachTask ) MachError ! std.os.pid_t {
28+ var pid : std.os.pid_t = undefined ;
29+ switch (std .c .getKernError (std .c .pid_for_task (self .port , & pid ))) {
30+ .SUCCESS = > return pid ,
31+ .FAILURE = > return error .PermissionDenied ,
32+ else = > | err | {
33+ log .err ("pid_for_task kernel call failed with error code: {s}" , .{@tagName (err )});
34+ return error .Unexpected ;
35+ },
36+ }
37+ }
38+
2739 pub fn allocatePort (self : MachTask , right : std.c.MACH_PORT_RIGHT ) MachError ! MachTask {
2840 var out_port : std.c.mach_port_name_t = undefined ;
2941 switch (std .c .getKernError (std .c .mach_port_allocate (
@@ -440,7 +452,7 @@ const mach_task = if (builtin.target.isDarwin()) struct {
440452 }
441453
442454 const ThreadList = struct {
443- buf : []ThreadId ,
455+ buf : []MachThread ,
444456
445457 pub fn deinit (list : ThreadList ) void {
446458 const self_task = machTaskForSelf ();
@@ -456,7 +468,7 @@ const mach_task = if (builtin.target.isDarwin()) struct {
456468 var thread_list : std.c.mach_port_array_t = undefined ;
457469 var thread_count : std.c.mach_msg_type_number_t = undefined ;
458470 switch (std .c .getKernError (std .c .task_threads (task .port , & thread_list , & thread_count ))) {
459- .SUCCESS = > return ThreadList { .buf = @ptrCast ([* ]ThreadId , thread_list )[0.. thread_count ] },
471+ .SUCCESS = > return ThreadList { .buf = @ptrCast ([* ]MachThread , thread_list )[0.. thread_count ] },
460472 else = > | err | {
461473 log .err ("task_threads kernel call failed with error code: {s}" , .{@tagName (err )});
462474 return error .Unexpected ;
@@ -465,14 +477,18 @@ const mach_task = if (builtin.target.isDarwin()) struct {
465477 }
466478 };
467479
468- pub const ThreadId = extern struct {
469- id : std.c.thread_act_t ,
480+ pub const MachThread = extern struct {
481+ port : std.c.mach_port_t ,
482+
483+ pub fn isValid (thread : MachThread ) bool {
484+ return thread .port != std .c .THREAD_NULL ;
485+ }
470486
471- pub fn getBasicInfo (thread_id : ThreadId ) MachError ! std.c.thread_basic_info {
487+ pub fn getBasicInfo (thread : MachThread ) MachError ! std.c.thread_basic_info {
472488 var info : std.c.thread_basic_info = undefined ;
473489 var count = std .c .THREAD_BASIC_INFO_COUNT ;
474490 switch (std .c .getKernError (std .c .thread_info (
475- thread_id . id ,
491+ thread . port ,
476492 std .c .THREAD_BASIC_INFO ,
477493 @ptrCast (std .c .thread_info_t , & info ),
478494 & count ,
@@ -485,11 +501,11 @@ const mach_task = if (builtin.target.isDarwin()) struct {
485501 }
486502 }
487503
488- pub fn getIdentifierInfo (thread_id : ThreadId ) MachError ! std.c.thread_identifier_info {
504+ pub fn getIdentifierInfo (thread : MachThread ) MachError ! std.c.thread_identifier_info {
489505 var info : std.c.thread_identifier_info = undefined ;
490506 var count = std .c .THREAD_IDENTIFIER_INFO_COUNT ;
491507 switch (std .c .getKernError (std .c .thread_info (
492- thread_id . id ,
508+ thread . port ,
493509 std .c .THREAD_IDENTIFIER_INFO ,
494510 @ptrCast (std .c .thread_info_t , & info ),
495511 & count ,
0 commit comments