use std::cell::Cell; use std::ptr; use std::sync::atomic::{AtomicPtr, AtomicUsize, Ordering}; use std::sync::Arc; use std::thread::{self, Thread, ThreadId}; use std::time::Instant;
use crossbeam_utils::Backoff;
usecrate::select::Selected;
/// Thread-local context used in select. // This is a private API that is used by the select macro. #[derive(Debug, Clone)] pubstruct Context {
inner: Arc<Inner>,
}
/// A slot into which another thread may store a pointer to its `Packet`.
packet: AtomicPtr<()>,
/// Thread handle.
thread: Thread,
/// Thread id.
thread_id: ThreadId,
}
impl Context { /// Creates a new context for the duration of the closure. #[inline] pubfn with<F, R>(f: F) -> R where
F: FnOnce(&Context) -> R,
{
std::thread_local! { /// Cached thread-local context. static CONTEXT: Cell<Option<Context>> = Cell::new(Some(Context::new()));
}
letmut f = Some(f); letmut f = |cx: &Context| -> R { let f = f.take().unwrap();
f(cx)
};
CONTEXT
.try_with(|cell| match cell.take() {
None => f(&Context::new()),
Some(cx) => {
cx.reset(); let res = f(&cx);
cell.set(Some(cx));
res
}
})
.unwrap_or_else(|_| f(&Context::new()))
}
/// Stores a packet. /// /// This method must be called after `try_select` succeeds and there is a packet to provide. #[inline] pubfn store_packet(&self, packet: *mut ()) { if !packet.is_null() { self.inner.packet.store(packet, Ordering::Release);
}
}
/// Waits until a packet is provided and returns it. #[inline] pubfn wait_packet(&self) -> *mut () { let backoff = Backoff::new(); loop { let packet = self.inner.packet.load(Ordering::Acquire); if !packet.is_null() { return packet;
}
backoff.snooze();
}
}
/// Waits until an operation is selected and returns it. /// /// If the deadline is reached, `Selected::Aborted` will be selected. #[inline] pubfn wait_until(&self, deadline: Option<Instant>) -> Selected { // Spin for a short time, waiting until an operation is selected. let backoff = Backoff::new(); loop { let sel = Selected::from(self.inner.select.load(Ordering::Acquire)); if sel != Selected::Waiting { return sel;
}
if backoff.is_completed() { break;
} else {
backoff.snooze();
}
}
loop { // Check whether an operation has been selected. let sel = Selected::from(self.inner.select.load(Ordering::Acquire)); if sel != Selected::Waiting { return sel;
}
// If there's a deadline, park the current thread until the deadline is reached. iflet Some(end) = deadline { let now = Instant::now();
if now < end {
thread::park_timeout(end - now);
} else { // The deadline has been reached. Try aborting select. returnmatchself.try_select(Selected::Aborted) {
Ok(()) => Selected::Aborted,
Err(s) => s,
};
}
} else {
thread::park();
}
}
}
/// Unparks the thread this context belongs to. #[inline] pubfn unpark(&self) { self.inner.thread.unpark();
}
/// Returns the id of the thread this context belongs to. #[inline] pubfn thread_id(&self) -> ThreadId { self.inner.thread_id
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.10 Sekunden
(vorverarbeitet am 2026-06-27)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.