pub fn with_thread<F, T>(tid: ThreadId, f: F) -> T
where
    F: FnOnce(&Thread) -> T,
{
    let (page_addr, offset) = tid_to_addr(tid);
    // Safety:
    // Only non-mutable references are ever made to `TcbPage`.
    let page = unsafe { &*(page_addr as *const TcbPage) };
    let block = &page.threads[offset];
    unsafe {
        // Safety:
        // We want to verify the thread id to be of the same generation.
        // We can't use `get_ref` on an uninitialized thread because that's undefined behaviour.
        // That means we have to read the field without calling `get_ref` first.
        // If there is no thread here, its id and generation will be zero.
        let tid_ptr =
            (block.0.as_ptr() as *const u8).add(offset_of!(Thread, id)) as *const ThreadId;
        assert_eq!(*tid_ptr, tid, "thread generation mismatch");

        // We now know that the thread was initialized, otherwise the assert would've failed.
        f(block.0.assume_init_ref())
    }
}
/// Executes something in context of a thread.
///
/// # Panic
///
/// Panics if the thread is not valid anymore.
/// Callers should've been notified when a thread they're interested in ceases to exist.
Executes something in context of a thread.
Executes something in context of a thread .
pub fn with_thread<F, T>(tid: ThreadId, f: F) -> T
where
    F: FnOnce(&Thread) -> T,
{
    let (page_addr, offset) = tid_to_addr(tid);
    // Safety:
    // Only non-mutable references are ever made to `TcbPage`.
    let page = unsafe { &*(page_addr as *const TcbPage) };
    let block = &page.threads[offset];
    unsafe {
        // Safety:
        // We want to verify the thread id to be of the same generation.
        // We can't use `get_ref` on an uninitialized thread because that's undefined behaviour.
        // That means we have to read the field without calling `get_ref` first.
        // If there is no thread here, its id and generation will be zero.
        let tid_ptr =
            (block.0.as_ptr() as *const u8).add(offset_of!(Thread, id)) as *const ThreadId;
        assert_eq!(*tid_ptr, tid, "thread generation mismatch");
        // We now know that the thread was initialized, otherwise the assert would've failed.
        f(block.0.assume_init_ref())
    }
}
pub fn with_thread < F , T > ( tid : ThreadId , f : F ) -> T where F : FnOnce ( & Thread ) -> T , { let ( page_addr , offset ) = tid_to_addr ( tid ) ; // Safety: // Only non-mutable references are ever made to `TcbPage`. let page = unsafe { & * ( page_addr as * const TcbPage ) } ; let block = & page . threads [ offset ] ; unsafe { // Safety: // We want to verify the thread id to be of the same generation. // We can't use `get_ref` on an uninitialized thread because that's undefined behaviour. // That means we have to read the field without calling `get_ref` first. // If there is no thread here, its id and generation will be zero. let tid_ptr = ( block . 0 . as_ptr ( ) as * const u8 ) . add ( offset_of ! ( Thread id ) ) as * const ThreadId ; assert_eq ! ( tid_ptr tid " " ) ; // We now know that the thread was initialized, otherwise the assert would've failed. f ( block . 0 . assume_init_ref ( ) ) } }
Executes something in context of a thread.
Executes something in context of a thread .
tid
ThreadId
f
F
tid
ThreadId
f
F
panic
Panics if the thread is not valid anymore.
Callers should've been notified when a thread they're interested in ceases to exist.
Panics if the thread is not valid anymore . Callers should ' ve been notified when a thread they ' re interested in ceases to exist .