Let’s break down the design of CachedParkThread:

  1. Purpose: CachedParkThread is designed to provide thread parking functionality with caching, optimized for use within a single thread.

  2. Structure:

    pub(crate) struct CachedParkThread {
        _anchor: PhantomData<Rc<()>>,
    }

    It uses a PhantomData<Rc<()>> to ensure the struct is not Send or Sync, tying it to a single thread.

  3. Key methods:

    • new(): Creates a new instance.
    • waker(): Returns a Waker for the current thread.
    • park(): Parks the current thread.
    • park_timeout(): Parks the thread with a timeout.
    • block_on<F: Future>(): Blocks the thread while polling a future to completion.
  4. Caching mechanism: It uses a thread-local storage (TLS) to cache the ParkThread:

    tokio_thread_local! {
        static CURRENT_PARKER: ParkThread = ParkThread::new();
    }

    This allows efficient reuse of the parking mechanism within a thread.

  5. Thread-safety: The use of PhantomData<Rc<()>> and TLS ensures that CachedParkThread is bound to a single thread and cannot be shared or sent between threads.

  6. Waker creation: It can create a Waker from the underlying UnparkThread, allowing integration with Rust’s async/await system.

  7. Future execution: The block_on method implements a basic executor, running a future to completion by repeatedly polling and parking the thread when necessary.

  8. Error handling: Methods return Result<_, AccessError> to handle cases where TLS access fails.

This design allows for efficient thread parking and waking operations within Tokio’s runtime, particularly useful for blocking operations and integrating with the async ecosystem.