use std::fmt;
use std::future::Future;
use std::marker::PhantomData;
use std::pin::{Pin, pin};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex, Weak};
use std::task::{Context, Poll, Wake, Waker};
use std::time::{Duration, Instant};
use snare_interpose::{ClockKind, Domain, LeaseId};
use crate::clock::Clock;
use crate::readiness::Deadline;
use crate::scope::SimShared;
pub use snare_interpose::{
CensusThread, LeaseInfo, LeaseKind, SimId, ThreadCensus, ThreadClass, ThreadOwner,
};
pub use crate::executive::{
AttachError, AuditReport, BlockerKind, ClassEffect, Executive, ExecutiveConfig, Grant,
NotQuiescent, PState, ParticipantInfo, Quiescence, QuiescenceViolation, TimerInfo, attach,
with_driver_time,
};
pub fn thread_class() -> ThreadClass {
snare_interpose::recorded_thread_class().0
}
pub fn in_sim() -> bool {
Domain::current().is_some()
}
pub fn current_sim() -> Option<SimId> {
Domain::current().map(|domain| domain.id())
}
pub fn thread_census() -> Option<ThreadCensus> {
snare_interpose::census(Domain::current().as_ref())
}
pub fn is_participant() -> bool {
snare_interpose::thread_class() == Some(ThreadClass::Participant)
}
pub fn is_driven() -> bool {
is_participant() && snare_interpose::virtual_now().is_some()
}
pub fn mark_background(label: &'static str) {
snare_interpose::set_thread_class(ThreadClass::Background, Some(label));
}
pub fn mark_helper() {
snare_interpose::set_thread_class(ThreadClass::Helper, None);
}
pub fn mark_driver_thread() {
snare_interpose::set_thread_class(ThreadClass::Driver, None);
}
#[must_use]
pub struct ParticipantGuard {
previous: Option<(ThreadClass, Option<&'static str>)>,
name: Option<Arc<str>>,
_not_send: PhantomData<*const ()>,
}
pub fn participate(name: &str) -> ParticipantGuard {
let previous = snare_interpose::set_thread_class(ThreadClass::Participant, None);
let name = previous
.is_some()
.then(|| snare_interpose::set_thread_name(Some(name)))
.flatten();
ParticipantGuard {
previous,
name,
_not_send: PhantomData,
}
}
impl Drop for ParticipantGuard {
fn drop(&mut self) {
if let Some((class, label)) = self.previous {
snare_interpose::set_thread_name(self.name.as_deref());
snare_interpose::set_thread_class(class, label);
}
}
}
impl fmt::Debug for ParticipantGuard {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ParticipantGuard")
.field("previous", &self.previous)
.field("name", &self.name)
.finish()
}
}
#[must_use]
pub struct SpawnClassGuard {
previous: Option<(ThreadClass, &'static str)>,
_not_send: PhantomData<*const ()>,
}
pub fn spawn_as(class: ThreadClass, label: &'static str) -> SpawnClassGuard {
SpawnClassGuard {
previous: snare_interpose::set_child_class(Some((class, label))),
_not_send: PhantomData,
}
}
impl Drop for SpawnClassGuard {
fn drop(&mut self) {
snare_interpose::set_child_class(self.previous);
}
}
impl fmt::Debug for SpawnClassGuard {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("SpawnClassGuard")
.field("previous", &self.previous)
.finish()
}
}
#[must_use]
pub struct BusyLease {
held: Option<(Domain, &'static str, LeaseKind)>,
lease: Option<LeaseId>,
}
impl BusyLease {
pub(crate) fn take(domain: Domain, label: &'static str) -> Self {
Self::take_of(Some(domain), label, LeaseKind::Busy)
}
fn take_of(domain: Option<Domain>, label: &'static str, kind: LeaseKind) -> Self {
let lease = domain
.as_ref()
.map(|domain| domain.take_lease_of(label, kind));
BusyLease {
held: domain.map(|domain| (domain, label, kind)),
lease,
}
}
pub fn is_held(&self) -> bool {
self.lease.is_some()
}
pub fn label(&self) -> Option<&'static str> {
self.held.as_ref().map(|&(_, label, _)| label)
}
pub fn kind(&self) -> Option<LeaseKind> {
self.held.as_ref().map(|&(_, _, kind)| kind)
}
}
impl Drop for BusyLease {
fn drop(&mut self) {
if let (Some(lease), Some((domain, _, _))) = (self.lease.take(), &self.held) {
domain.release_lease(lease);
}
}
}
impl fmt::Debug for BusyLease {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("BusyLease")
.field("label", &self.label())
.field("kind", &self.kind())
.field("sim", &self.held.as_ref().map(|(domain, _, _)| domain.id()))
.finish()
}
}
pub fn busy(label: &'static str) -> BusyLease {
snare_interpose::note_effect("busy");
BusyLease::take_of(Domain::current(), label, LeaseKind::Busy)
}
#[must_use]
pub struct SetupScope {
lease: BusyLease,
_not_send: PhantomData<*const ()>,
}
impl Drop for SetupScope {
fn drop(&mut self) {
snare_interpose::set_in_setup(false);
}
}
impl fmt::Debug for SetupScope {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("SetupScope")
.field("lease", &self.lease)
.finish()
}
}
pub fn setup_scope(label: &'static str) -> SetupScope {
snare_interpose::set_in_setup(true);
snare_interpose::note_effect("busy");
SetupScope {
lease: BusyLease::take_of(Domain::current(), label, LeaseKind::Setup),
_not_send: PhantomData,
}
}
pub fn hint_starving(source: &str, severity: f32) {
if let Some(domain) = Domain::current() {
domain.push_hint(source, severity);
}
}
pub fn held_leases() -> Vec<LeaseInfo> {
Domain::current().map_or_else(Vec::new, |domain| domain.leases())
}
pub fn thread_name() -> Option<Arc<str>> {
snare_interpose::thread_name()
}
pub fn now() -> Duration {
crate::scope::try_here()
.and_then(|shared| {
let clock = shared.clock.as_ref()?;
Some(clock.driver_value().unwrap_or_else(|| clock.value()))
})
.unwrap_or_else(crate::readiness::real_now)
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum ParkResult {
Unparked,
TimedOut,
}
struct ParkCell {
token: AtomicBool,
thread: std::thread::Thread,
sim: Mutex<Weak<SimShared>>,
}
impl ParkCell {
fn unpark(&self) {
self.token.store(true, Ordering::Release);
self.thread.unpark();
if crate::clock::in_clock_wake() {
return;
}
let sim =
snare_interpose::real(|| self.sim.lock().unwrap_or_else(|e| e.into_inner()).upgrade());
if let Some(sim) = sim {
sim.kick();
}
}
}
impl Wake for ParkCell {
fn wake(self: Arc<Self>) {
self.unpark();
}
fn wake_by_ref(self: &Arc<Self>) {
self.unpark();
}
}
thread_local! {
static PARK_CELL: Arc<ParkCell> = Arc::new(ParkCell {
token: AtomicBool::new(false),
thread: std::thread::current(),
sim: Mutex::new(Weak::new()),
});
}
fn park_cell() -> Arc<ParkCell> {
let cell = PARK_CELL.with(Arc::clone);
if let Some(sim) = crate::scope::try_here() {
snare_interpose::real(|| {
*cell.sim.lock().unwrap_or_else(|e| e.into_inner()) = Arc::downgrade(&sim);
});
}
cell
}
#[derive(Clone)]
pub struct Unparker(Arc<ParkCell>);
impl Unparker {
pub fn unpark(&self) {
self.0.unpark();
}
pub fn waker(&self) -> Waker {
Waker::from(self.0.clone())
}
}
impl fmt::Debug for Unparker {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Unparker").finish_non_exhaustive()
}
}
pub fn current_unparker() -> Unparker {
Unparker(park_cell())
}
pub fn park(deadline: Option<Instant>) -> ParkResult {
park_as("park", deadline)
}
fn park_as(label: &'static str, deadline: Option<Instant>) -> ParkResult {
let cell = park_cell();
let in_sim = snare_interpose::thread_class().is_some();
loop {
if cell.token.swap(false, Ordering::AcqRel) {
return ParkResult::Unparked;
}
let left = match deadline {
Some(deadline) => match deadline.checked_duration_since(Instant::now()) {
Some(left) if !left.is_zero() => Some(left),
_ => return ParkResult::TimedOut,
},
None => None,
};
if !in_sim {
match left {
Some(left) => std::thread::park_timeout(left),
None => std::thread::park(),
}
continue;
}
let woken = snare_interpose::real(|| {
crate::readiness::readiness().wait_until(label, left.map(Deadline::timeout), || {
cell.token.load(Ordering::Acquire)
})
});
if !woken && left.is_none() {
let _label = snare_interpose::wait_label(label);
std::thread::park();
}
}
}
pub fn block_on<F: Future>(f: F) -> F::Output {
match block_on_until(f, None) {
Some(output) => output,
None => unreachable!("a block_on with no deadline ends only when its future does"),
}
}
pub fn block_on_until<F: Future>(f: F, deadline: Option<Instant>) -> Option<F::Output> {
let waker = Waker::from(park_cell());
let mut cx = Context::from_waker(&waker);
let mut f = pin!(f);
loop {
if let Poll::Ready(output) = f.as_mut().poll(&mut cx) {
return Some(output);
}
if park_as("block_on", deadline) == ParkResult::TimedOut {
return match f.as_mut().poll(&mut cx) {
Poll::Ready(output) => Some(output),
Poll::Pending => None,
};
}
}
}
pub fn block_on_timeout<F: Future>(f: F, timeout: Duration) -> Option<F::Output> {
block_on_until(f, Instant::now().checked_add(timeout))
}
fn instant_clock() -> Option<(Arc<Clock>, u64)> {
let clock = crate::scope::try_here()?.clock.clone()?;
if snare_interpose::thread_class() == Some(ThreadClass::Driver)
&& snare_interpose::real(|| clock.driver_time()).is_none()
{
return None;
}
let now = snare_interpose::now(ClockKind::Monotonic)?;
Some((clock, crate::clock::nanos(now)))
}
pub struct Sleep {
deadline: Instant,
at: u64,
clock: Option<Arc<Clock>>,
wake: Option<(u64, Waker)>,
}
pub fn sleep_until(deadline: Instant) -> Sleep {
snare_interpose::end_spin();
let (clock, now) = match instant_clock() {
Some((clock, now)) => (Some(clock), now),
None => (None, crate::clock::nanos(crate::readiness::real_now())),
};
let now_instant = Instant::now();
let at = match deadline.checked_duration_since(now_instant) {
Some(ahead) => now.saturating_add(crate::clock::nanos(ahead)),
None => now.saturating_sub(crate::clock::nanos(now_instant - deadline)),
};
Sleep {
deadline,
at,
clock,
wake: None,
}
}
impl Sleep {
pub fn is_elapsed(&self) -> bool {
self.reading() >= self.at
}
fn reading(&self) -> u64 {
match &self.clock {
Some(clock) => {
snare_interpose::real(|| clock.driver_time().unwrap_or_else(|| clock.monotonic()))
}
None => crate::clock::nanos(crate::readiness::real_now()),
}
}
fn on_my_clock(&self) -> bool {
match (instant_clock(), &self.clock) {
(Some((here, _)), Some(clock)) => Arc::ptr_eq(&here, clock),
(None, None) => true,
_ => false,
}
}
fn finished(&mut self) -> bool {
if !self.is_elapsed() {
return false;
}
if self.on_my_clock()
&& let Some(rest) = self.deadline.checked_duration_since(Instant::now())
&& !rest.is_zero()
{
self.at = self.reading().saturating_add(crate::clock::nanos(rest));
self.cancel();
return false;
}
self.cancel();
true
}
fn register(&mut self, waker: &Waker) {
let Some(clock) = &self.clock else {
let id = crate::clock::wake_id();
crate::readiness::schedule_wake(Duration::from_nanos(self.at), id, waker.clone());
self.wake = Some((id, waker.clone()));
return;
};
let at = Duration::from_nanos(self.at);
let here = crate::scope::try_here()
.and_then(|shared| shared.clock.clone())
.is_some_and(|c| Arc::ptr_eq(&c, clock));
let id = if here {
snare_interpose::register_wake(at, waker.clone())
} else {
snare_interpose::real(|| clock.register_wake(self.at, waker.clone(), true))
};
match id {
Some(id) => self.wake = Some((id, waker.clone())),
None => snare_interpose::real(|| clock.expire(at)),
}
}
fn cancel(&mut self) {
let Some((id, _)) = self.wake.take() else {
return;
};
match &self.clock {
Some(clock) => snare_interpose::real(|| clock.cancel_wake(id)),
None => crate::readiness::cancel_wake(id),
}
}
}
impl Future for Sleep {
type Output = ();
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
let this = self.get_mut();
if this.finished() {
return Poll::Ready(());
}
if !this
.wake
.as_ref()
.is_some_and(|(_, waker)| waker.will_wake(cx.waker()))
{
this.cancel();
snare_interpose::note_effect("sleep");
this.register(cx.waker());
}
if this.finished() {
return Poll::Ready(());
}
Poll::Pending
}
}
impl Drop for Sleep {
fn drop(&mut self) {
self.cancel();
}
}
impl fmt::Debug for Sleep {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Sleep")
.field("deadline", &self.deadline)
.field("registered", &self.wake.is_some())
.finish()
}
}
#[derive(Default)]
pub struct WakerSet {
wakers: Mutex<Vec<Waker>>,
}
impl WakerSet {
pub fn new() -> Self {
Self::default()
}
pub fn register(&self, waker: &Waker) {
snare_interpose::real(|| {
let mut wakers = self.wakers.lock().unwrap_or_else(|e| e.into_inner());
match wakers.iter_mut().find(|w| w.will_wake(waker)) {
Some(w) => w.clone_from(waker),
None => wakers.push(waker.clone()),
}
});
}
pub fn wake_all(&self) {
let wakers = snare_interpose::real(|| {
std::mem::take(&mut *self.wakers.lock().unwrap_or_else(|e| e.into_inner()))
});
wakers.into_iter().for_each(Waker::wake);
}
}
impl fmt::Debug for WakerSet {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let waiting =
snare_interpose::real(|| self.wakers.lock().unwrap_or_else(|e| e.into_inner()).len());
f.debug_struct("WakerSet")
.field("waiting", &waiting)
.finish()
}
}