1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//! This module corresponds to `mach/mach_traps.h`.

use crate::ffi::c_int;

use crate::kern_return::kern_return_t;
use crate::port::{mach_port_name_t, mach_port_t};

extern "C" {
    static mach_task_self_: mach_port_t;

    pub fn task_for_pid(
        target_tport: mach_port_name_t,
        pid:          c_int,
        tn:           *mut mach_port_name_t,
    ) -> kern_return_t;
}

#[cfg(feature = "unstable")]
pub mod sync {
    use super::{mach_port_t, mach_task_self_};

    #[cfg(feature = "std")]
    use once_cell::sync::Lazy;
    #[cfg(feature = "std")]
    static _MUTEX_STD: Lazy<parking_lot::Mutex<()>> = Lazy::new(|| { parking_lot::Mutex::new(()) });

    #[cfg(not(feature = "std"))]
    use generic_once_cell::Lazy as GenericLazy;
    #[cfg(not(feature = "std"))]
    use mcslock::{
        raw::spins,
        barging::Mutex as SpinMutex,
        relax::Spin,
    };
    #[cfg(not(feature = "std"))]
    static _MUTEX_MCS: GenericLazy<SpinMutex<(), Spin>, spins::Mutex<()>> = GenericLazy::new(|| { spins::Mutex::new(()) });

    pub fn mach_task_self() -> mach_port_t {
        #[cfg(feature = "std")]
        let _guard = _MUTEX_STD.lock();

        #[cfg(not(feature = "std"))]
        let mut _node = spins::MutexNode::new();
        #[cfg(not(feature = "std"))]
        let _guard = _MUTEX_MCS.lock(&mut _node);

        /// SAFETY: Mutex lock for any access operations
        unsafe { mach_task_self_ }

        // guard dropping here.
    }
}

/// SAFETY? not sure but you can use a thread lock externally, or use [sync::mach_task_self].
pub unsafe fn mach_task_self() -> mach_port_t {
    mach_task_self_
}

/// SAFETY? see [mach_task_self]
pub unsafe fn current_task() -> mach_port_t {
    mach_task_self()
}

#[cfg(test)]
mod tests {
    use crate::port::*;
    use crate::traps::*;

    #[test]
    fn mach_task_self_sanity() {
        unsafe {
            let this_task = mach_task_self();
            assert!(this_task != MACH_PORT_NULL);
            assert!(this_task != MACH_PORT_DEAD);
        }
    }
}