pub struct ThreadContext { /* private fields */ }Expand description
ThreadContext maintains the long-lived, persistent contention state of a
specific thread as it interacts with a specific cache instance.
Unlike a request-scoped budget (quota), ThreadContext acts as the “Thread Memory.”
It tracks historical contention via Backoff, allowing the thread to adapt its
timing based on how “hot” the cache has been in recent operations.
§Safety and Thread Locality
This struct is explicitly !Send and !Sync.
It uses UnsafeCell to provide interior mutability with zero runtime overhead.
This is only safe when the context is stored in a way that guarantees exclusive
access by a single thread (e.g., inside a ThreadLocal ). The non-thread-safe
markers ensure this state cannot accidentally leak or be shared across thread boundaries.
Implementations§
Source§impl ThreadContext
impl ThreadContext
Sourcepub fn new(backoff: Backoff) -> Self
pub fn new(backoff: Backoff) -> Self
Creates a new ThreadContext with the provided backoff policy.
Usually initialized lazily when a thread first interacts with a cache instance.
Sourcepub fn wait(&self)
pub fn wait(&self)
Signals hardware-level contention and triggers a thread yield.
This should be called when an atomic operation (like a CAS) fails.
It increases the internal frustration level and performs a hardware-friendly
stall (e.g., spin_loop or yield) based on the persistent backoff state.