1use crate::ffi::c_int;
4
5use crate::kern_return::kern_return_t;
6use crate::port::{mach_port_name_t, mach_port_t};
7
8extern "C" {
9 static mach_task_self_: mach_port_t;
10
11 pub fn task_for_pid(
12 target_tport: mach_port_name_t,
13 pid: c_int,
14 tn: *mut mach_port_name_t,
15 ) -> kern_return_t;
16}
17
18#[cfg(feature = "unstable")]
19pub mod sync {
20 use super::{mach_port_t, mach_task_self_};
21
22 #[cfg(feature = "std")]
23 use once_cell::sync::Lazy;
24 #[cfg(feature = "std")]
25 static _MUTEX_STD: Lazy<parking_lot::Mutex<()>> = Lazy::new(|| { parking_lot::Mutex::new(()) });
26
27 #[cfg(not(feature = "std"))]
28 use generic_once_cell::Lazy as GenericLazy;
29 #[cfg(not(feature = "std"))]
30 use mcslock::{
31 raw::spins,
32 barging::Mutex as SpinMutex,
33 relax::Spin,
34 };
35 #[cfg(not(feature = "std"))]
36 static _MUTEX_MCS: GenericLazy<SpinMutex<(), Spin>, spins::Mutex<()>> = GenericLazy::new(|| { spins::Mutex::new(()) });
37
38 pub fn mach_task_self() -> mach_port_t {
39 #[cfg(feature = "std")]
40 let _guard = _MUTEX_STD.lock();
41
42 #[cfg(not(feature = "std"))]
43 let mut _node = spins::MutexNode::new();
44 #[cfg(not(feature = "std"))]
45 let _guard = _MUTEX_MCS.lock(&mut _node);
46
47 unsafe { mach_task_self_ }
49
50 }
52}
53
54pub unsafe fn mach_task_self() -> mach_port_t {
56 mach_task_self_
57}
58
59pub unsafe fn current_task() -> mach_port_t {
61 mach_task_self()
62}
63
64#[cfg(test)]
65mod tests {
66 use crate::port::*;
67 use crate::traps::*;
68
69 #[test]
70 fn mach_task_self_sanity() {
71 unsafe {
72 let this_task = mach_task_self();
73 assert!(this_task != MACH_PORT_NULL);
74 assert!(this_task != MACH_PORT_DEAD);
75 p!("task id = {this_task}");
76 }
77 }
78}
79